1. 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. “‘)”);
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)
- sendmail($to,$subject,$body)
- mail(to,subject,body)
- sendmail(to,subject,body)
3. Which of the following is used to maintain the value of a variable over different pages?
Answers:
- static
- global
- session_register()
- None of these
4. 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
5. Which of the following is not a file-related function in PHP?
Answers:
- fclose
- fopen
- fwrite
- fgets
- fappend
6.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.
7. Which of the following characters are taken care of by htmlspecialchars?
Answers:
- <
- >
- single quote
- double quote
- &
- All of these
8. 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;
9. 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’;
10. Which of the following is not a PHP magic constant?
Answers:
- __FUNCTION__
- __TIME__
- __FILE__
- __NAMESPACE__
- __CLASS__
11. 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());
12. What will be the output of the following code?
<?php
var_dump (3*4);
?>
<?php
var_dump (3*4);
?>
Answers:
- int(3*4)
- int(12)
- 3*4
- 12
- None of the above
13. 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.
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 will start a session?
Answers:
- session(start);
- session();
- session_start();
- login_sesion();
16. For the following code:
<?php
<?php
function Expenses()
{
function Salary()
{
}
{
function Salary()
{
}
function Loan()
{
function Balance()
{
}
}
}
{
function Balance()
{
}
}
}
?>
Which of the following sequence will run successfully?
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();
17. What enctype is required for file uploads to work?
Answers:
- multipart/form-data
- multipart
- file
- application/octect-stream
- None of these
18. 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.
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;
?>
<?
echo 5 * 6 / 2 + 2 * 3;
?>
Answers:
- 1
- 20
- 21
- 23
- 34
29. Does PHP 5 support exceptions?
Answers:
- Yes
- No
30.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.
31.Which of the following is the correct way to check if a session has already been started?
Answers:
- if (session_id()) echo ‘session started’;
- if ($_SESSION[«session_id»]) echo ‘session started’;
- if ($GLOBALS[«session_id»]) echo ‘session started’;
- if ($_SERVER[«session_id»]) echo ‘session started’;
32.Given the following array:
$array = array(0 => ‘blue’, 1 => ‘red’, 2 => ‘green’, 3 => ‘red’);
$array = array(0 => ‘blue’, 1 => ‘red’, 2 => ‘green’, 3 => ‘red’);
Which one of the following will print 2?
Answers:
- echo array_search(‘green’, $array);
- echo array_key_exists(2, $array);
- echo in_array(‘green’, $array);
- echo array_search(‘red’,$array);
33.What is the output of the following code?
echo 0x500;
?>
echo 0x500;
?>
Answers:
- 500
- 0x500
- 0500
- 1280
- 320
34.Which of the following regular expressions can be used to check the validity of an e-mail address?
Answers:
- ^[^@ ]+@[^@ ]+\.[^@ ]+$
- ^[^@ ]+@[^@ ]+.[^@ ]+$
- $[^@ ]+@[^@ ]+\.[^@ ]+^
- $[^@ ]+@[^@ ]+.[^@ ]+^
35.Which of the following is not a valid DOM method in PHP?
Answers:
- loadXMLFile()
- loadHTML()
- loadXML()
- loadHTMLFile()
36.What is the fastest way to insert an item $item into the specified position $position of the array $array?
Answers:
- array_splice()
- array_merge() and array_slice()
- 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;
} - 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?
$array = array(array(141,151,161), 2, 3, array(101, 202, 303));
Which of the following will display all values in the array?
Answers:
- function DisplayArray($array) {
foreach ($array as $value) {
if (array_valid($value)) {
DisplayArray($value);
} else {
echo $value. “ ”;
}
}
}
DisplayArray($array); - function DisplayArray($array) {
for ($array as $value) {
if (valid_array($value)) {
DisplayArray($value);
} else {
echo $value. “ ”;
}
}
}
DisplayArray($array); - function DisplayArray($array) {
for ($array as $value) {
if (is_array($value)) {
DisplayArray($value);
} else {
echo $value. “ ”;
}
}
}
DisplayArray($array); - 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:
- It’s difficult to determine whether it is a $_POST or $_GET request.
- $_REQUEST is deprecated
- $_REQUEST includes $_COOKIE by default, and parameters from $_COOKIE with the same name may be overriden with parameters from $_GET or $_POST.
- $_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:
- local
- global
- static
- None of these
40.Which is the best way to automatically deploy a PHP website using git push?
Answers:
- . It is not possible.
- 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.
- Developing from scratch a custom deployment script to manage all the aspects.
- 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:
- ASCII
- UTF-8
- UTF-16
- Implementation dependent
- 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:
- r+
- w+
- a()
- 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:
- Adding a member variable that gets incremented in the default constructor and decremented in the destructor.
- This cannot be accomplished since the creation of objects is being done dynamically via «new.»
- Add a static member variable that gets incremented in each constructor and decremented in the destructor.
- 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:
<?php
function abc()
{
return __FUNCTION__;
}
function xyz()
{
return abc();
}
echo xyz();
?>
Answers:
- abc
- __FUNCTION__
- xyz
45.Which of the following is the operator with the highest precedence?
Answers:
- +
- instanceof
- new
- =
- none of above
46.Which function is used to remove the first element of an array?
Answers:
- array_remove_first_element
- array_shift
- array_ltrim
- a[0] = nil
- None of these
47.Which of the following functions output text?
Note: There may be more than one right answer.
Note: There may be more than one right answer.
Answers:
- echo()
- print()
- println()
- display()
48. What is the best way to load a file that contains necessary functions and classes?
Answers:
- include($filename);
- require($filename);
- include_once($filename);
- require_once($filename);
49.With regard to abstract classes, which of the following statements is false?
Answers:
- Abstract classes are not available in PHP.
- A class with a single abstract method must be declared abstract.
- An abstract class can contain non-abstract methods.
- 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 (&$a, $b)
{
$a[‘x’] += $b[‘x’];
$a[‘y’] += $b[‘y’];
$a[‘z’] += $b[‘z’];
}
$a = array (x => 3, y => 2, z => 5);
$b = array (x => 9, y => 3, z => -7);
vec_add (&$a, $b);
print_r ($a);
?>
Answers:
<?php
function
vec_add (&$a, $b)
{
$a[‘x’] += $b[‘x’];
$a[‘y’] += $b[‘y’];
$a[‘z’] += $b[‘z’];
}
$a = array (x => 3, y => 2, z => 5);
$b = array (x => 9, y => 3, z => -7);
vec_add (&$a, $b);
print_r ($a);
?>
Answers:
- Array
(
[x] => 9
[y] => 3
[z] => -7
) - Array
(
[x] => 3
[y] => 2
[z] => 5
) - Array
(
[x] => 12
[y] => 5
[z] => -2
) - Error
- None of these
51.Which of the following are not considered as Boolean false?
Answers:
- FALSE
- 0
- «0»
- «FALSE»
- 4
- -4
- null
52.Which of the following code snippets has the most appropriate headers to force the browser to download a CSV file?
Answers:
- header(«Content-type: text/csv»);
header(«Content-Disposition: attachment; filename=file.csv»);
header(«Pragma: no-cache»);
header(«Expires: 0»); - header(‘Content-Type: application/download’);
header(«Content-Disposition: attachment; filename=file.csv»);
header(«Pragma: no-cache»);
header(«Expires: 0»); - header(‘Content-Type: application/csv’);
header(«Content-Disposition: attachment; filename=file.csv»);
header(«Pragma: no-cache»);
header(«Expires: 0»); - 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:
<?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:
- b
a
b - b
a
a - b
a
a - b
b
b
54.With regards to the «static» keyword in PHP, which of the following statements is false?
Answers:
- The $this variable can be used inside any static method.
- Static properties may only be initialized using a literal or a constant.
- A property declared as static can not be accessed with an instantiated class object.
- A static variable or method can be accessed without requiring instantiation of the class.
55. What is «empty()»?
Answers:
- A function
- A language construct
- A variable
- A reference
- None of these
56.Which of the following is not a valid cURL parameter in PHP?
Answers:
- CURLOPT_RETURNTRANSFER
- CURLOPT_GET
- CURLOPT_POST
- CURLOPT_POSTFIELDS
57.What will be the output of the following code?
<?
$a = 0;
echo ~$a;
?>
Answers:
<?
$a = 0;
echo ~$a;
?>
Answers:
- -1
- 0
- 1
- 10
- Syntax error
58.When comparing two arrays, what is the difference between «==» and «===»?
Answers:
- «==» compares keys while «===» compares keys and values.
- «===» also compares the order and types of the objects.
- «===» compares the array references.
- «===» They are identical.
- None of these
59.Which of the the following are PHP file upload-related functions?
Answers:
- upload_file()
- is_uploaded_file()
- move_uploaded_file()
- None of these
60.Which of the following is not a valid API?
Answers:
- trigger_print_error()
- trigger_error()
- debug_backtrace()
- debug_print_backtrace()
61.What will be the output of the following code?
<?php echo 30 * 5 . 7; ?>
Answers:
- 150.7
- 1507
- Integers can’t be concatenated.
- An error will be thrown.
62.Which of these is not a valid SimpleXML Parser method?
Answers:
- simplexml_import_dom()
- simplexml_import_sax()
- 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:
- $IP_ADDR
- $REMOTE_ADDR_USER
- $REMOTE_ADDR
- $IP_ADDR_USER
64.Consider the following class:
- class Insurance
- {
- function clsName()
- {
- echo get_class($this);
- }
- }
- $cl = new Insurance();
- $cl->clsName();
- 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.
65.What is the correct syntax of mail() function in PHP?
Answers:
- mail($from,$to,$subject,$message)
- mail($to,$subject,$message,$headers)
- mail($from,$to,$subject,$message)
- mail($to,$from,$subject,$message)
- 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:
$array = array(0 => ‘blue’, 1 => ‘red’, 2 => ‘green’, 3 => ‘red’);
Which one of the following will print 2?
Answers:
- echo array_search(‘green’, $array);
- echo in_array(‘green’, $array);
- echo array_key_exists(2, $array);
- 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);
$size=X(filename);
Answers:
- size
- filesize
- sizeofFile
- getSize
68. Which of the following will not give the correct date and time in PHP?
Answers:
- date(“Y-m-d H:i:s”)
- date(“y-m-d H:i:s”)
- date(“f, j Y H:i:s”)
- date(“F, j Y H:i:s”)
69.Which of the following functions is not used in debugging?
Answers:
- var_dump()
- fprintf()
- print_r()
- var_export()
70.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.
71.Should assert() be used to check user input?
Answers:
- Yes
- 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:
- 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.”
73.Which of the following is the right MIME to use as a Content Type for JSON data?
Answers:
- text/x-json
- text/javascript
- application/json
- 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:
<?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 )
- foot
- bar
- baz
75.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’;
76.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’);
77.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’);
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:
<?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.
79.With what encoding does chr() work?
Answers:
- ASCII
- UTF-8
- UTF-16
- Implementation dependent
- None of these
80.Which of the following is not related to debugging in PHP?
Answers:
- PDO
- watch
- breakpoints
- call stack
81.Which of the following is not a predefined constant?
Answers:
- TRUE
- __FILE__
- FALSE
- NULL
- 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:
<?php
$str=»Hello»;
$test=»lo»;
echo substr_compare($str, $test, -strlen($test), strlen($test)) === 0;
?>
Answers:
- Syntax error
- 1
- FALSE
- 0
83. With the following code, will the word Hello be printed?
<?
//?>Hello
Answers:
<?
//?>Hello
Answers:
- Yes
- No
84.Which of the following features are supported in PHP5?
Note: There may be more than one right answer.
Note: There may be more than one right answer.
Answers:
- Multiple Inheritance
- Embedded Database with SQLite
- Exceptions and Iterators
- Interoperable XML Tools
85.Consider the following statements:
I : while (expr) statement
II : while (expr): statement … endwhile;
I : while (expr) statement
II : while (expr): statement … endwhile;
Answers:
- I is correct and II is wrong.
- I is wrong and II is correct.
- Both I and II are wrong.
- 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:
<?php
$fig = 23;
$varb1 = (real) $fig;
$varb2 = (double) $fig;
$varb3 = (decimal) $fig;
$varb4 = (bool) $fig;
?>
Answers:
- real
- double
- decimal
- boolean
87.Which of the following printing construct/function accepts multiple parameters?
Note: There may be more than one right answer.
Note: There may be more than one right answer.
Answers:
- echo
- printf
- All of these
88.Which of the following is false about cURL?
Answers:
- cURL can be used to send plain text data to a remote server.
- cURL can be used to send both text as well as files using a single request.
- cURL can be used to send files to a remote server.
- Files cannot be sent using cURL.
No comments:
Post a Comment