Upwork PHP Test 2016
1. What is the best practice for running MySQL queries in PHP? Consider the risk of SQL injection.
Answers:
a. Use mysql_query() and variables: for example: $input = $_POST[‘user_input’]; mysql_query(“INSERT INTO table (column) VALUES (‘” . $input . “‘)”);
b. 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));
c. 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. “‘)”);
d. 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. “‘)”);
2. Which of the following methods should be used for sending an email using the variables $to, $subject, and $body?
Answers:
a. mail($to,$subject,$body)
b. sendmail($to,$subject,$body)
c. mail(to,subject,body)
d. sendmail(to,subject,body)
3. Which of the following is used to maintain the value of a variable over different pages?
Answers:
a. static
b. global
c. session_register()
d. None of these
4. Which of the following will check if a function exists?
Answers:
a. function_exists()
b. has_function()
c. $a = “function to check”; if ($a ()) // then function exists
d. None of these
5. Which of the following is not a file-related function in PHP?
Answers:
a. fclose
b. fopen
c. fwrite
d. fgets
a. fappend
6. Which of the following is true about the singleton design pattern?
Answers:
a. A singleton pattern means that a class will only have a single method.
b. A singleton pattern means that a class can have only one instance object.
c. A singleton pattern means that a class has only a single member variable.
d. Singletons cannot be implemented in PHP.
7. Which of the following characters are taken care of by htmlspecialchars?
Answers:
a. < a. >
b. single quote
c. double quote
d. &
a. All of these
8. Which of the following will read an object into an array variable?
Answers:
a. $array_variable = get_object_vars($object);
b. $array_variable = (array)$object;
c. $array_variable = array $object;
d. $array_variable = get_object_vars $object;
9. Which of the following variable declarations within a class is invalid in PHP?
Answers:
a. private $type = ‘moderate’;
b. internal $term = 3;
c. public $amnt = ‘500’;
d. protected $name = ‘Quantas Private Limited’;
10. Which of the following is not a PHP magic constant?
Answers:
a. __FUNCTION__
b. __TIME__
c. __FILE__
d. __NAMESPACE__
e. __CLASS__
11. Which of the following will print out the PHP call stack?
Answers:
a. $e = new Exception; var_dump($e->debug());
b. $e = new Exception; var_dump($e->getTraceAsString());
c. $e = new Exception; var_dump($e->backtrace());
d. $e = new Exception; var_dump($e->getString());
12. What will be the output of the following code?
Answers:
a. int(3*4)
b. int(12)
c. 3*4
d. 12
e. None of the above
13. Which of the following is correct about Mysqli and PDO?
Answers:
a. Mysqli provides the procedural way to access the database while PDO provides the object oriented way.
b. Mysqli can only be used to access MySQL database while PDO can be used to access any DBMS.
c. MySQLi prevents SQL Injection whereas PDO does not.
d. MySQLi is used to create prepared statements whereas PDO is not.
14. What is the correct way to send a SMTP (Simple Mail Transfer Protocol) email using PHP?
Answers:
a. s.sendmail($EmailAddress, [$MessageBody], msg.as_string())
b. sendmail($EmailAddress, “Subject”, $MessageBody);
c. mail($EmailAddress, “Subject”, $MessageBody);
d. $MessageBody
15. Which of the following will start a session?
Answers:
a. session(start);
b. session();
c. session_start();
d. login_sesion();
16. For the following code:
Which of the following sequence will run successfully?
Answers:
a. Expenses();Salary();Loan();Balance();
b. Salary();Expenses();Loan();Balance();
c. Expenses();Salary();Balance();Loan();
d. Balance();Loan();Salary();Expenses();
17. What enctype is required for file uploads to work?
Answers:
a. multipart/form-data
b. multipart
c. file
d. application/octect-stream
e. None of these
18. Which of the following is incorrect with respect to separating PHP code and HTML?
Answers:
a. Use an MVC design pattern.
b. As PHP is a scripting language, HTML and PHP cannot be separated.
c. Use any PHP template engine e.g: smarty to keep the presentation separate from business logic.
d. Create one script containing your (PHP) logic outputting XML and one script produce the XSL to translate the XML to views.
19. Which one of the following is not an encryption method in PHP?
Answers:
a. crypt()
b. md5()
c. sha1()
d. bcrypt()
20. What function should you use to join array elements with a glue string?
Answers:
a. join_st
b. implode
c. connect
d. make_array
e. None of these
21. Which function can be used to delete a file?
Answers:
a. delete()
b. delete_file()
c. unlink()
d. fdelete()
e. file_unlink()
22. What is the string concatenation operator in PHP?
Answers:
a. +
b. ||
c. .
d. |||
e. None of these
23. Which of the following is useful for method overloading?
Answers:
a. __call,__get,__set
b. _get,_set,_load
c. __get,__set,__load
d. __overload
24. Which of the following will store order number (34) in an ‘OrderCookie’?
Answers:
a. setcookie(“OrderCookie”,34);
a. makeCookie(“OrderCookie”,34);
a. Cookie(“OrderCookie”,34);
a. OrderCookie(34);
25. What would occur if a fatal error was thrown in your PHP program?
Answers:
a. The PHP program will stop executing at the point where the error occurred.
b. The PHP program will show a warning message and program will continue executing.
c. Since PHP is a scripting language so it does not have fatal error.
d. 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:
a. memory_limit = 128M
b. limit_memory = 128M
c. memory_limit: 128M
d. limit_memory: 128M
27. What is the best way to change the key without changing the value of a PHP array element?
Answers:
a. $arr[$newkey] = $oldkey; unset($arr[$oldkey]);
b. $arr[$newkey] = $arr[$oldkey]; unset($arr[$oldkey]);
c. $newkey = $arr[$oldkey]; unset($arr[$oldkey]);
d. $arr[$newkey] = $oldkey.GetValue(); unset($arr[$oldkey]);
28. What will be the output of the following code?
<? echo 5 * 6 / 2 + 2 * 3; ?>
Answers:
a. 1
b. 20
a. 21
a. 23
b. 34
29. Does PHP 5 support exceptions?
Answers:
a. Yes
b. No
21 NOT Answered Yet Test Questions:
(hold on, will be updated soon)
30. Which of the the following are PHP file upload-related functions?
Answers:
a. upload_file()
b. is_uploaded_file()
c. move_uploaded_file()
d. None of these
31. Which of the following cryptographic functions in PHP returns the longest hash value?
Answers:
a. md5()
b. sha1()
c. crc32()
d. All return the same hash value length.
32. Which of the following is not a valid API?
Answers:
a. trigger_print_error()
b. trigger_error()
c. debug_backtrace()
d. debug_print_backtrace()
33.
What will be the output of the following code?
Answers:
a. 150 . 7
b. 1507
c. 150.7
d. Integers can’t be concatenated.
a. An error will be thrown.
34. Which of these is not a valid SimpleXML Parser method?
Answers:
a. simplexml_import_dom()
b. simplexml_import_sax()
c. simplexml_load_file()
d. simplexml_load_string()
35. Which of the following environment variables is used to fetch the IP address of the user in a PHP application?
Answers:
a. $IP_ADDR
b. $REMOTE_ADDR_USER
c. $REMOTE_ADDR
d. $IP_ADDR_USER
36. 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:
a. Line 8 and 9
b. Line 10
c. Line 9 and 10
d. All the three lines 8,9, and 10 should be left as it is.
37. What is the correct syntax of mail() function in PHP?
Answers:
a. mail($to,$subject,$message,$headers)
b. mail($from,$to,$subject,$message)
c. mail($to,$from,$subject,$message)
d. mail($to,$from,$message,$headers)
38. Given the following array:
$array = array(0 => ‘blue’, 1 => ‘red’, 2 => ‘green’, 3 => ‘red’);
Which one of the following will print 2?
Answers:
a. echo array_search(‘green’, $array);
b. echo in_array(‘green’, $array);
c. echo array_key_exists(2, $array);
d. echo array_search(‘red’,$array);
39. Which function will suitably replace ‘X’ if the size of a file needs to be checked?
$size=X(filename);
Answers:
a. filesize
b. size
c. sizeofFile
d. getSize
40. Which of the following will not give the correct date and time in PHP?
Answers:
a. date(“Y-m-d H:i:s”)
b. date(“y-m-d H:i:s”)
c. date(“f, j Y H:i:s”)
d. date(“F, j Y H:i:s”)
41. Which of the following functions is not used in debugging?
Answers:
a. var_dump()
b. fprintf()
c. print_r()
d. var_export()
42. What is the difference between die() and exit() in PHP?
Answers:
a. die() is an alias for exit().
b. exit() is a function, die() is a language construct and cannot be called using variable functions.
c. 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.
d. die() terminates the script immediately, exit() calls shutdown functions and object destructors first.
43. Should assert() be used to check user input?
Answers:
a. Yes
b. No
44. 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:
a. Adding a member variable that gets incremented in the default constructor and decremented in the destructor.
b. Adding a local variable that gets incremented in each constructor and decremented in the destructor.
c. Add a static member variable that gets incremented in each constructor and decremented in the destructor.
d. This cannot be accomplished since the creation of objects is being done dynamically via “new.”
45. Which of the following is the right MIME to use as a Content Type for JSON data?
Answers:
a. text/x-json
b. text/javascript
c. application/json
d. application/x-javascript
46.
What would be the output of the following code?
$arr = array(“foo”,
“bar”,
“baz”);
for ($i = 0; $i < count($arr); $i++) {
$item = $arr[$i];
}
echo ”
";“;
print_r($item);
echo "
?>
Answers:
a. Array ( [0] => foo [1] => bar [2] => baz )
b. foo
c. bar
d. baz
47. Which of the following is the correct way to check if a session has already been started?
Answers:
a. if ($_SERVER[“session_id”]) echo ‘session started’;
b. if (session_id()) echo ‘session started’;
c. if ($_SESSION[“session_id”]) echo ‘session started’;
d. if ($GLOBALS[“session_id”]) echo ‘session started’;
48. What is the correct PHP command to use to catch any error messages within the code?
Answers:
a. set_error(‘set_error’);
b. set_error_handler(‘error_handler’);
c. set_handler(‘set_handler’);
d. set_exception(‘set_exception’);
49.
What is wrong with the following code?
Answers:
a. There is nothing wrong with the code.
b. The cURL resource $ch has not been created using the curl_init() method.
c. The $ch variable needs to be initialized as $ch=null;.
d. The code will cause a parse error.
50. With what encoding does chr() work?
Answers:
a. ASCII
b. UTF-8
c. UTF-16
d. Implementation dependent
e. None of these
No comments:
Post a Comment