Friday, August 26, 2016

PHP Questions and Answers 2016

1. What is the best practice for running MySQL queries in PHP? Consider the risk of SQL injection.
Answers:
  1. Use mysql_query() and variables: for example: $input = $_POST[‘user_input’]; mysql_query(“INSERT INTO table (column) VALUES (‘” . $input . “‘)”);
  2. 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));
  3. 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. “‘)”);
  4. 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:
  1. mail($to,$subject,$body)
  2. sendmail($to,$subject,$body)
  3. mail(to,subject,body)
  4. sendmail(to,subject,body)
3. Which of the following is used to maintain the value of a variable over different pages?
Answers:
  1. static
  2. global
  3. session_register()
  4. None of these
4. Which of the following will check if a function exists?
Answers:
  1. function_exists()
  2. has_function()
  3. $a = “function to check”; if ($a ()) // then function exists
  4. None of these
5. Which of the following is not a file-related function in PHP?
Answers:
  1. fclose
  2. fopen
  3. fwrite
  4. fgets
  5. fappend
6.Which of the following is true about the singleton design pattern?
Answers:
  1. A singleton pattern means that a class will only have a single method.
  2. A singleton pattern means that a class can have only one instance object.
  3. A singleton pattern means that a class has only a single member variable.
  4. Singletons cannot be implemented in PHP.
7. Which of the following characters are taken care of by htmlspecialchars?
Answers:
  1. <
  2. >
  3. single quote
  4. double quote
  5. &
  6. All of these
8. Which of the following will read an object into an array variable?
Answers:
  1. $array_variable = get_object_vars($object);
  2. $array_variable = (array)$object;
  3. $array_variable = array $object;
  4. $array_variable = get_object_vars $object;
9. Which of the following variable declarations within a class is invalid in PHP?
Answers:
  1. private $type = ‘moderate’;
  2. internal $term = 3;
  3. public $amnt = ‘500’;
  4. protected $name = ‘Quantas Private Limited’;
10. Which of the following is not a PHP magic constant?
Answers:
  1. __FUNCTION__
  2. __TIME__
  3. __FILE__
  4. __NAMESPACE__
  5. __CLASS__
11. Which of the following will print out the PHP call stack?
Answers:
  1. $e = new Exception; var_dump($e->debug());
  2. $e = new Exception; var_dump($e->getTraceAsString());
  3. $e = new Exception; var_dump($e->backtrace());
  4. $e = new Exception; var_dump($e->getString());
12. What will be the output of the following code?
<?php
var_dump (3*4);
?>
Answers:
  1. int(3*4)
  2. int(12)
  3. 3*4
  4. 12
  5. None of the above
13. Which of the following is correct about Mysqli and PDO?
Answers:
  1. Mysqli provides the procedural way to access the database while PDO provides the object oriented way.
  2. Mysqli can only be used to access MySQL database while PDO can be used to access any DBMS.
  3. MySQLi prevents SQL Injection whereas PDO does not.
  4. 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:
  1. s.sendmail($EmailAddress, [$MessageBody], msg.as_string())
  2. sendmail($EmailAddress, “Subject”, $MessageBody);
  3. mail($EmailAddress, “Subject”, $MessageBody);
  4. <a href=”mailto:$EmailAddress”>$MessageBody</a>
15. Which of the following will start a session?
Answers:
  1. session(start);
  2. session();
  3. session_start();
  4. login_sesion();
16. For the following code:
<?php
function Expenses()
{
function Salary()
{
}
function Loan()
{
function Balance()
{
}
}
}
?>
Which of the following sequence will run successfully?
Answers:
  1. Expenses();Salary();Loan();Balance();
  2. Salary();Expenses();Loan();Balance();
  3. Expenses();Salary();Balance();Loan();
  4. Balance();Loan();Salary();Expenses();
17. What enctype is required for file uploads to work?
Answers:
  1. multipart/form-data
  2. multipart
  3. file
  4. application/octect-stream
  5. None of these
18. Which of the following is incorrect with respect to separating PHP code and HTML?
Answers:
  1. Use an MVC design pattern.
  2. As PHP is a scripting language, HTML and PHP cannot be separated.
  3. Use any PHP template engine e.g: smarty to keep the presentation separate from business logic.
  4. 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:
  1. crypt()
  2. md5()
  3. sha1()
  4. bcrypt()
20. What function should you use to join array elements with a glue string?
Answers:
  1. join_st
  2. implode
  3. connect
  4. make_array
  5. None of these
21. Which function can be used to delete a file?
Answers:
  1. delete()
  2. delete_file()
  3. unlink()
  4. fdelete()
  5. file_unlink()
22. What is the string concatenation operator in PHP?
Answers:
  1. +
  2. ||
  3. .
  4. |||
  5. None of these
23.Which of the following is useful for method overloading?
Answers:
  1. __call,__get,__set
  2. _get,_set,_load
  3. __get,__set,__load
  4. __overload
24. Which of the following will store order number (34) in an ‘OrderCookie’?
Answers:
  1. setcookie(“OrderCookie”,34);
  2. makeCookie(“OrderCookie”,34);
  3. Cookie(“OrderCookie”,34);
  4. OrderCookie(34);
25. What would occur if a fatal error was thrown in your PHP program?
Answers:
  1. The PHP program will stop executing at the point where the error occurred.
  2. The PHP program will show a warning message and program will continue executing.
  3. Since PHP is a scripting language so it does not have fatal error.
  4. 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:
  1. memory_limit = 128M
  2. limit_memory = 128M
  3. memory_limit: 128M
  4. limit_memory: 128M
27. What is the best way to change the key without changing the value of a PHP array element?
Answers:
  1. $arr[$newkey] = $oldkey; unset($arr[$oldkey]);
  2. $arr[$newkey] = $arr[$oldkey]; unset($arr[$oldkey]);
  3. $newkey = $arr[$oldkey]; unset($arr[$oldkey]);
  4. $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. 1
  2. 20
  3. 21
  4. 23
  5. 34
29. Does PHP 5 support exceptions?
Answers:
  1. Yes
  2. No
30.Which of the following is true about posting data using cURL in PHP?
Answers:
  1. Data can be posted using only the POST method.
  2. Data can be posted using only the GET method.
  3. Data can be posted using both GET and POST methods.
  4. Data cannot be posted using cURL.
31.Which of the following is the correct way to check if a session has already been started?
Answers:
  1. if (session_id()) echo ‘session started’;
  2. if ($_SESSION[«session_id»]) echo ‘session started’;
  3. if ($GLOBALS[«session_id»]) echo ‘session started’;
  4. if ($_SERVER[«session_id»]) echo ‘session started’;
32.Given the following array:
$array = array(0 => ‘blue’, 1 => ‘red’, 2 => ‘green’, 3 => ‘red’);
Which one of the following will print 2?
Answers:
  1. echo array_search(‘green’, $array);
  2. echo array_key_exists(2, $array);
  3. echo in_array(‘green’, $array);
  4. echo array_search(‘red’,$array);
33.What is the output of the following code?
echo 0x500;
?>
Answers:
  1. 500
  2. 0x500
  3. 0500
  4. 1280
  5. 320
34.Which of the following regular expressions can be used to check the validity of an e-mail address?
Answers:
  1. ^[^@ ]+@[^@ ]+\.[^@ ]+$
  2. ^[^@ ]+@[^@ ]+.[^@ ]+$
  3. $[^@ ]+@[^@ ]+\.[^@ ]+^
  4. $[^@ ]+@[^@ ]+.[^@ ]+^
35.Which of the following is not a valid DOM method in PHP?
Answers:
  1. loadXMLFile()
  2. loadHTML()
  3. loadXML()
  4. loadHTMLFile()
36.What is the fastest way to insert an item $item into the specified position $position of the array $array?
Answers:
  1. array_splice()
  2. array_merge() and array_slice()
  3. PHP does not have any built-in function that can do this: the source array will have to be copied, and $item inserted in to the required position:
    $n = 0;
    foreach ($array as $key => $val) {
    if ($n == $position) {
    $target[] = $item;
    }
    ++$n;
    $target[$key] = $val;
    }
  4. array_insert()
37.Consider the following 2D array in PHP:
$array = array(array(141,151,161), 2, 3, array(101, 202, 303));
Which of the following will display all values in the array?
Answers:
  1. function DisplayArray($array) {
    foreach ($array as $value) {
    if (array_valid($value)) {
    DisplayArray($value);
    } else {
    echo $value. “ ”;
    }
    }
    }
    DisplayArray($array);
  2. function DisplayArray($array) {
    for ($array as $value) {
    if (valid_array($value)) {
    DisplayArray($value);
    } else {
    echo $value. “ ”;
    }
    }
    }
    DisplayArray($array);
  3. function DisplayArray($array) {
    for ($array as $value) {
    if (is_array($value)) {
    DisplayArray($value);
    } else {
    echo $value. “ ”;
    }
    }
    }
    DisplayArray($array);
  4. function DisplayArray($array) {
    foreach ($array as $value) {
    if (is_array($value)) {
    DisplayArray($value);
    } else {
    echo $value “ ”;
    }
    }
    }
    DisplayArray($array);
38. Why is it not recommended to use $_REQUEST when handling form submissions in PHP?
Answers:
  1. It’s difficult to determine whether it is a $_POST or $_GET request.
  2. $_REQUEST is deprecated
  3. $_REQUEST includes $_COOKIE by default, and parameters from $_COOKIE with the same name may be overriden with parameters from $_GET or $_POST.
  4. $_REQUEST does not handle HTTP rquests, it handles database requests.
39.How should a variable be declared in a function, if the value has to be retained over multiple calls?
Answers:
  1. local
  2. global
  3. static
  4. None of these
40.Which is the best way to automatically deploy a PHP website using git push?
Answers:
  1. . It is not possible.
  2. You should have two copies on your server. A bare copy, that you can push/pull from, to which you would push your changes to when you are done. Then you would clone this into your web directory and set up a cronjob to update git pull from your web directory every day or couple of days.
  3. Developing from scratch a custom deployment script to manage all the aspects.
  4. Copy over your git directory to your web server. On your local copy, modify your .git/config file and add your web server as a remote. On the server, replace .git/hooks/post-update with an existing script to process the rest of the workflow.
    Make the script executable.
41.With what encoding does chr () work?
Answers:
  1. ASCII
  2. UTF-8
  3. UTF-16
  4. Implementation dependent
  5. None of the above
42.Which of the following file modes is used to write into a file at the end of the existing content, and create the file if the file does not exist?
Answers:
  1. r+
  2. w+
  3. a()
  4. x
43.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:
  1. Adding a member variable that gets incremented in the default constructor and decremented in the destructor.
  2. This cannot be accomplished since the creation of objects is being done dynamically via «new.»
  3. Add a static member variable that gets incremented in each constructor and decremented in the destructor.
  4. Adding a local variable that gets incremented in each constructor and decremented in the destructor.
44.What is the output of the following code?

<?php
function abc()
{
return __FUNCTION__;
}
function xyz()
{
return abc();
}
echo xyz();
?>
Answers:
  1. abc
  2. __FUNCTION__
  3. xyz
45.Which of the following is the operator with the highest precedence?
Answers:
  1. +
  2. instanceof
  3. new
  4. =
  5. none of above
46.Which function is used to remove the first element of an array?
Answers:
  1. array_remove_first_element
  2. array_shift
  3. array_ltrim
  4. a[0] = nil
  5. None of these
47.Which of the following functions output text?
Note: There may be more than one right answer.
Answers:
  1. echo()
  2. print()
  3. println()
  4. display()
48. What is the best way to load a file that contains necessary functions and classes?
Answers:
  1. include($filename);
  2. require($filename);
  3. include_once($filename);
  4. require_once($filename);
49.With regard to abstract classes, which of the following statements is false?
Answers:
  1. Abstract classes are not available in PHP.
  2. A class with a single abstract method must be declared abstract.
  3. An abstract class can contain non-abstract methods.
  4. An abstract method must have a method definition and can have optional empty braces following it.
50. What is the output of the following code?
<?php
function
vec_add (&amp;$a, $b)
{
$a[‘x’] += $b[‘x’];
$a[‘y’] += $b[‘y’];
$a[‘z’] += $b[‘z’];
}
$a = array (x =&gt; 3, y =&gt; 2, z =&gt; 5);
$b = array (x =&gt; 9, y =&gt; 3, z =&gt; -7);
vec_add (&amp;$a, $b);
print_r ($a);
?>
Answers:
  1. Array
    (
    [x] => 9
    [y] => 3
    [z] => -7
    )
  2. Array
    (
    [x] => 3
    [y] => 2
    [z] => 5
    )
  3. Array
    (
    [x] => 12
    [y] => 5
    [z] => -2
    )
  4. Error
  5. None of these
51.Which of the following are not considered as Boolean false?
Answers:
  1. FALSE
  2. 0
  3. «0»
  4. «FALSE»
  5. 4
  6. -4
  7. null
52.Which of the following code snippets has the most appropriate headers to force the browser to download a CSV file?
Answers:
  1. header(«Content-type: text/csv»);
    header(«Content-Disposition: attachment; filename=file.csv»);
    header(«Pragma: no-cache»);
    header(«Expires: 0»);
  2. header(‘Content-Type: application/download’);
    header(«Content-Disposition: attachment; filename=file.csv»);
    header(«Pragma: no-cache»);
    header(«Expires: 0»);
  3. header(‘Content-Type: application/csv’);
    header(«Content-Disposition: attachment; filename=file.csv»);
    header(«Pragma: no-cache»);
    header(«Expires: 0»);
  4. header(‘Content-Type: application/octet-stream’);
    header(«Content-Disposition: attachment; filename=file.csv»);
    header(«Pragma: no-cache»);
    header(«Expires: 0»);
53. What is the output of the following code?
<?php
echo «<pre>»;
$array = array(«red»,»green»,»blue»);
$last_key = end(array_keys($array));
foreach ($array as $key => $value) {
if ($key == $last_key) {
echo «a<br>»;
} else {
echo «b<br>»;
}
}
?>
Answers:
  1. b
    a
    b
  2. b
    a
    a
  3. b
    a
    a
  4. b
    b
    b
54.With regards to the «static» keyword in PHP, which of the following statements is false?
Answers:
  1. The $this variable can be used inside any static method.
  2. Static properties may only be initialized using a literal or a constant.
  3. A property declared as static can not be accessed with an instantiated class object.
  4. A static variable or method can be accessed without requiring instantiation of the class.
55. What is «empty()»?
Answers:
  1. A function
  2. A language construct
  3. A variable
  4. A reference
  5. None of these
56.Which of the following is not a valid cURL parameter in PHP?
Answers:
  1. CURLOPT_RETURNTRANSFER
  2. CURLOPT_GET
  3. CURLOPT_POST
  4. CURLOPT_POSTFIELDS
57.What will be the output of the following code?
<?
$a = 0;
echo ~$a;
?>
Answers:
  1. -1
  2. 0
  3. 1
  4. 10
  5. Syntax error
58.When comparing two arrays, what is the difference between «==» and «===»?
Answers:
  1. «==» compares keys while «===» compares keys and values.
  2. «===» also compares the order and types of the objects.
  3. «===» compares the array references.
  4. «===» They are identical.
  5. None of these
59.Which of the the following are PHP file upload-related functions?
Answers:
  1. upload_file()
  2. is_uploaded_file()
  3. move_uploaded_file()
  4. None of these
60.Which of the following is not a valid API?
Answers:
  1. trigger_print_error()
  2. trigger_error()
  3. debug_backtrace()
  4. debug_print_backtrace()
61.What will be the output of the following code?
<?php echo 30 * 5 . 7; ?>
Answers:
  1. 150.7
  2. 1507
  3. Integers can’t be concatenated.
  4. An error will be thrown.
62.Which of these is not a valid SimpleXML Parser method?
Answers:
  1. simplexml_import_dom()
  2. simplexml_import_sax()
  3. simplexml_load_string()
63.Which of the following environment variables is used to fetch the IP address of the user in a PHP application?
Answers:
  1. $IP_ADDR
  2. $REMOTE_ADDR_USER
  3. $REMOTE_ADDR
  4. $IP_ADDR_USER
64.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:
  1. Line 8 and 9
  2. Line 10
  3. Line 9 and 10
  4. All the three lines 8,9, and 10 should be left as it is.
65.What is the correct syntax of mail() function in PHP?
Answers:
  1. mail($from,$to,$subject,$message)
  2. mail($to,$subject,$message,$headers)
  3. mail($from,$to,$subject,$message)
  4. mail($to,$from,$subject,$message)
  5. mail($to,$from,$message,$headers).
66.Given the following array:
$array = array(0 => ‘blue’, 1 => ‘red’, 2 => ‘green’, 3 => ‘red’);
Which one of the following will print 2?
Answers:
  1. echo array_search(‘green’, $array);
  2. echo in_array(‘green’, $array);
  3. echo array_key_exists(2, $array);
  4. echo array_search(‘red’,$array);
67.Which function will suitably replace ‘X’ if the size of a file needs to be checked?
$size=X(filename);
Answers:
  1. size
  2. filesize
  3. sizeofFile
  4. getSize
68. Which of the following will not give the correct date and time in PHP?
Answers:
  1. date(“Y-m-d H:i:s”)
  2. date(“y-m-d H:i:s”)
  3. date(“f, j Y H:i:s”)
  4. date(“F, j Y H:i:s”)
69.Which of the following functions is not used in debugging?
Answers:
  1. var_dump()
  2. fprintf()
  3. print_r()
  4. var_export()
70.What is the difference between die() and exit() in PHP?
Answers:
  1. die() is an alias for exit().
  2. exit() is a function, die() is a language construct and cannot be called using variable functions.
  3. 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.
  4. die() terminates the script immediately, exit() calls shutdown functions and object destructors first.
71.Should assert() be used to check user input?
Answers:
  1. Yes
  2. No
72.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:
  1. Adding a member variable that gets incremented in the default constructor and decremented in the destructor.
  2. Adding a local variable that gets incremented in each constructor and decremented in the destructor.
  3. Add a static member variable that gets incremented in each constructor and decremented in the destructor.
  4. This cannot be accomplished since the creation of objects is being done dynamically via “new.”
73.Which of the following is the right MIME to use as a Content Type for JSON data?
Answers:
  1. text/x-json
  2. text/javascript
  3. application/json
  4. application/x-javascript
74.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:
  1. Array ( [0] => foo [1] => bar [2] => baz )
  2. foot
  3. bar
  4. baz
75.Which of the following is the correct way to check if a session has already been started?
Answers:
  1. if ($_SERVER[“session_id”]) echo ‘session started’;
  2. if (session_id()) echo ‘session started’;
  3. if ($_SESSION[“session_id”]) echo ‘session started’;
  4. if ($GLOBALS[“session_id”]) echo ‘session started’;
76.What is the correct PHP command to use to catch any error messages within the code?
Answers:
  1. set_error(‘set_error’);
  2. set_error_handler(‘error_handler’);
  3. set_handler(‘set_handler’);
  4. set_exception(‘set_exception’);
77.What is the correct PHP command to use to catch any error messages within the code?
Answers:
  1. set_error(‘set_error’);
  2. set_error_handler(‘error_handler’);
  3. set_handler(‘set_handler’);
  4. set_exception(‘set_exception’);
78.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:
  1. There is nothing wrong with the code.
  2. The cURL resource $ch has not been created using the curl_init() method.
  3. The $ch variable needs to be initialized as $ch=null;.
  4. The code will cause a parse error.
79.With what encoding does chr() work?
Answers:
  1. ASCII
  2. UTF-8
  3. UTF-16
  4. Implementation dependent
  5. None of these
80.Which of the following is not related to debugging in PHP?
Answers:
  1. PDO
  2. watch
  3. breakpoints
  4. call stack
81.Which of the following is not a predefined constant?
Answers:
  1. TRUE
  2. __FILE__
  3. FALSE
  4. NULL
  5. CONSTANT
82. 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:
  1. Syntax error
  2. 1
  3. FALSE
  4. 0
83. With the following code, will the word Hello be printed?
<?
//?>Hello
Answers:
  1. Yes
  2. No
84.Which of the following features are supported in PHP5?
Note: There may be more than one right answer.
Answers:
  1. Multiple Inheritance
  2. Embedded Database with SQLite
  3. Exceptions and Iterators
  4. Interoperable XML Tools
85.Consider the following statements:
I : while (expr) statement
II : while (expr): statement … endwhile;
Answers:
  1. I is correct and II is wrong.
  2. I is wrong and II is correct.
  3. Both I and II are wrong.
  4. Both I and II are wrong.
86.Which of the following type cast is not correct?
<?php
$fig = 23;
$varb1 = (real) $fig;
$varb2 = (double) $fig;
$varb3 = (decimal) $fig;
$varb4 = (bool) $fig;
?>
Answers:
  1. real
  2. double
  3. decimal
  4. boolean
87.Which of the following printing construct/function accepts multiple parameters?
Note: There may be more than one right answer.
Answers:
  1. echo
  2. print
  3. printf
  4. All of these
88.Which of the following is false about cURL?
Answers:
  1. cURL can be used to send plain text data to a remote server.
  2. cURL can be used to send both text as well as files using a single request.
  3. cURL can be used to send files to a remote server.
  4. Files cannot be sent using cURL.

No comments:

Post a Comment