oDesk PHP Test Answers 2015



1. Which of the following is used to maintain the value of a variable over 
different pages?
 
 
Answers:
 
 
• static
 
 
• global
 
 
• session_register()
 
 
• None of these
 
 
 
2. Which of the following is not a file-related function in PHP?
 
 
Answers:
 
 
• fclose
 
 
• fopen
 
 
• fwrite
 
 
• fgets
 
 
• fappend
 
3. Which of the following characters are taken care of by htmlspecialchars?
 
 
Answers:
 
 
• <
 
 
• >
 
 
• single quote
 
 
• double quote
 
 
• &
 
 
• All of these
 
4. Which of the following is not a PHP magic constant?
 
 
Answers:
 
 
• __FUNCTION__
 
 
• __TIME__
 
 
• __FILE__
 
 
• __NAMESPACE__
 
 
• __CLASS__
 
5. 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
 
6. 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.
 
7. 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.
 
8. 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.
"')");
 
9. 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)
 
10. Which of the following will check if a function exists?
 
 
Answers:
 
 
• function_exists()
 
 
• has_function()
 
 
• $a =
"function to check"; if ($a ()) // then function exists
 
 
• None of these
 
11. 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.
 
12. 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;
 
13. Which of the following variable declarations within a class is invalid in PHP?
 
 
Answers:
 
 
• private $type =
'moderate';
 
 
• internal $term = 3;
 
 
• public $amnt =
'500';
 
 
• protected $name
= 'Quantas Private Limited';
 
14. 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());
 
15. 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>
 
16. Which of the following will start a session?
 
 
Answers:
 
 
• session(start);
 
 
• session();
 
 
• session_start();
 
 
• login_sesion();
 
17. For the following code:
 
<?php
 
 
 
function Expenses()
 
{
 
    function Salary()
 
    {
 
    }
 
 
 
    function Loan()
 
    {
 
        function Balance()
 
        {
 
        }
 
    }
 
}
 
 
 
?>
 
Which of the following sequence will run successfully?
 
 
Answers:
 
 
• Expenses();Salary();Loan();Balance();
 
 
Salary();Expenses();Loan();Balance();
 
 
Expenses();Salary();Balance();Loan();
 
 
Balance();Loan();Salary();Expenses();
 
18. What enctype is required for file uploads to work?
 
 
Answers:
 
 
• multipart/form-data
 
 
• multipart
 
 
• file
 
 
application/octect-stream
 
 
• None of these
 
19. Which one of the following is not an encryption method in PHP?
 
 
Answers:
 
 
• crypt()
 
 
• md5()
 
 
• sha1()
 
 
• bcrypt()
 
20. What function should you use to join array elements with a glue string?
 
 
Answers:
 
 
• join_st
 
 
• implode

 
• connect
 
 
• make_array
 
 
• None of these
 
21. Which function can be used to delete a file?
 
 
Answers:
 
 
• delete()
 
 
• delete_file()
 
 
• unlink()
 
 
• fdelete()
 
 
• file_unlink()
 
22. What is the string concatenation operator in PHP?
 
 
Answers:
 
 
• +
 
 
• ||
 
 
• .
 
 
• |||
 
 
• None of these
 
23. Which of the following is useful for method overloading?
 
 
Answers:
 
 
• __call,__get,__set
 
 
• _get,_set,_load
 
 
__get,__set,__load
 
 
• __overload
 
24. Which of the following will store order number (34) in an 'OrderCookie'?
 
 
Answers:
 
 
• setcookie("OrderCookie",34);
 
 
makeCookie("OrderCookie",34);
 
 
Cookie("OrderCookie",34);
 
 
• OrderCookie(34);
 
25. 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.
 
26. What is the correct line to use within the php.ini file, to specify that 128MB would be the maximum amount of memory that a script may use?
 
 
Answers:
 
 
• memory_limit = 128M

 
• limit_memory =
128M
 
 
• memory_limit:
128M
 
 
• limit_memory:
128M
 
27. What is the best way to change the key without changing the value of a PHP array element?
 
 
Answers:
 
 
• $arr[$newkey] =
$oldkey; unset($arr[$oldkey]);
 
 
• $arr[$newkey] = $arr[$oldkey]; unset($arr[$oldkey]);
 
 
• $newkey =
$arr[$oldkey]; unset($arr[$oldkey]);
 
 
• $arr[$newkey] =
$oldkey.GetValue(); unset($arr[$oldkey]);
 
28. What will be the output of the following code?
 
<?
 
echo 5 * 6 / 2 + 2 * 3;
 
?>
 
 
Answers:
 
 
• 1
 
 
• 20
 
 
• 21
 
 
• 23
 
 
• 34
 
29. Does PHP 5 support exceptions?
 
 
Answers:
 
 
• Yes

 
• No