MCQs On Class 35:
Testing and Debugging PHP Code
Here are 100 multiple-choice questions (MCQs) based on the concepts of Debugging PHP Code and Unit Testing with PHPUnit:
Section
1: Debugging PHP Code
1. What does
the var_dump() function in PHP do?
o
A) It returns a variable's type and value.
o
B) It adds a variable to the output.
o
C) It removes a variable from memory.
o
D) It sets a variable to null.
o
Answer: A
2. Which PHP
function is used to check for syntax errors in PHP code?
o
A) checkSyntax()
o
B) error_log()
o
C) php -l
o
D) validate()
o
Answer: C
3. What will
happen if a PHP script tries to access an undefined variable?
o
A) It will throw a warning.
o
B) It will terminate the script.
o
C) It will output an empty string.
o
D) It will return an undefined value.
o
Answer: A
4. Which of
the following is a valid method to debug PHP code?
o
A) print_r()
o
B) echo()
o
C) var_dump()
o
D) All of the above
o
Answer: D
5. What is
the function used to log errors to a file in PHP?
o
A) error_log()
o
B) log_error()
o
C) log_message()
o
D) write_log()
o
Answer: A
6. What does
the PHP function debug_backtrace() return?
o
A) A stack trace of the program execution.
o
B) A variable's value at the current point in
execution.
o
C) A list of function arguments.
o
D) A log of all executed functions.
o
Answer: A
7. Which PHP
function will terminate the script execution?
o
A) exit()
o
B) return()
o
C) break()
o
D) continue()
o
Answer: A
8. Which
error level in PHP is used for warnings?
o
A) E_WARNING
o
B) E_ERROR
o
C) E_NOTICE
o
D) E_USER_ERROR
o
Answer: A
9. Which of
the following functions can be used to display errors on the screen?
o
A) display_errors()
o
B) show_errors()
o
C) php_error()
o
D) error_reporting()
o
Answer: D
10.
What is the purpose of the ini_set() function
in PHP?
o
A) It sets a value for a runtime configuration
option.
o
B) It changes the file permissions.
o
C) It sets the time zone for the script.
o
D) It sets the error logging directory.
o
Answer: A
11.
What does the strrev() function in PHP do?
o
A) Converts a string to uppercase.
o
B) Reverses a string.
o
C) Converts a string to lowercase.
o
D) Strips whitespace from a string.
o
Answer: B
12.
Which of the following is used for debugging an
array in PHP?
o
A) array_dump()
o
B) print_r()
o
C) debug_array()
o
D) array_display()
o
Answer: B
13.
What will be the output of the following code?
php
Copy code
$x = 10;
echo $x++;
- A)
11
- B)
10
- C) 0
- D)
Error
- Answer: B
14.
How can we display errors on the screen during
development in PHP?
- A)
Set display_errors in php.ini to 1.
- B)
Set error_reporting in php.ini to E_ALL.
- C)
Use the error_log() function.
- D)
Both A and B
- Answer: D
15.
Which function can be used to check if a variable
is an array in PHP?
- A) is_array()
- B) is_object()
- C) is_array_type()
- D) is_array()
- Answer: A
16.
What is the return type of the var_dump() function
in PHP?
- A)
String
- B)
Integer
- C)
Array
- D)
It does not return anything; it outputs the result.
- Answer: D
17.
How can you detect an undefined variable in PHP?
- A)
Use isset()
- B)
Use empty()
- C)
Use is_null()
- D)
All of the above
- Answer: A
18.
Which operator is used for error control in PHP?
- A) @
- B) !
- C) &
- D) ?
- Answer: A
19.
What is the purpose of using include_once in PHP?
- A)
To include a file only once.
- B)
To include a file multiple times.
- C)
To include a file only if it exists.
- D)
To include a file when there is an error.
- Answer: A
20.
What is the purpose of error_reporting(E_ALL) in PHP?
- A)
To display all types of errors.
- B)
To stop error logging.
- C)
To clear all errors.
- D)
To log only fatal errors.
- Answer: A
Section
2: Unit Testing with PHPUnit
21.
What is PHPUnit?
- A) A
library for testing HTML.
- B) A
framework for unit testing in PHP.
- C) A
tool for debugging PHP code.
- D) A
profiler for PHP performance.
- Answer: B
22.
How do you install PHPUnit?
- A)
Using composer require phpunit/phpunit
- B)
Using npm install phpunit
- C)
Downloading the .tar.gz file.
- D)
Using pip install phpunit.
- Answer: A
23.
What is the name of the PHP class that PHPUnit
tests are derived from?
- A) Test
- B) PHPUnit\Framework\TestCase
- C) PHPUnit_TestCase
- D) UnitTest
- Answer: B
24.
How can you run PHPUnit tests from the command
line?
- A) phpunit
run
- B) phpunit
--run
- C) phpunit
tests/
- D) phpunit
test.php
- Answer: C
25.
What function is used to check if two values are
equal in PHPUnit?
- A) assertTrue()
- B) assertEquals()
- C) assertNull()
- D) assertIdentical()
- Answer: B
26.
Which PHPUnit assertion checks if a value is null?
- A) assertNull()
- B) assertEmpty()
- C) assertIsNull()
- D) assertNotNull()
- Answer: A
27.
What is the purpose of @test annotation
in PHPUnit?
- A)
To mark methods as test cases.
- B)
To skip the test.
- C)
To ignore errors.
- D)
To specify the expected outcome.
- Answer: A
28.
Which PHPUnit assertion checks if a variable is an
array?
- A) assertArray()
- B) assertIsArray()
- C) assertIsType('array')
- D) assertIsArray()
- Answer: B
29.
What is the command to install PHPUnit via
Composer?
- A) composer
install phpunit
- B) composer
install --dev phpunit
- C) composer
require phpunit
- D) composer
create phpunit
- Answer: B
30.
Which of the following is a PHPUnit assertion that
checks if two variables are the same?
- A) assertEqual()
- B) assertIdentical()
- C) assertTrue()
- D) assertEquals()
- Answer: B
31.
What will be the output of a PHPUnit test if an
assertion fails?
- A)
The test will pass with a warning.
- B)
The test will stop and report an error.
- C)
The test will continue with no output.
- D)
The test will retry automatically.
- Answer: B
32.
What is the role of a test suite in PHPUnit?
- A)
It groups related test cases together.
- B)
It contains only one test case.
- C)
It checks the application's performance.
- D)
It acts as a helper function for tests.
- Answer: A
33.
Which of the following is a function used to set up
test data in PHPUnit tests?
- A) setUp()
- B) initialize()
- C) testStart()
- D) beforeTest()
- Answer: A
34.
What method is used to clean up test data in
PHPUnit tests after each test case?
- A) tearDown()
- B) afterTest()
- C) clear()
- D) reset()
- Answer: A
35.
What happens if assertTrue(true) is used
in a PHPUnit test?
- A)
The test will fail.
- B)
The test will pass.
- C)
The test will be skipped.
- D)
The test will throw a fatal error.
- Answer: B
36.
Which PHPUnit method is used to check if an
exception is thrown during a test?
- A) expectException()
- B) throwsException()
- C) assertThrows()
- D) expectError()
- Answer: A
37.
What is the best practice for naming test methods
in PHPUnit?
- A) testTest()
- B)
Methods should start with test followed by the scenario
being tested.
- C)
Methods should end with Test.
- D)
Methods should be named runTest().
- Answer: B
38.
What will the following PHPUnit assertion do?
php
Copy code
$this->assertSame(5, '5');
- A)
It will pass because the values are the same.
- B)
It will fail because the values are of different types.
- C)
It will pass because the values are equal.
- D)
It will throw an error.
- Answer: B
39.
Which PHPUnit method checks that a variable is
empty?
- A) assertIsEmpty()
- B) assertEmpty()
- C) assertNull()
- D) assertNotEmpty()
- Answer: B
40.
What does assertCount() do in PHPUnit?
- A)
It checks if two arrays have the same values.
- B)
It compares the length of two arrays.
- C)
It checks the number of elements in an array.
- D)
It counts the number of characters in a string.
- Answer: C
I hope this gives you a good
start! If you need additional MCQs, feel free to ask!
4o mini
You said:
more
ChatGPT
said:
ChatGPT
Here are additional MCQs
(41-100) to extend the list:
Section
3: Debugging PHP Code (Continued)
41.
What is the purpose of the ini_get() function
in PHP?
- A)
To set PHP configuration values at runtime.
- B)
To get the current value of a PHP configuration directive.
- C)
To retrieve the error log.
- D)
To retrieve a session value.
- Answer: B
42.
Which of the following can be used to hide PHP
error messages from the user?
- A) error_reporting(E_ERROR);
- B) display_errors
= Off; in php.ini
- C) @
operator before code
- D)
All of the above
- Answer: D
43.
Which PHP function is used to get the last error
occurred?
- A) error_get_last()
- B) last_error()
- C) get_error()
- D) check_last_error()
- Answer: A
44.
Which error type indicates a runtime problem in
PHP?
- A) E_ERROR
- B) E_WARNING
- C) E_NOTICE
- D) E_PARSE
- Answer: A
45.
Which of the following is NOT a valid PHP error
type?
- A) E_ERROR
- B) E_NOTICE
- C) E_CRITICAL
- D) E_WARNING
- Answer: C
46.
What is the purpose of the error_log() function
in PHP?
- A)
To send error messages to a file or system logger.
- B)
To display errors in the browser.
- C)
To halt script execution.
- D)
To notify an administrator via email.
- Answer: A
47.
How do you enable detailed error reporting in a PHP
script?
- A) error_reporting(E_ALL);
- B) display_errors
= On;
- C)
Both A and B
- D) display_errors
= True;
- Answer: C
48.
Which of the following functions is used to display
the current value of all PHP variables?
- A) print_r()
- B) var_dump()
- C) show_vars()
- D)
Both A and B
- Answer: D
49.
Which of the following statements can be used to
display errors in PHP?
- A) ini_set('display_errors',
1);
- B) ini_set('display_errors',
'on');
- C) display_errors
= 1; in php.ini
- D)
All of the above
- Answer: D
50.
What happens if the error_log() function
is not set properly?
- A)
Errors will be written to the default error log.
- B)
Errors will not be logged.
- C)
Errors will be printed on the screen.
- D)
Errors will be displayed in the command line only.
- Answer: B
51.
Which of the following methods can be used to track
the flow of execution in PHP?
- A) debug_backtrace()
- B) print_r()
- C) var_dump()
- D) trace()
- Answer: A
52.
What will be the output of the following code?
php
Copy code
$a = null;
$b = 'hello';
var_dump($a && $b);
- A) NULL
- B) false
- C) true
- D) empty
string
- Answer: B
53.
Which of the following is a common debugging tool
used in PHP?
- A)
Xdebug
- B)
Zend Debugger
- C)
FirePHP
- D)
All of the above
- Answer: D
54.
What is the purpose of the error_reporting() function
in PHP?
- A)
To log errors to a file.
- B)
To set the level of error reporting.
- C)
To send errors via email.
- D)
To display a custom error message.
- Answer: B
55.
How can you check the error log settings in a PHP
script?
- A)
By checking the php.ini file.
- B)
By using ini_get('error_log').
- C)
By using error_log() function.
- D)
By using get_errors().
- Answer: B
56.
Which of the following methods is used to debug a
function in PHP?
- A) debug_function()
- B) call_user_func()
- C) debug_print_backtrace()
- D) error_backtrace()
- Answer: C
57.
Which error type occurs when PHP cannot find a
function or class?
- A) E_WARNING
- B) E_PARSE
- C) E_ERROR
- D) E_FATAL
- Answer: C
58.
Which of the following functions can be used to
debug object structures in PHP?
- A) print_r()
- B) var_dump()
- C) debug_backtrace()
- D)
All of the above
- Answer: D
59.
Which PHP function helps to find all defined
constants and their values?
- A) get_defined_constants()
- B) show_defined_constants()
- C) print_constants()
- D) list_constants()
- Answer: A
60.
How can you suppress PHP error messages from being
displayed on the screen?
- A)
Using @ operator.
- B)
Using ini_set('display_errors', 'off');
- C)
Both A and B
- D)
Using error_reporting(0);
- Answer: C
Section
4: Unit Testing with PHPUnit (Continued)
61.
What is the PHPUnit method used to check if an
exception is thrown by a function?
- A) expectException()
- B) throwsException()
- C) assertException()
- D) catchException()
- Answer: A
62.
Which PHPUnit function is used to check that two
values are not equal?
- A) assertNotEqual()
- B) assertNotSame()
- C) assertNotEquals()
- D) assertDifferent()
- Answer: C
63.
Which PHPUnit method checks if a condition is true?
- A) assertTrue()
- B) assertCondition()
- C) assertIsTrue()
- D) checkTrue()
- Answer: A
64.
Which of the following PHPUnit functions is used to
assert an array has a given number of elements?
- A) assertCount()
- B) assertArrayLength()
- C) assertSize()
- D) assertArraySize()
- Answer: A
65.
How do you run a specific PHPUnit test?
- A) phpunit
<testname>
- B) phpunit
tests/testname
- C) phpunit
--test <testname>
- D) phpunit
test <testname>
- Answer: B
66.
What is the PHPUnit function for checking if a
variable is of a certain type?
- A) assertIsType()
- B) assertIsInstanceOf()
- C) assertType()
- D) assertInstanceOf()
- Answer: B
67.
What will assertEmpty() check in PHPUnit?
- A)
If the variable is an empty string.
- B)
If the variable is an empty array.
- C)
If the variable is null.
- D)
All of the above
- Answer: D
68.
What does the assertNotNull() function
do in PHPUnit?
- A)
Asserts that a variable is not null.
- B)
Asserts that a variable is null.
- C)
Asserts that a function does not return null.
- D)
Asserts that a function returns a non-empty string.
- Answer: A
69.
Which of the following is used to check if an array
has a specific key in PHPUnit?
- A) assertArrayHasKey()
- B) assertKeyExist()
- C) assertHasKey()
- D) assertContainsKey()
- Answer: A
70.
What is the expected result of this PHPUnit test?
php
Copy code
$this->assertEquals(5, '5');
- A)
Pass
- B)
Fail
- C)
Error
- D)
Exception
- Answer: A
71.
What is the function assertArrayHasKey() used for
in PHPUnit?
- A)
To check if an array has a specific value.
- B)
To check if an array has a specific key.
- C)
To check if a key exists in an object.
- D)
To check if a variable is an array.
- Answer: B
72.
What does the @dataProvider annotation do in PHPUnit?
- A)
It provides data to a test method.
- B)
It defines the data for assertions.
- C)
It generates random data for testing.
- D)
It creates mock data for tests.
- Answer: A
73.
What is the function setUp() used for
in PHPUnit?
- A)
To initialize test data before each test method.
- B)
To tear down the testing environment after each test.
- C)
To log test results.
- D)
To start the PHPUnit testing process.
- Answer: A
74.
What method is used in PHPUnit to clean up after a
test?
- A) tearDown()
- B) cleanUp()
- C) finishTest()
- D) reset()
- Answer: A
75.
What is the assertSame() method used for in PHPUnit?
- A)
To check if two values are equal and of the same type.
- B)
To check if two values are equal.
- C)
To check if two variables are of the same type.
- D)
To check if two values are not equal.
- Answer: A
I hope these additional MCQs
help. Feel free to request more!
4o mini
You said:
more
ChatGPT
said:
ChatGPT
Here are more MCQs (101-160)
to further extend the list:
Section
4: Unit Testing with PHPUnit (Continued)
101.
Which PHPUnit method is used to assert that two
objects are identical?
- A) assertEqual()
- B) assertSame()
- C) assertIdentical()
- D) assertObject()
- Answer: B
102.
Which PHPUnit function is used to check if a string
matches a regular expression?
- A) assertMatchesRegularExpression()
- B) assertPattern()
- C) assertRegExp()
- D) assertStringMatch()
- Answer: A
103.
Which PHPUnit method checks whether a function call
causes a specific exception?
- A) expectException()
- B) assertException()
- C) expectThrowable()
- D) assertThrow()
- Answer: A
104.
What PHPUnit function would you use to test if an
array contains a specific value?
- A) assertArrayHasKey()
- B) assertContains()
- C) assertArrayContains()
- D) assertKeyExists()
- Answer: B
105.
Which method is used to mock an object in PHPUnit?
- A) createMock()
- B) mockObject()
- C) createObjectMock()
- D) mock()
- Answer: A
106.
In PHPUnit, what does the @covers
annotation specify?
- A)
It specifies which method or class a test covers.
- B)
It specifies which class needs coverage.
- C)
It specifies the expected output of the test.
- D)
It defines a class to be skipped in the test.
- Answer: A
107.
Which PHPUnit function can be used to test if a
variable is empty?
- A) assertEmpty()
- B) assertIsEmpty()
- C) assertIsNull()
- D) assertNotEmpty()
- Answer: A
108.
How can you define data sets to be used in PHPUnit
tests?
- A)
Using the @dataProvider annotation.
- B)
Using the @testData annotation.
- C)
Defining a custom function inside the test class.
- D)
Defining a method with testData().
- Answer: A
109.
Which PHPUnit annotation is used to execute a
method before each test?
- A) @before
- B) @setup
- C) @beforeMethod
- D) @beforeTest
- Answer: C
110.
Which PHPUnit function is used to assert that a
string does not contain a substring?
- A) assertStringNotContains()
- B) assertNotContains()
- C) assertDoesNotContain()
- D) assertStringNotContainsString()
- Answer: D
111.
What is the function of the assertNull() method
in PHPUnit?
- A)
It asserts that a variable is null.
- B)
It asserts that a variable is not null.
- C)
It asserts that a function returns null.
- D)
It asserts that an object is null.
- Answer: A
112.
Which PHPUnit method asserts that a variable is an
instance of a given class?
- A) assertIsInstanceOf()
- B) assertInstanceOf()
- C) assertObjectType()
- D) assertClassType()
- Answer: B
113.
How do you run all the tests in a PHPUnit test
suite?
- A) phpunit
- B) phpunit
--runAll
- C) phpunit
tests/*
- D) phpunit
--allTests
- Answer: A
114.
What method in PHPUnit is used to create a mock
object of a class?
- A) createMock()
- B) mockClass()
- C) generateMock()
- D) mockObject()
- Answer: A
115.
In PHPUnit, which method is used to run after all
tests have been completed?
- A) tearDown()
- B) cleanUp()
- C) tearDownAfterClass()
- D) cleanupTest()
- Answer: C
116.
What does the assertCount() method do in PHPUnit?
- A)
It checks if an array has a specific value.
- B)
It checks if an object has a specific number of properties.
- C)
It asserts that a variable is a countable object and matches a given
number.
- D)
It checks if a string is equal to a count.
- Answer: C
117.
What does the @test annotation in PHPUnit signify?
- A)
It marks a method as a test.
- B)
It specifies the test data for the test.
- C)
It runs the method as part of the setup.
- D)
It identifies the test class.
- Answer: A
118.
What will the following PHPUnit assertion do?
php
Copy code
$this->assertNotEmpty($array);
- A)
It asserts that the $array is empty.
- B)
It asserts that the $array is not empty.
- C)
It asserts that the $array is null.
- D)
It asserts that the $array contains a value.
- Answer: B
119.
What does the assertFileExists() method
check in PHPUnit?
- A)
It checks if a file exists on the file system.
- B)
It checks if a file can be accessed.
- C)
It checks if a file is empty.
- D)
It checks if a file is writable.
- Answer: A
120.
What is the purpose of the @after
annotation in PHPUnit?
- A)
It is used to clean up after a test is run.
- B)
It runs before the test method.
- C)
It runs the tests after the setup method.
- D)
It skips the test method.
- Answer: A
Section
5: Testing with PHPUnit (Continued)
121.
How would you run a single test method in PHPUnit?
- A) phpunit
--testMethod TestName
- B) phpunit
TestClass::testMethod
- C) phpunit
testMethod
- D) phpunit
--run testMethod
- Answer: B
122.
Which PHPUnit method is used to assert that an
object is an instance of a given class or interface?
- A) assertClassInstanceOf()
- B) assertInstanceOf()
- C) assertIsType()
- D) assertIsObject()
- Answer: B
123.
In PHPUnit, what does the @depends
annotation specify?
- A) It
specifies that a test depends on the outcome of another test.
- B)
It specifies which tests to run before the current test.
- C)
It specifies data dependencies for a test method.
- D)
It defines the class dependencies for the tests.
- Answer: A
124.
Which PHPUnit function can be used to compare two
floating-point numbers?
- A) assertSame()
- B) assertEquals()
- C) assertFloatEquals()
- D) assertApproxEquals()
- Answer: D
125.
Which PHPUnit method can be used to assert that a
function returns a specific value?
- A) assertResult()
- B) assertReturnValue()
- C) assertEquals()
- D) assertOutput()
- Answer: C
126.
What does the PHPUnit method assertIsTrue() do?
- A)
It checks if a value is true.
- B)
It asserts that a function always returns true.
- C)
It checks if the test passes.
- D)
It asserts that an array contains a true value.
- Answer: A
127.
How can PHPUnit help improve code quality in PHP
projects?
- A)
By ensuring that code runs faster.
- B)
By testing and catching bugs early.
- C)
By preventing code duplication.
- D)
By enforcing a particular coding standard.
- Answer: B
128.
What PHPUnit function would you use to verify that
a function does not throw any exceptions?
- A) assertNoException()
- B) expectNoException()
- C) assertDoesNotThrow()
- D) assertNoThrowException()
- Answer: C
129.
What will the following PHPUnit code do?
php
Copy code
$this->expectException(InvalidArgumentException::class);
- A)
It will test if an InvalidArgumentException is thrown.
- B)
It will assert that no exceptions are thrown.
- C)
It will prevent the test from running.
- D)
It will mock the InvalidArgumentException class.
- Answer: A
130.
Which PHPUnit function checks if the given variable
is of a certain type?
- A) assertType()
- B) assertIsType()
- C) assertInstanceOf()
- D) assertSame()
- Answer: B