Pages

Friday, July 8, 2022

PHP Test 2022-23

We don't guarantee that all provided answers is correct.


1. Which of the following is true about the singleton design pattern?



Answers:

• A singleton pattern means that a class will only have a single method.
• A singleton pattern means that a class can have only one instance object.
• A singleton pattern means that a class has only a single member variable.
• Singletons cannot be implemented in PHP.


2. Which of the following is useful for method overloading?



Answers:

• __call,__get,__set
• _get,_set,_load
• __get,__set,__load
• __overload


3. What is the best practice for running MySQL queries in PHP? Consider the risk of SQL injection.



Answers:

• Use mysql_query() and variables: for example: $input = $_POST[‘user_input’]; mysql_query(«INSERT INTO table (column) VALUES (‘» . $input . «‘)»);
• Use PDO prepared statements and parameterized queries: for example: $input= $_POST[«user-input»] $stmt = $pdo->prepare(‘INSERT INTO table (column) VALUES («:input»); $stmt->execute(array(‘:input’ => $input));
• Use mysql_query() and string escaped variables: for example: $input= $_POST[«user-input»] $input_safe = mysql_real_escape_string($input); mysql_query(«INSERT INTO table (column) VALUES (‘» . $input. «‘)»);
• Use mysql_query() and variables with a blacklisting check: for example: $blacklist = array(«DROP»,»INSERT»,»DELETE»); $input= $_POST[«user-input»] if (!$array_search($blacklist))) mysql_query(«INSERT INTO table (column) VALUES (‘» . $input. «‘)»);


4. Which of the following methods should be used for sending an email using the variables $to, $subject, and $body?



Answers:

• mail($to,$subject,$body)
• sendmail($to,$subject,$body)
• mail(to,subject,body)
• sendmail(to,subject,body)


5. Which of the following will print out the PHP call stack?


Answers:

• $e = new Exception; var_dump($e->debug());
• $e = new Exception; var_dump($e->getTraceAsString());
• $e = new Exception; var_dump($e->backtrace());
• $e = new Exception; var_dump($e->getString());


6. What will be the output of the following code?


<?php
var_dump (3*4);
?>


Answers:

• int(3*4)
• int(12)
• 3*4
• 12
• None of the above


7. Which of the following will start a session?


Answers:

• session(start);
• session();
• session_start();
• login_sesion();


8. Which of the following is incorrect with respect to separating PHP code and HTML?


Answers:

• Use an MVC design pattern.
• As PHP is a scripting language, HTML and PHP cannot be separated.
• Use any PHP template engine e.g: smarty to keep the presentation separate from business logic.
• Create one script containing your (PHP) logic outputting XML and one script produce the XSL to translate the XML to views.


9. Which of the following will read an object into an array variable?


Answers:

• $array_variable = get_object_vars($object);
• $array_variable = (array)$object;
• $array_variable = array $object;
• $array_variable = get_object_vars $object;


10. Which of the following is correct about Mysqli and PDO?



Answers:

• Mysqli provides the procedural way to access the database while PDO provides the object oriented way.
• Mysqli can only be used to access MySQL database while PDO can be used to access any DBMS.
• MySQLi prevents SQL Injection whereas PDO does not.
• MySQLi is used to create prepared statements whereas PDO is not.


11. Which of the following characters are taken care of by html special chars?



Answers:

• <
• >
• single quote
• double quote
• &
• All of these


12. Which function can be used to delete a file?



Answers:

delete()
• delete_file()
• unlink()
• fdelete()
• file_unlink()


13. What would occur if a fatal error was thrown in your PHP program?



Answers:



• The PHP program will stop executing at the point where the error occurred.
• The PHP program will show a warning message and program will continue executing.
• Since PHP is a scripting language so it does not have fatal error.
• Nothing will happen.


14. What is the correct way to send a SMTP (Simple Mail Transfer Protocol) email using PHP?



Answers:

• s.sendmail($EmailAddress, [$MessageBody], msg.as_string())
• sendmail($EmailAddress, «Subject», $MessageBody);
• mail($EmailAddress, «Subject», $MessageBody);
• <a href=»mailto:$EmailAddress»>$MessageBody</a>


15. Which of the following is not a PHP magic constant?



Answers:

• __FUNCTION__
• __TIME__
• __FILE__
• __NAMESPACE__
• __CLASS__


16. Which of the following is used to maintain the value of a variable over different pages?



Answers:

• static
• global
• session_register()
• None of these


17. Which of the following is not related to debugging in PHP?



Answers:

• watch
• PDO
• breakpoints
• call stack


18. Should assert() be used to check user input?



Answers:

• Yes
• No


19. Which function will suitably replace ‘X’ if the size of a file needs to be checked?



$size=X(filename);



Answers:

• filesize
• size
• sizeofFile
• getSize


20. Which of the following is not a predefined constant?



Answers:

• TRUE
• FALSE
• NULL
• __FILE__
• CONSTANT


21. What will be the output of the following code?

<?php
$str=»Hello»;
$test=»lo»;



echo substr_compare($str, $test, -strlen($test), strlen($test)) === 0;
?>



Answers:



• FALSE
• Syntax error
• 0
• 1


22. Which of the following functions is not used in debugging?



Answers:



• var_dump()
• fprintf()
• print_r()
• var_export()


23. Which of the following cryptographic functions in PHP returns the longest hash value?



Answers:



• md5()
• sha1()
• crc32()
• All return the same hash value length.


24. Consider the following class:



1 class Insurance
2 {
3 function clsName()
4 {
5 echo get_class($this);
6 }
7 }
8 $cl = new Insurance();
9 $cl->clsName();
10 Insurance::clsName();



Which of the following lines should be commented to print the class name without errors?



Answers:



• Line 8 and 9
• Line 10
• Line 9 and 10
• All the three lines 8,9, and 10 should be left as it is.


25. Without introducing a non-class member variable, which of the following can be used to keep an eye on the existing number of objects of a given class?



Answers:



• Adding a member variable that gets incremented in the default constructor and decremented in the destructor.
• Adding a local variable that gets incremented in each constructor and decremented in the destructor.
• Add a static member variable that gets incremented in each constructor and decremented in the destructor.
• This cannot be accomplished since the creation of objects is being done dynamically via «new.»


26. Which of these is not a valid PHP XML API?



Answers:



• a.libxml_clear_errors()
• b.libXMLError()
• c.libxml_get_errors()
• d.libxml_use_internal_errors()


27. Which of the following is not a valid API?



Answers:



• trigger_print_error()
• trigger_error()
• debug_backtrace()
• debug_print_backtrace()


28. Which of the following is true about posting data using cURL in PHP?



Answers:



• Data can be posted using only the POST method.
• Data can be posted using only the GET method.
• Data can be posted using both GET and POST methods.
• Data cannot be posted using cURL.


29. Which of the following is the correct way to check if a session has already been started?



Answers:



• if ($_SERVER[«session_id»]) echo ‘session started’;
• if (session_id()) echo ‘session started’;
• if ($_SESSION[«session_id»]) echo ‘session started’;
• if ($GLOBALS[«session_id»]) echo ‘session started’;


30. Which statement will return true?



Note: There may be more than one right answer



Answers:



• a. is_numeric («200»)
• b. is_numeric («20,0»)
• c. is_numeric («$200»)
• d. is_numeric («.25e4»)
• e. None


31. Which of the the following are PHP file upload-related functions?



Answers:



• upload_file()
• is_uploaded_file()
• move_uploaded_file()
• None of these


32. Which of the following statements regarding PHP forms, are correct?



Note: There may be more than one right answer



Answers:



• In PHP, the predefined $_POST variable is used to collect values in a form with method=»post».
• In PHP, the predefined $_GET variable is used to collect values in a form with method=»get».
• In PHP, the predefined $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.
• Information sent from a form with the POST method is invisible to others and has an 8MB limit on the amount of information to send, which cannot be changed.


33. What is the output of the following code?



<?php
$array = array(«1″,»2″,»3″,»4»);
$variable = end(array_keys($array));
echo $variable;
?>



Answers:



• 1
• 2
• 3
• 4


34. What would be the output of the following code?



<?php
$arr = array(«foo»,
«bar»,
«baz»);
for ($i = 0; $i < count($arr); $i++) { 
$item = $arr[$i]; 
}
echo «<pre>»;
print_r($item);
echo «</pre>»;
?>



Answers:



• Array ( [0] => foo [1] => bar [2] => baz )
• foo
• bar
• baz


35. Which of the following is not a valid php.ini parameter with respect to file uploading?



Answers:



• upload_max_filesize 
• allow_url_fopen
• upload_tmp_dir 
• post_max_size


36. What is the correct syntax of mail() function in PHP?



Answers:



• mail($to,$subject,$message,$headers)
• mail($from,$to,$subject,$message)
• mail($to,$from,$subject,$message)
• mail($to,$from,$message,$headers)


37. What is the correct PHP command to use to catch any error messages within the code?



Answers:



• set_error(‘set_error’);
• set_error_handler(‘error_handler’);
• set_handler(‘set_handler’);
• set_exception(‘set_exception’);


38. Which of the following is not a valid xdebug configuration setting?



Answers:



• xdebug.var_display_max_depth
• xdebug.var_display_max_children
• xdebug.var_display_max_data
• xdebug.trace


39. What is wrong with the following code?



<?php
curl_setopt($ch, CURLOPT_URL, «http://www.example.com/»);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>



Answers:



• There is nothing wrong with the code.
• The cURL resource $ch has not been created using the curl_init() method.
• The $ch variable needs to be initialized as $ch=null;.
• The code will cause a parse error.


40. What is the difference between die() and exit() in PHP?



Answers:



• die() is an alias for exit().
• exit() is a function, die() is a language construct and cannot be called using variable functions.
• die() accepts a string as its optional parameter which is printed before the application terminates; exit() accepts an integer as its optional parameter which is passed to the operating system as the exit code.
• die() terminates the script immediately, exit() calls shutdown functions and object destructors first.




No comments:

Post a Comment