MCQ Flashcards
What will be the output of the following code?
<?php $x=; $y=20; echo $x;
?
>
Answer: a. Parse error: syntax error, unexpected ‘;’ in /opt/lampp/htdocs/er.php on line 3
Explanation: The code has a syntax error because $x is not assigned a value, leading to a parse error.
Using _____ function it is possible to trigger an error anywhere in the program.
Answer: a. trigger_error()
Explanation: The trigger_error() function is specifically designed to trigger user-level errors in PHP.
trigger_error() has at least _ number of parameters.
2
Explanation: The trigger_error() function requires at least two parameters: the error message and the error type (optional).
What will be the output of the following code?
<?php
function ($errno, $errstr) {
echo “Error:[$errno] $errstr”;
Parse error: syntax error, unexpected ‘set_error_handler’ (T_STRING) in /opt/lampp/htdocs/er.php on line 7
Explanation: The function definition is missing a name, which causes a parse error.
What will be the output of the following code?
<?php
$x = 10;
$y = 20;
z = $x + $y;
echo $z;
?
?
Answer: a. Parse error: syntax error, unexpected ‘=’ in /opt/lampp/htdocs/er.php on line 5
Explanation: The variable $z is missing the dollar sign ($) before it, which causes a syntax error.
Unexpected ‘{‘ is a Syntax Error.
a. True
b. False
Answer: a. True
Explanation: An unexpected { in PHP indicates that the parser encountered a curly brace where it did not expect one, which is indeed a syntax error.
try {
if($value == 9) {
throw new Exception(‘Number is 9 </br>’);
}
} catch(Exception $e) {
echo “</br>Exception Caught properly “, $e->getMessage();
} finally {
echo “This block clean all the activity “;
Exception Caught properly Number is 9 This block clean all the activity
Explanation: The exception is thrown and caught, displaying the message, followed by the finally block executing.
try {
if($value == 9) {
throw new Exception(‘Number is 9 </br>’);
}
} catch(Exception $e) {
echo “</br>Exception Caught properly “;
}
}
Test(99);
Test(9);
Exception Caught properly
Explanation: The first call to Test(99) does not throw an exception, so nothing is output. The second call to Test(9) throws an exception, but the catch block only outputs “Exception Caught properly”.
_____ It is a block of code where we can write code for which exception can occur.
try
Explanation: The try block is where you write code that may throw an exception.
_____ block is used after the catch block
finally
Explanation: The finally block is executed after the try and catch blocks, regardless of whether an exception was thrown.
_____ block is used to throw an exception.
a. try
b. catch
c. finally
d. None of the above
d. None of the above.
The throw statement is used to throw an exception, not a block.
Which of the following is an optional parameter?
a. host
b. dbname
c. username
d. socket
socket
Explanation: The socket parameter is optional when connecting to a MySQL database using MySQLi.
Which of the following is not a parameter of mysqli_connect()? Options:
a. host
b. username
c. port
d. query
d. query
Explanation: The query is not a parameter of the mysqli_connect() function.
Which of the following is correct syntax when the username and password are not set? Options:
a. $cn = new mysqli(“localhost”, “”, “”);
b. $cn = new mysqli(“localhost”, “NA”, “NA”);
c. $cn = new mysqli(“localhost”, “”, “”);
d. $cn = new mysqli(“localhost”, “ “, “ “);
Answer: a. $cn = new mysqli(“localhost”, “”, ““);
Explanation: This syntax correctly initializes a MySQLi connection
Which of the following is not a method of the MySQLi extension? Options:
a. fetch_array()
b. fetch_row()
c. fetch_object()
d. fetch_data()
Answer: d. fetch_data()
Explanation: fetch_data() is not a method of the MySQLi extension. The correct methods are fetch_array(), fetch_row(), and fetch_object().