Upwork PHP Test Question With Answers
1. What is the best practice for running MySQL queries in PHP? Consider the risk of SQL injection.
Answers:
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));
2. Which of the following methods should be used for sending an email using the variables $to, $subject, and $body?
Answers:
mail($to,$subject,$body)
3. Which of the following is used to maintain the value of a variable over different pages?
Answers:
session_register()
4. Which of the following will check if a function exists?
Answers:
function_exists()
5. Which of the following is not a file-related function in PHP?
Answers:
fappend
6. Which of the following is true about the singleton design pattern?
Answers:
A singleton pattern means that a class can have only one instance object.
7. Which of the following characters are taken care of by htmlspecialchars?
Answers:
All of these
8. Which of the following will read an object into an array variable?
Answers:
$array_variable = get_object_vars($object);
9. Which of the following variable declarations within a class is invalid in PHP?
Answers:
internal $term = 3;
10. Which of the following is not a PHP magic constant?
Answers:
__TIME__
11. Which of the following will print out the PHP call stack?
Answers:
$e = new Exception; var_dump($e->getTraceAsString());
12. What will be the output of the following code?
<?php
var_dump (3*4);
?>
Answers:
int(12)
13. Which of the following is correct about Mysqli and PDO?
Answers:
Mysqli can only be used to access MySQL database while PDO can be used to access any DBMS.
14. What is the correct way to send a SMTP (Simple Mail Transfer Protocol) email using PHP?
Answers:
mail($EmailAddress, “Subject”, $MessageBody);
15. Which of the following will start a session?
Answers:
session_start();
16. 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();
17. What enctype is required for file uploads to work?
Answers:
multipart/form-data
18. Which of the following is incorrect with respect to separating PHP code and HTML?
Answers:
As PHP is a scripting language, HTML and PHP cannot be separated.
19. Which one of the following is not an encryption method in PHP?
Answers:
bcrypt()
20. What function should you use to join array elements with a glue string?
Answers:
implode
21. Which function can be used to delete a file?
Answers:
fdelete()
22. What is the string concatenation operator in PHP?
Answers:
.
23. Which of the following is useful for method overloading?
Answers:
__call,__get,__set
24. Which of the following will store order number (34) in an ‘OrderCookie’?
Answers:
setcookie(“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.
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
27. What is the best way to change the key without changing the value of a PHP array element?
Answers:
$arr[$newkey] = $arr[$oldkey]; unset($arr[$oldkey]);
28. What will be the output of the following code?
<?
echo 5 * 6 / 2 + 2 * 3;
?>
Answers:
21
29. Does PHP 5 support exceptions?
Answers:
Yes
Answers:
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));
2. Which of the following methods should be used for sending an email using the variables $to, $subject, and $body?
Answers:
mail($to,$subject,$body)
3. Which of the following is used to maintain the value of a variable over different pages?
Answers:
session_register()
4. Which of the following will check if a function exists?
Answers:
function_exists()
5. Which of the following is not a file-related function in PHP?
Answers:
fappend
6. Which of the following is true about the singleton design pattern?
Answers:
A singleton pattern means that a class can have only one instance object.
7. Which of the following characters are taken care of by htmlspecialchars?
Answers:
All of these
8. Which of the following will read an object into an array variable?
Answers:
$array_variable = get_object_vars($object);
9. Which of the following variable declarations within a class is invalid in PHP?
Answers:
internal $term = 3;
10. Which of the following is not a PHP magic constant?
Answers:
__TIME__
11. Which of the following will print out the PHP call stack?
Answers:
$e = new Exception; var_dump($e->getTraceAsString());
12. What will be the output of the following code?
<?php
var_dump (3*4);
?>
Answers:
int(12)
13. Which of the following is correct about Mysqli and PDO?
Answers:
Mysqli can only be used to access MySQL database while PDO can be used to access any DBMS.
14. What is the correct way to send a SMTP (Simple Mail Transfer Protocol) email using PHP?
Answers:
mail($EmailAddress, “Subject”, $MessageBody);
15. Which of the following will start a session?
Answers:
session_start();
16. 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();
17. What enctype is required for file uploads to work?
Answers:
multipart/form-data
18. Which of the following is incorrect with respect to separating PHP code and HTML?
Answers:
As PHP is a scripting language, HTML and PHP cannot be separated.
19. Which one of the following is not an encryption method in PHP?
Answers:
bcrypt()
20. What function should you use to join array elements with a glue string?
Answers:
implode
21. Which function can be used to delete a file?
Answers:
fdelete()
22. What is the string concatenation operator in PHP?
Answers:
.
23. Which of the following is useful for method overloading?
Answers:
__call,__get,__set
24. Which of the following will store order number (34) in an ‘OrderCookie’?
Answers:
setcookie(“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.
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
27. What is the best way to change the key without changing the value of a PHP array element?
Answers:
$arr[$newkey] = $arr[$oldkey]; unset($arr[$oldkey]);
28. What will be the output of the following code?
<?
echo 5 * 6 / 2 + 2 * 3;
?>
Answers:
21
29. Does PHP 5 support exceptions?
Answers:
Yes
