Advertisement
Advertisement
i) Notepad
ii) Notepad++
iv) PDT
advertisement
a) Only iv)
b) i), ii), iii) and iv)
c) i), ii) and iii)
d) Only iii)
View Answer
Answer: b
Explanation: Any of the above editors can be used to type php code and run it.
5. Which of the following must be installed on your computer so as to run PHP script?
i) Adobe Dreamweaver
ii) XAMPP
iv) IIS
i) /?
ii) //
iii) #
iv) /* */
a) Only ii)
b) i), iii) and iv)
c) ii), iii) and iv)
d) Both ii) and iv)
View Answer
Answer: c
Explanation: /* */ can also be use to comment just a single line although it is used for
paragraphs. // and # are used only for single line comment.
8. Which of the following PHP statement/statements will store 111 in variable num?
1. <?php
2. $num = 1;
3. $num1 = 2;
4. print $num . "+". $num1;
5. ?>
a) 3
b) 1+2
c) 1.+.2
d) Error
View Answer
Answer: b
Explanation: .(dot) is used to combine two parts of the statement. Example ($num .
“Hello World”) will output 1Hello World.
10. What will be the output of the following PHP code?
1. <?php
2. $num = "1";
3. $num1 = "2";
4. print $num+$num1;
5. ?>
a) 3
b) 1+2
c) Error
d) 12
View Answer
Answer: a
Explanation: The numbers inside the double quotes are considered as integers and not
string, therefore the value 3 is printed and not 1+2.
1. Which is the right way of declaring a variable in PHP?
i) $3hello
ii) $_hello
iii) $this
iv) $This
a) Only ii)
b) Only iii)
c) ii), iii) and iv)
d) ii) and iv)
View Answer
Answer: d
Explanation: A variable in PHP can not start with a number, also $this is mainly used to
refer properties of a class so we can’t use $this as a user define variable name.
2. What will be the output of the following PHP code?
1. <?php
2. $foo = 'Bob';
3. $bar = &$foo;
4. $bar = "My name is $bar";
5. echo $bar;
6. echo $foo;
7. ?>
a) Error
b) My name is BobBob
c) My name is BobMy name is Bob
d) My name is Bob Bob
View Answer
Answer: c
Explanation: Firstly, the line $bar = &$foo; will reference $foo via $bar. So $bar is
assigned value Bob. Therefore $bar = “My name is $bar”; will print My name is
Bob($bar=Bob as said before).
3. Which of the following PHP statements will output Hello World on the screen?
advertisement
i) echo ("Hello World");
ii) print ("Hello World");
iii) printf ("Hello World");
iv) sprintf ("Hello World");
a) i) and ii)
b) i), ii) and iii)
c) i), ii), iii) and iv)
d) i), ii) and iv)
View Answer
Answer: b
Explanation: echo(), print() and printf() all three can be used to output a statement onto
the screen. The sprintf() statement is functionally identical to printf() except that the
output is assigned to a string rather than rendered to the browser.
4. What will be the output of the following PHP code?
1. <?php
2. $color = "maroon";
3. $var = $color[2];
4. echo "$var";
5. ?>
a) a
b) Error
c) $var
d) r
View Answer
Answer: d
Explanation: PHP treats strings in the same fashion as arrays, allowing for specific
characters to be accessed via array offset notation. In an array, index always starts from
0. So in the line $var = $color[2]; if we count from start ‘r’ comes at index 2. So the
output will be r.
5. What will be the output of the following PHP code?
1. <?php
2. $score = 1234;
3. $scoreboard = (array) $score;
4. echo $scoreboard[0];
5. ?>
a) 1
b) Error
c) 1234
d) 2
View Answer
Answer: c
Explanation: The (array) is a cast operator which is used for converting values from
other data types to array.
6. What will be the output of the following PHP code?
1. <?php
2. $total = "25 students";
3. $more = 10;
4. $total = $total + $more;
5. echo "$total";
6. ?>
a) Error
b) 35 students
c) 35
d) 25 students
View Answer
Answer: c
Explanation: The integer value at the beginning of the original $total string is used in the
calculation. However if it begins with anything but a numerical value, the value will be 0.
7. Which of the below statements is equivalent to $add += $add?
a) $add = $add
b) $add = $add +$add
c) $add = $add + 1
d) $add = $add + $add + 1
View Answer
Answer: b
Explanation: a += b is an addition assignment whose outcome is a = a + b. Same can be
done with subtraction, multiplication, division etc.
8. Which statement will output $x on the screen?
a) echo “\$x”;
b) echo “$$x”;
c) echo “/$x”;
d) echo “$x;”;
View Answer
Answer: a
Explanation: A backslash is used so that the dollar sign is treated as a normal string
character rather than prompt PHP to treat $x as a variable. The backslash used in this
manner is known as escape character.
9. What will be the output of the following PHP code?
1. <?php
2. function track() {
3. static $count = 0;
4. $count++;
5. echo $count;
6. }
7. track();
8. track();
9. track();
10. ?>
a) 123
b) 111
c) 000
d) 011
View Answer
Answer: a
Explanation: Because $count is static, it retains its previous value each time the function
is executed.
10. What will be the output of the following PHP code?
1. <?php
2. $a = "clue";
3. $a .= "get";
4. echo "$a";
5. ?>
a) get
b) true
c) false
d) clueget
View Answer
Answer: d
Explanation: ‘.’ is a concatenation operator. $a. = “get” is same as $a=$a.”get” where $a
is having value of “clue” in the previous statement. So the output will be clueget.
1. What will be the output of the following PHP code?
1. <?php
2. $a = 5;
3. $b = 5;
4. echo ($a === $b);
5. ?>
a) 5 === 5
b) Error
c) 1
d) False
View Answer
Answer: c
Explanation: === operator returns 1 if $a and $b are equivalent and $a and $b have the
same type.
2. Which of the below symbols is a newline character?
a) \r
b) \n
c) /n
d) /r
View Answer
Answer: b
Explanation: PHP treats \n as a newline character.
3. What will be the output of the following PHP code?
advertisement
1. <?php
2. $num = 10;
3. echo 'What is her age? \n She is $num years old';
4. ?>
View Answer
Answer: a
Explanation: When a string is enclosed within single quotes both variables and escape
sequences will not be interpreted when the string is parsed.
i) if statements
a) Only i)
b) i), ii) and iv)
c) ii), iii) and iv)
d) i), ii), iii) and iv)
View Answer
Answer: d
Explanation: All are conditional statements supported by PHP as all are used to evaluate
different conditions during a program and take decisions based on whether these
conditions evaluate to true of false.
5. What will be the output of the following PHP code?
1. <?php
2. $team = "arsenal";
3. switch ($team) {
4. case "manu":
5. echo "I love man u";
6. case "arsenal":
7. echo "I love arsenal";
8. case "manc":
9. echo "I love manc"; }
10. ?>
a) I love arsenal
b) Error
c) I love arsenalI love manc
d) I love arsenalI love mancI love manu
View Answer
Answer: c
Explanation: If a break statement isn’t present, all subsequent case blocks will execute
until a break statement is located.
6. Which of the looping statements is/are supported by PHP?
i) for loop
ii) while loop
a) i) and ii)
b) i), ii) and iii)
c) i), ii), iii) and iv)
d) Only iv)
View Answer
Answer: c
Explanation: All are supported looping statements in PHP as they can repeat the same
block of code a given number of times, or until a certain condition is met.
7. What will be the output of the following PHP code?
1. <?php
2. $user = array("Ashley", "Bale", "Shrek", "Blank");
3. for ($x=0; $x < count($user); $x++) {
4. if ($user[$x] == "Shrek") continue;
5. printf ($user[$x]);
6. }
7. ?>
a) AshleyBale
b) AshleyBaleBlank
c) ShrekBlank
d) Shrek
View Answer
Answer: b
Explanation: The continue statement causes execution of the current loop iteration to
end and commence at the beginning of the next iteration.
8. If $a = 12 what will be returned when ($a == 12) ? 5 : 1 is executed?
a) 12
b) 1
c) Error
d) 5
View Answer
Answer: d
Explanation: ?: is known as ternary operator. If condition is true then the part just after
the ? is executed else the part after : .
9. What will be the value of $a and $b after the function call in the following PHP code?
1. <?php
2. function doSomething( &$arg ) {
3. $return = $arg;
4. $arg += 1;
5. return $return;
6. }
7. $a = 3;
8. $b = doSomething( $a );
9. ?>
a) a is 3 and b is 4
b) a is 4 and b is 3
c) Both are 3
d) Both are 4
View Answer
Answer: b
Explanation: $a is 4 and $b is 3. The former because $arg is passed by reference, the
latter because the return value of the function is a copy of the initial value of the
argument.
10. Who is the father of PHP?
a) Rasmus Lerdorf
b) Willam Makepiece
c) Drek Kolkevi
d) List Barely
View Answer
Answer: a
Explanation: PHP was originally created by Rasmus Lerdorf in 1994.
1. <?php
2. function calc($price, $tax)
3. {
4. $total = $price + $tax;
5. }
6. $pricetag = 15;
7. $taxtag = 3;
8. calc($pricetag, $taxtag);
9. ?>
a) Call By Value
b) Call By Reference
c) Default Argument Value
d) Type Hinting
View Answer
Answer: a
Explanation: If we call a function by value, we actually pass the values of the arguments
which are stored or copied into the formal parameters of the function. Hence, the original
values are unchanged only the parameters inside the function changes.
4. What will be the output of the following PHP code?
1. <?php
2. function calc($price, $tax="")
3. {
4. $total = $price + ($price * $tax);
5. echo "$total";
6. }
7. calc(42);
8. ?>
a) Error
b) 0
c) 42
d) 84
View Answer
Answer: c
Explanation: You can designate certain arguments as optional by placing them at the
end of the list and assigning them a default value of nothing.
advertisement
i) function()
ii) €()
iii) .function()
iv) $function()
a) Only i)
b) Only ii)
c) i) and ii)
d) iii) and iv)
View Answer
Answer: b
Explanation: A valid function name can start with a letter or underscore, followed by any
number of letters, numbers, or underscores. According to the specified regular
expression ([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*), a function name like this one is valid.
6. What will be the output of the following PHP code?
1. <?php
2. function a()
3. {
4. function b()
5. {
6. echo 'I am b';
7. }
8. echo 'I am a';
9. }
10. a();
11. a();
12. ?>
a) I am a
b) I am bI am a
c) Error
d) I am a Error
View Answer
Answer: a
Explanation: The output will be “I am a” as we are calling a(); so the statement outside
the block of function b() will be called.
7. What will be the output of the following PHP code?
1. <?php
2. function a()
3. {
4. function b()
5. {
6. echo 'I am b';
7. }
8. echo 'I am a';
9. }
10. b();
11. a();
12. ?>
a) I am b
b) I am bI am a
c) Error
d) I am a Error
View Answer
Answer: c
Explanation: The output will be Fatal error: Call to undefined function b(). You cannot
call a function which is inside a function without calling the outside function first. It should
be a(); then b();
8. What will be the output of the following PHP code?
1. <?php
2. $op2 = "blabla";
3. function foo($op1)
4. {
5. echo $op1;
6. echo $op2;
7. }
8. foo("hello");
9. ?>
a) helloblabla
b) Error
c) hello
d) helloblablablabla
View Answer
Answer: c
Explanation: If u want to put some variables in function that was not passed by it, you
must use “global”. Inside the function type global $op2.
9. A function in PHP which starts with __ (double underscore) is known as __________
a) Magic Function
b) Inbuilt Function
c) Default Function
d) User Defined Function
View Answer
Answer: a
Explanation: PHP functions that start with a double underscore – a “__” – are called
magic functions in PHP. They are functions that are always defined inside classes, and
are not stand-alone functions.
10. What will be the output of the following PHP code?
1. <?php
2. function foo($msg)
3. {
4. echo "$msg";
5. }
6. $var1 = "foo";
7. $var1("will this work");
8. ?>
a) Error
b) $msg
c) 0
d) Will this work
View Answer
Answer: d
Explanation: It is possible to call a function using a variable which stores the function
name.
2. Which one of the following PHP functions can be used to find files?
a) glob()
b) file()
c) fold()
d) get_file()
View Answer
Answer: a
Explanation: The function glob() returns an array of filenames or directories which
matches a specified pattern. The function returns an array of files/directories, or it will
return FALSE on failure. Here is an example-
1. // get all php files AND txt files
2. $files = glob('*.{php,txt}', GLOB_BRACE);
3. print_r($files);
4. /* output looks like:
5. Array
6. (
7. [0] => phptest.php
8. [1] => pi.php
9. [2] => post_output.php
10. .
11. .
12. .
13. )
3. Which of the following PHP functions can be used to get the current memory usage?
a) get_usage()
b) get_peak_usage()
c) memory_get_usage()
d) memory_get_peak_usage()
View Answer
Answer: c
Explanation: memory_get_usage() returns the amount of memory, in bytes, that’s
currently being allocated to the PHP script. We can set the parameter ‘real_usage’ to
TRUE to get total memory allocated from system, including unused pages. If it is not set
or FALSE then only the used memory is reported. To get the highest amount of memory
used at any point, we can use the memory_get_peak_usage() function.
4. Which of the following PHP functions can be used for generating unique ids?
a) uniqueid()
b) id()
c) md5()
d) mdid()
View Answer
Answer: a
Explanation: The function uniqueid() is used to generate a unique ID based on the
microtime (current time in microseconds). The ID generated from the function uniqueid()
is not optimal, as it is based on the system time. To generate an ID which is extremely
difficult to predict we can use the md5() function.
5. Which one of the following functions can be used to compress a string?
a) zip_compress()
b) zip()
c) compress()
d) gzcompress()
View Answer
Answer: d
Explanation: The function gzcompress() compresses the string using the ZLIB data
format. One can achieve upto 50% size reduction using this function. The
gzuncompress() function is used to uncompress the string.
advertisement
1. <?php
2. echo chr(52);
3. ?>
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: d
Explanation: The chr() function returns a character from the specified ASCII value. We
can specify ASCII value in decimal, octal, or hex values. The Octal values are defined as
a leading 0, while hex values are defined as a leading 0x. Since the ASCII value of 4 is
52, thus 4 was displayed.
7. What will be the output of the following PHP code?
1. <?php
2. echo ord ("hi");
3. ?>
a) 106
b) 103
c) 104
d) 209
View Answer
Answer: c
Explanation: The ord() function returns the ASCII value of the first character of a string.
The ASCII value of h is 104, thus 104 was displayed.
8. What will be the output of the following PHP code?
1. <?php
2. $str = "Hello World";
3. echo wordwrap($str,5,"<br>\n");
4. ?>
a) Hello World
b)
Hello
World
c)
Hell
o wo
rld
d) World
View Answer
Answer: b
Explanation: The wordwrap() function wraps a string into new lines when it reaches a
specific length.
9. What will be the output of the following PHP code?
1. <?php
2. echo ucwords("i love my country");
3. ?>
a) I love my country
b) i love my Country
c) I love my Country
d) I Love My Country
View Answer
Answer: d
Explanation: The ucwords() function converts the first character of each word in a string
to uppercase.
10. What will be the output of the following PHP code?
1. <?php
2. echo lcfirst("welcome to India");
3. ?>
a) welcome to India
b) welcome to india
c) Welcome to India
d) Welcome to india
View Answer
Answer: a
Explanation: The lcfirst() function converts the first character of a string to lowercase.
1. PHP’s numerically indexed array begin with position ___________
a) 1
b) 2
c) 0
d) -1
View Answer
Answer: c
Explanation: Like all the other programming languages, the first element of an array
always starts with ‘0’.
2. Which of the following are correct ways of creating an array?
i) state[0] = "karnataka";
ii) $state[] = array("karnataka");
iii) $state[0] = "karnataka";
iv) $state = array("karnataka");
advertisement
1. <?php
2. $states = array("Karnataka" => array
3. ("population" => "11,35,000", "capital" => "Bangalore"),
4. "Tamil Nadu" => array( "population" => "17,90,000",
5. "capital" => "Chennai") );
6. echo $states["Karnataka"]["population"];
7. ?>
a) Karnataka 11,35,000
b) 11,35,000
c) population 11,35,000
d) Karnataka population
View Answer
Answer: b
Explanation: In the following PHP code, the variable states are treated as a
multidimensional array and accordingly traverse it to get the value of ‘Karnataka’s
population’.
4. Which of the following PHP function will return true if a variable is an array or false if it
is not an array?
a) this_array()
b) is_array()
c) do_array()
d) in_array()
View Answer
Answer: b
Explanation: The function is_array() is an inbuilt function in PHP which is used to check
whether a variable is an array or not. Its prototype follows: boolean is_array(mixed
variable).
5. Which in-built function will add a value to the end of an array?
a) array_unshift()
b) into_array()
c) inend_array()
d) array_push()
View Answer
Answer: d
Explanation: array_push adds a value to the end of an array, returning the total count of
elements in the array after the new value has been added.
6. What will be the output of the following PHP code?
1. <?php
2. $state = array ("Karnataka", "Goa", "Tamil Nadu",
3. "Andhra Pradesh");
4. echo (array_search ("Tamil Nadu", $state) );
5. ?>
a) True
b) 1
c) False
d) 2
View Answer
Answer: d
Explanation: The array_search() function searches an array for a specified value,
returning its key if located and FALSE otherwise.
7. What will be the output of the following PHP code?
1. <?php
2. $fruits = array ("apple", "orange", "banana");
3. echo (next($fruits));
4. echo (next($fruits));
5. ?>
a) orangebanana
b) appleorange
c) orangeorange
d) appleapple
View Answer
Answer: a
Explanation: The next() function returns the value of the next element in the array. In the
first ‘next($fruits)’ call, it will print orange which is next to apple and so on.
8. Which of the following function is used to get the value of the previous element in an
array?
a) last()
b) before()
c) prev()
d) previous()
View Answer
Answer: c
Explanation: The prev() function returns the previous element in the array.
9. What will be the output of the following PHP code?
1. <?php
2. $fruits = array ("apple", "orange", array ("pear", "mango"),
3. "banana");
4. echo (count($fruits, 1));
5. ?>
a) 3
b) 4
c) 5
d) 6
View Answer
Answer: d
Explanation: The function count() will return the number of elements in an array. The
parameter 1 counts the array recursively i.e it will count all the elements of
multidimensional arrays.
10. Which function returns an array consisting of associative key/value pairs?
a) count()
b) array_count()
c) array_count_values()
d) count_values()
View Answer
Answer: c
Explanation: The function array_count_values() will count all the values of an array. It
will return an associative array, where the keys will be the original array’s values, and
the values are the number of occurrences.
1. What will be the output of the following PHP code?
1. <?php
2. $cars = array("Volvo", "BMW", "Toyota");
3. echo "I like " . $cars[2] . ", " . $cars[1] . " and " . $cars[0] .
".";
4. ?>
1. <?php
2. $fname = array("Peter", "Ben", "Joe");
3. $age = array("35", "37", "43");
4. $c = array_combine($age, $fname);
5. print_r($c);
6. ?>
advertisement
1. <?php
2. $a=array("A","Cat","Dog","A","Dog");
3. $b=array("A","A","Cat","A","Tiger");
4. $c=array_combine($a,$b);
5. print_r(array_count_values($c));
6. ?>
1. <?php
2. $a1 = array("a" => "red", "b" => "green", "c" => "blue", "d" =>
"yellow");
3. $a2 = array("e" => "red", "f" => "green", "g" => "blue", "h" =>
"orange");
4. $a3 = array("i" => "orange");
5. $a4 = array_merge($a2, $a3);
6. $result = array_diff($a1, $a4);
7. print_r($result);
8. ?>
1. <?php
2. $a1 = array("red", "green");
3. $a2 = array("blue", "yellow");
4. $a3 = array_merge($a1, $a2);
5. $a4 = array("a", "b", "c", "d");
6. $a = array_combine($a4, $a3);
7. print_r($a);
8. ?>
a) Array ( [a] => blue [b] => yellow [c] => red [d] => green )
b) Array ( [0] => blue [1] => yellow [2] => red [3] => green )
c) Array ( [0] => red [1] => green [2] => blue [3] => yellow )
d) Array ( [a] => red [b] => green [c] => blue [d] => yellow )
View Answer
Answer: d
Explanation: The function array_merge() merges one or more arrays into one array. If in
the function array_merge(), two or more array elements have the same key, the last one
overrides the others. The function array_combine() will create an array by using the
elements from one “keys” array and one “values” array. The program is the basic
combined application of array_combine() and array_merge().
6. What will be the output of the following PHP code?
1. <?php
2. $a = array("a" => "india", "b" => "brazil", "c" => "china");
3. echo array_shift($a);
4. echo "<br>";
5. array_pop($a);
6. print_r($a);
7. ?>
a)
india
b)
india
c)
china
Array ( [a] => india )
d)
china
View Answer
Answer: a
Explanation: The function array_shift() removes the first element from an array, and it
returns the value of the removed element and the function array_pop() deletes the last
element of an array. So “a” => “India”, “c” => “China” will be deleted and “b” => “Brazil”
will be printed.
1. <?php
2. $a1 = array_fill(1, 4, "hello");
3. $b1 = array_fill(5, 1, "php");
4. $a2 = array_merge($a1, $a2);
5. print_r($a2);
6. echo "<br>";
7. print_r($b1);
8. ?>
a)
Array ( [1] => hello [4] => hello [5] => php )
b)
Array ( [1] => hello [2] => hello [3] => hello [4] => hello )
c)
Array ( [1] => hello [2] => hello [3] => hello [4] => hello [5] => php )
d)
Array ( [1] => hello [2] => hello [3] => hello [4] => hello )
Array ( [1] => php )
View Answer
Answer: c
Explanation: Usage of array_fill() and array_merge() functions.
1. <?php
2. $names = array("Sam", "Bob", "Jack");
3. echo $names[0] . "is the brother of " . $names[1] . " and " .
$names[1] . ".";
4. ?>
1. <?php
2. $names = array("Sam", "Bob", "Jack");
3. echo $names[0]."is the brother of ".$names[1]." and
".$names[1].".".$brother;
4. ?>
1. <?php
2. $place = array("NYC", "LA", "Paris");
3. array_pop($place);
4. $place1 = array("Paris");
5. $place = array_merge($place, $place1);
6. print_r($place);
7. ?>
1. <?php
2. $age = array("Harry" => "21", "Ron" => "23","Malfoy" => "21");
3. array_pop($age);
4. print_r(array_change_key_case($age, CASE_UPPER));
5. ?>
1. <?php
2. $a1 = array("a" => "red", "b" => "green", "c" => "blue", "d" =>
"yellow");
3. $result = array_flip($a1);
4. print_r($result);
5. ?>
a) Array ( [red] => red [green] => green [blue] => blue [yellow] => yellow )
b) Array ( [a] => a [b] => b [c] => c [d] => d )
c) Array ( [red] => a [green] => b [blue] => c [yellow] => d )
d) Array ( [a] => red [b] => green [c] => blue [d] => yellow )
View Answer
Answer: c
Explanation: The function array_flip() flips/exchanges all keys with their associated
values in an array. So, in the above program “a” will be flipped with “red”, “b” will be
flipped with “green” and so on.
3. What will be the output of the following PHP code?
advertisement
1. <?php
2. $a1 = array("a" => "red", "b" => "green", "c" => "blue", "d" =>
"yellow");
3. $a2 = array("e" => "red","f" => "green", "g" => "blue");
4. $result = array_intersect($a1, $a2);
5. print_r($result);
6. ?>
a) Array ( [a] => red [b] => green [c] => blue )
b) Array ( [a] => red [b] => green [c] => blue [d] => yellow )
c) Array ( [e] => red [f] => green [g] => blue )
d) Array ( [a] => red [b] => green [c] => blue [d] => yellow [e] => red [f] => green [g] =>
blue )
View Answer
Answer: a
Explanation: The function array_intersect() compares the values of two (or more) arrays,
and returns the matches. So, in the above program values of a1 and a2 will be
compared and the values present in both the arrays will be the returned.
4. What will be the output of the following PHP code?
1. <?php
2. $a = array(12, 5, 2);
3. echo(array_product($a));
4. ?>
a) 024
b) 120
c) 010
d) 060
View Answer
Answer: b
Explanation: The array_product() function calculates and returns the product of an array.
5. What will be the output of the following PHP code?
1. <?php
2. $a = array("a" => "Jaguar", "b" => "Land Rover",
3. "c" => "Audi", "d" => "Maseratti");
4. echo array_search("Audi", $a);
5. ?>
a) a
b) b
c) c
d) d
View Answer
Answer: c
Explanation: The array_search() function searches for the element and returns the key
of that element.
6. What will be the output of the following PHP code?
1. <?php
2. $city_west = array("NYC", "London");
3. $city_east = array("Mumbai", "Beijing");
4. print_r(array_replace($city_west, $city_east));
5. ?>
1. <?php
2. $people = array("Peter", "Susan", "Edmund", "Lucy");
3. echo pos($people);
4. ?>
a) Lucy
b) Peter
c) Susan
d) Edmund
View Answer
Answer: b
Explanation: The pos() function returns the value of the current element in an array, and
since no operation has been done, the current element is the first element.
8. What will be the output of the following PHP code?
1. <?php
2. $number = range(0, 5);
3. print_r ($number);
4. ?>
a) Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
b) Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 )
c) Array ( [0] => 5 [1] => 5 [2] => 5 [3] => 5 [4] => 5 [5] => 5 )
d) Array ( [0] => 0 [5] => 5 )
View Answer
Answer: a
Explanation: The range() function creates an array containing a range of elements.
9. What will be the output of the following PHP code?
1. <?php
2. $array = array("red", "green");
3. array_push($array, "blue", "yellow");
4. print_r($array);
5. ?>
a) Array ( [0] => red [1] => green [2] => blue [3] => yellow )
b) Array ( [0] => blue [1] => yellow [2] => red [3] => green )
c) Array ( [0] => red [1] => green )
d) Array ( [0] => blue [1] => yellow )
View Answer
Answer: a
Explanation: The function array_push() inserts one or more elements to the end of an
array. So, in the above program blue and yellow will be inserted after previous values.
10. What will be the output of the following PHP code?
1. <?php
2. $age = array("Harry" => "21", "Ron" => "19", "Malfoy" => "23");
3. ksort($age);
4. foreach($age as $x => $x_value)
5. {
6. echo "Key=" . $x . ", Value=" . $x_value;
7. echo "<br>";
8. }
9. ?>
a)
b)
c)
d)
View Answer
Answer: c
Explanation: The ksort() function sorts an associative array in ascending order,
according to the key.
1. What will be the output of the following PHP code?
1. <?php
2. $cars = array("Volvo", "BMW", "Toyota");
3. echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] .
".";
4. ?>
1. <?php
2. $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
3. print_r(array_change_key_case($age, CASE_UPPER));
4. ?>
advertisement
1. <?php
2. $cars = array("Volvo", "BMW", "Toyota", "Honda", "Mercedes",
"Opel");
3. print_r(array_chunk($cars, 2));
4. ?>
a) Array ( [0] => Array ( [1] => Volvo [2] => BMW ) [1] => Array ( [1] => Toyota [2] =>
Honda ) [2] => Array ( [1] => Mercedes [2] => Opel ) )
b) Array ( [1] => Array ( [1] => Volvo [2] => BMW ) [2] => Array ( [1] => Toyota [2] =>
Honda ) [3] => Array ( [1] => Mercedes [2] => Opel ) )
c) Array ( [0] => Array ( [0] => Volvo [1] => Volvo ) [1] => Array ( [0] => BMW [1] =>
BMW ) [2] => Array ( [0] => Toyota [1] => Toyota ) )
d) Array ( [0] => Array ( [0] => Volvo [1] => BMW ) [1] => Array ( [0] => Toyota [1] =>
Honda ) [2] => Array ( [0] => Mercedes [1] => Opel ) )
View Answer
Answer: d
Explanation: The array_chunk() function splits an array into chunks of new arrays.
4. What will be the output of the following PHP code?
1. <?php
2. $fname = array("Peter", "Ben", "Joe");
3. $age = array("35", "37", "43");
4. $c = array_combine($fname, $age);
5. print_r($c);
6. ?>
1. <?php
2. $a = array("A", "Cat", "Dog", "A", "Dog");
3. print_r(array_count_values($a));
4. ?>
1. <?php
2. $a1 = array("a"=>"red", "b"=>"green", "c"=>"blue", "d"=>"yellow");
3. $a2 = array("e"=>"red", "f"=>"green", "g"=>"blue");
4. $result = array_diff($a1, $a2);
5. print_r($result);
6. ?>
1. <?php
2. $a1 = array_fill(3, 4, "blue");
3. $b1 = array_fill(0, 1, "red");
4. print_r($a1);
5. echo "<br>";
6. print_r($b1);
7. ?>
a)
b)
Array ( [4] => blue [5] => blue [6] => blue)
c)
Array ( [3] => blue [4] => blue [5] => blue [6] => blue )
Array ()
d)
Array ( [3] => blue [4] => blue [5] => blue [6] => blue )
View Answer
Answer: d
Explanation: The array_fill() function fills an array with values.
1. <?php
2. $a1 = array("red", "green");
3. $a2 = array("blue", "yellow");
4. print_r(array_merge($a1, $a2));
5. ?>
a) green
b) red
c) redArray( [c] => green [c] => blue )
d) redArray( [b] => green [c] => blue )
View Answer
Answer: d
Explanation: The array_shift() function removes the first element from an array, and
returns the value of the removed element.
10. What will be the output of the following PHP code?
1. <?php
2. $a = array("red", "green", "blue");
3. array_pop($a);
4. print_r($a);
5. ?>
1. <?php
2. $fruits = array ("mango", "apple", "pear", "peach");
3. $fruits = array_flip($fruits);
4. echo ($fruits[0]);
5. ?>
a) mango
b) error
c) peach
d) 0
View Answer
Answer: b
Explanation: As we are flipping the values, $fruits[“mango”] = 0, $fruits[“apple”] = 1 and
so on.
2. Which of the functions is used to sort an array in descending order?
a) sort()
b) asort()
c) rsort()
d) dsort()
View Answer
Answer: c
Explanation: The function sort() will sort the arrays in ascending order, the function
rsort() will sort arrays in descending order. While the function asort() will sort associative
arrays in ascending order, according to the value.
3. What will be the output of the following PHP code?
1. <?php
2. $fruits = array ("mango", "apple", "peach", "pear");
3. $fruits = asort ($fruits);
4. printr ($fruits);
5. ?>
a) Array ( [1] => apple [0] => mango [2] => peach [3] => pear )
b) Array ( [0] => apple [1] => mango [2] => peach [3] => pear )
c) Error
d) Array ( [1] => apple [0] => mango [3] => peach [2] => pear )
View Answer
Answer: c
Explanation: The program will give an error i.e. uncaught error: call to undefined function
printr(). As the correct function is print_r().
advertisement
1. <?php
2. $arr = array ("picture1.JPG", "picture2.jpg",
3. "Picture10.jpg", "picture20.jpg");
4. sort($arr);
5. print_r($arr);
6. ?>
a) Array ( [0] => picture1.JPG [1] => Picture10.jpg [2] => picture2.jpg [3] => picture20.jpg
)
b) Array ( [0] => picture1.JPG [1] => picture2.jpg [2] => Picture10.jpg [3] => picture20.jpg
)
c) Array ( [0] => Picture10.jpg [1] => picture1.JPG [2] => picture2.jpg [3] => picture20.jpg
)
d) Array ( [0] => Picture10.jpg [1] => picture1.JPG [2] => picture20.jpg [3] => picture2.jpg
)
View Answer
Answer: c
Explanation: The function sort() in PHP sorts an indexed array in ascending order. While
sorting, each character is compared with the others and sorted using ASCII values.
5. Which function should we use to sort the array in natural order?
a) dsort()
b) casesort()
c) natcasesort()
d) naturalsort()
View Answer
Answer: c
Explanation: The function natcasesort() in PHP sorts an array by using a “natural order”
algorithm. All the values keep their original keys. Eg: In a natural algorithm, as the
number 2 is less than the number 10. But in computer sorting, 10 is less than 2, because
the first number in “10” is less than 2. The function is case-insensitive.
6. What will be the output of the following PHP code?
1. <?php
2. $face = array ("A", "J", "Q", "K");
3. $number = array ("2","3","4", "5", "6", "7", "8", "9", "10");
4. $cards = array_merge ($face, $number);
5. print_r ($cards);
6. ?>
a) Array ( [0] => A [1] => J [2] => Q [3] => K [4] => 2 [5] => 3 [6] => 4 [7] => 5 [8] => 6 [9]
=> 7 [10] => 8 [11] => 9 [12] => 10 )
b) Array ( [0] => A [1] => 2 [2] => J [3] => 3 [4] => Q [5] => 4 [6] => K [7] => 5 [8] => 6 [9]
=> 7 [10] => 8 [11] => 9 [12] => 10 )
c) Error
d) Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 7 [6] => 8 [7] => 9 [8] => 10 [9]
=> A [10] => J [11] => Q [12] => K )
View Answer
Answer: a
Explanation: The array_merge() function merges one or more arrays into one array. The
resulting array will begin with the first input array parameter, appending each
subsequent array parameter in the order of appearance.
7. What will be the output of the following PHP code?
1. <?php
2. $fruits = array ("apple", "mango", "peach", "pear",
3. "orange");
4. $subset = array_slice ($fruits, 2);
5. print_r ($subset);
6. ?>
1. <?php
2. $fruits = array ("apple", "mango", "peach", "pear",
3. "orange");
4. $subset = array_splice ($fruits, 2);
5. print_r ($fruits);
6. ?>
a) Error
b) Array ( [0] => apple [1] => mango [2] => peach )
c) Array ( [0] => apple [1] => mango )
d) Array ( [0] => pear [1] => orange )
View Answer
Answer: c
Explanation: The array_splice() function removes all elements of an array found within a
specified range.
9. What will be the output of the following PHP code?
1. <?php
2. $number = array ("4", "hello", 2);
3. echo (array_sum ($number));
4. ?>
a) 4hello2
b) 4
c) 2
d) 6
View Answer
Answer: d
Explanation: The array_sum() function add all the values of the input array together,
returning the final sum. If a string datatype is found, it’ll be ignored.
10. What will be the output of the following PHP code?
1. <?php
2. $array1 = array ("KA", "LA", "CA", "MA", "TA");
3. $array2 = array ("KA", "IA", "CA", "GA", "TA");
4. $inter = array_intersect ($array1, $array2);
5. print_r ($inter);
6. ?>
a) Array ( [0] => KA [1] => LA [2] => CA [3] => MA [4] => TA [5] => IA [6] => GA )
b) Array ( [0] => KA [2] => CA [4] => TA )
c) Array ( [1] => IA [3] => GA )
d) Array ( [1] => LA [3] => MA )
View Answer
Answer: b
Explanation: The array_intersect() function returns a key preserved array consisting only
of those values present in the first array that are also present in each of the other input
arrays.
1. The practice of separating the user from the true inner workings of an application
through well-known interfaces is known as _________
a) Polymorphism
b) Inheritance
c) Encapsulation
d) Abstraction
View Answer
Answer: c
Explanation: In object-oriented PHP encapsulation is a concept of wrapping up or
binding up the data members and methods in a single module.
2. Which of the following term originates from the Greek language that means “having
multiple forms,” defines OOP’s ability to redefine, a class’s characteristics?
a) Abstraction
b) Polymorphism
c) Inheritance
d) Differential
View Answer
Answer: b
Explanation: The word polymorphism is derived from Greek word poly which means
“many” and morphism which means the property which helps us to assign more than
one property.
3. The practice of creating objects based on predefined classes is often referred to as
______________
a) class creation
b) object creation
c) object instantiation
d) class instantiation
View Answer
Answer: d
Explanation: In object-oriented programming, classes are the blueprints of php objects.
Classes do not actually become objects until instantiation is done. When someone
instantiates a class, it creates an instance of it, thus creating the object. In other words,
instantiation is the process of creating an instance of an object in memory.
4. Which one of the following property scopes is not supported by PHP?
a) friendly
b) final
c) public
d) static
View Answer
Answer: a
Explanation: PHP supports five class property scopes: public, private, protected, final
and static.
5. Which one of the following can be used to instantiate an object in PHP assuming
class name to be Foo?
a) $obj = new $foo;
b) $obj = new foo;
c) $obj = new foo ();
d) obj = new foo ();
View Answer
Answer: c
Explanation: To create a new object in PHP we can use the new statement to instantiate
a class.
advertisement
a) Only ii)
b) Only iv)
c) i) and ii)
d) iii) and iv)
View Answer
Answer: c
Explanation: In case of public methods, you can forgo explicitly declaring the scope and
just declare the method like you would a function.
10. Which of the following method scopes is/are not supported by PHP?
i) private
ii) friendly
iii) static
iv) abstract
a) Only ii)
b) Only iv)
c) ii) and iv)
d) Only i)
View Answer
Answer: a
Explanation: PHP supports six method scopes: public, private, final, static, protected and
abstract. But it does not support friendly.
1. Which method scope prevents a method from being overridden by a subclass?
a) Abstract
b) Protected
c) Final
d) Static
View Answer
Answer: c
Explanation: When we declare a method is as final then it is not possible to override that
method. Methods should not be overridden due to some security or any other reasons.
2. Which of the following statements is/are true about Constructors in PHP?
a) ii)
b) ii) and iii)
c) i), ii), iii) and iv)
d) ii), iii) and iv)
View Answer
Answer: c
Explanation: If a class name and the function name is similar then the function is known
as constructor. Constructor is automatically called when an object will be initialized.
Constructors can accept parameters. Constructors can call class methods or other
functions. Class constructors can call on other constructors.
3. PHP recognizes constructors by the name _________
a) classname()
b) _construct()
c) function _construct()
d) function __construct()
View Answer
Answer: d
Explanation: A double underscore followed by the construct keyword. Its syntax is
function __construct ([ argument1, argument2,…..]) { Class Initialization code }.
4. Which version of PHP introduced the instanceof keyword?
a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 6
View answer
Answer: b
Explanation: Using instanceof keyword we can determine whether an object is an
instance of a class. $manager = new Employee() … if ($manager instanceof Employee)
echo “True”;
advertisement
5. Which one of the following functions is used to determine whether a class exists?
a) exist()
b) exist_class()
c) class_exist()
d) __exist()
View Answer
Answer: c
Explanation: The class_exist() function returns true or false according to whether the
class exists within the currently executing script content.
6. Which one of the following functions is used to determine object type?
a) obj_type()
b) type()
c) is_a()
d) is_obj()
View Answer
Answer: c
Explanation: The is_a() function returns true if object belongs to a class type or if it
belongs to a class that is a child of that class. Or else false is returned.
7. Which one of the following keyword is used to inherit our subclass into a superclass?
a) extends
b) implements
c) inherit
d) include
View Answer
Answer: a
Explanation: When we extend a class then the subclass will inherit all the public and
protected methods from the parent class.
The keyword implements are used with interfaces. With inheritance, we use the keyword
extends.
8. In the following PHP code, what is/are the properties?
1. <?php
2. class Example
3. {
4. public $name;
5. function Sample()
6. {
7. echo "This is an example";
8. }
9. }
10. ?>
a) echo “This is an example”;
b) public $name;
c) class Example
d) function sample()
View Answer
Answer: b
Explanation: Above code is an example of ‘classes’ in PHP. Classes are the blueprints
of objects. Classes are the programmer-defined data type, which includes the local
methods and the local variables. Class is a collection of objects which has properties
and behaviour.
9. Which keyword is used to refer to properties or methods within the class itself?
a) private
b) public
c) protected
d) $this
View Answer
Answer: d
Explanation: In PHP, the self and ‘this’ keyword are used to refer the class members
within the scope of a class itself. The class members can be either variables or
functions.
10. Which keyword allows class members (methods and properties) to be used without
needing to instantiate a new instance of the class?
a) protected
b) final
c) static
d) private
View Answer
Answer: c
Explanation: Sometimes it is very handy to access the methods and properties in terms
of a class rather than an object. But this can be done through static keyword. Any
method declared as ‘static’ can be accessed without the creation of an object.
1. Which of the following advanced OOP features is/are not supported by PHP?
i) Method overloading
iii) Namespaces
a) i)
b) ii)
c) i) and ii)
d) iii) and iv)
View Answer
Answer: c
Explanation: The advanced OOP features are: Object cloning, Inheritance, Interfaces,
Abstract classes, and Namespaces.
2. Which version of PHP introduced the advanced concepts of OOP?
a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 6
View Answer
Answer: b
Explanation: Advanced concepts of OOP were introduced in PHP version 5.
3. Which one of the following is the right way to clone an object?
a) _clone(targetObject);
b) destinationObject = clone targetObject;
c) destinationObject = _clone(targetObject);
d) destinationObject = clone(targetObject);
View answer
Answer: b
Explanation: You can clone an object by prefacing it with the clone keyword. A copy of
an object is created by using the clone keyword. $copy_of_object = clone $object;
4. The class from which the child class inherits is called ________
i) Child class
advertisement
a) Only i)
b) ii), iii) and iv)
c) Only iii)
d) ii) and iv)
View Answer
Answer: d
Explanation: The class whose properties are inherited by child class is called Base Class
or Parent class.
5. Which of the following is/are true for an abstract class?
i) Abstract classes in PHP are declared with the help of abstract keyword.
ii) A class is declare abstract by using the keyword implements.
iii) It is a class that really isn’t supposed to ever be instantiated but instead serves as a
base class.
iv) Attempting to instantiate an abstract class results in an error.
a) Only i)
b) Only iii)
c) ii) and iv)
d) ii), iii) and iv)
View Answer
Answer: a
Explanation: The abstract classes are the classes in which at least one method need to
be abstract. Abstract classes in PHP are declared with the help of abstract keyword.
6. If one intends to create a model that will be assumed by a number of closely related
objects, which class must be used?
a) Normal class
b) Static class
c) Abstract class
d) Interface
View Answer
Answer: c
Explanation: The abstract classes are the classes in which at least one method need to
be abstract. Abstract classes in PHP are declared with the help of keyword abstract. The
use of class abstract are that all base classes implementing abstract class should give
implementation of abstract methods declared in parent class.
7. If your object must inherit behavior from a number of sources you must use a/an
a) Interface
b) Object
c) Abstract class
d) Static class
View Answer
Answer: a
Explanation: An interface in PHP consists of methods that have no implementations, i.e.
the interface methods are abstract methods. The methods in the interfaces must have
public visibility scope. The interfaces are different from classes as the class can inherit
from one class only whereas the class can implement one or more interfaces.
8. Which method is used to tweak an object’s cloning behavior?
a) clone()
b) __clone()
c) _clone
d) object_clone()
View Answer
Answer: b
Explanation: A copy of an object is created by using the clone keyword, which calls the
object’s __clone() method.
9. Which feature allows us to call more than one method or function of the class in single
instruction?
a) Typecasting
b) Method Including
c) Method adding
d) Method chaining
View Answer
Answer: d
Explanation: When many methods are called in a single instruction in PHP, it is called
method chaining. Following is a basic example of method chaining in php:
$a = new Order();
$a->CreateOrder()->sendOrderEmail()->createShipment();
10. Which magic method is used to implement overloading in PHP?
a) __call
b) __invoke
c) __wakeup
d) __unset
View Answer
Answer: a
Explanation: When a class implements __call(), then an object of that class is called with
a method that doesn’t exist, __call() is called instead.
1. How many error levels are available in PHP?
a) 14
b) 15
c) 16
d) 17
View Answer
Answer: c
Explanation: Whenever the PHP engine encounters any problem that prevents a script
from running properly it generates an error message. There are sixteen error levels and
each level is represented by an integer value and an associated constant.
2. What is the description of Error level E_ERROR?
a) Fatal run-time error
b) Near-fatal error
c) Compile-time error
d) Fatal Compile-time error
View Answer
Answer: a
Explanation: E_ERROR is a fatal run-time error, that can’t be recovered from and the
execution of the script is stopped immediately.
3. Which version of PHP introduced E_STRICT Error level?
a) PHP 4
b) PHP 5
c) PHP 5.2
d) PHP 5.3
View Answer
Answer: b
Explanation: E_STRICT is PHP version portability suggestions. It is not strictly an error,
but it is triggered whenever PHP encounters code that could lead to problems or forward
incompatibilities.
4. Which character does the error_reporting directive use to represent the logical
operator NOT?
a) /
b) !
c) ~
d) ^
View Answer
Answer: c
Explanation: The twidle (~) character is used to represent the logical operator NOT.
5. Say you want to report error concerned about fatal run-time, fatal compile-time error
and core error which statement would you use?
a) error_reporting = E_ALL
b) error_reporting = E_ERROR | E_PARSE | E_CORE_ERROR
c) error_reporting = E_ERROR | E_COMPILE_WARNING | E_CORE_ERROR
d) error_reporting = E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR
View Answer
Answer: d
Explanation: E_ERROR is a fatal run-time error, that can’t be recovered from.
E_COMPILE_ERROR is a fatal error that occurs while the script was being compiled.
And E_CORE_ERROR is a fatal error that occurs during the PHP’s engine initial startup.
advertisement
6. You can extend the exception base class, but you cannot override any of the
preceding methods because the are declared as__________
a) protected
b) final
c) static
d) private
View Answer
Answer: b
Explanation: Marking a method as final prevents it from being overridden by a subclass.
7. What does SPL stand for?
a) Standard PHP Library
b) Source PHP Library
c) Standard PHP List
d) Source PHP List
View Answer
Answer: a
Explanation: The standard PHP library(SPL) extends PHP by offering ready-made
solutions to commonplace tasks such as file access, iteration of various sorts etc.
8. How many predefined exceptions does SPL provide access to?
a) 13
b) 14
c) 15
d) 16
View Answer
Answer: a
Explanation: It provides 13 exceptions: BadFunctionCallException,
BadMethodCallException, DomainException, InvalidArgumentException,
LengthException, LogicException, OutOfBoundsException, OutOfRangeException,
OverflowException, RangeException, RuntimeException, UnderflowException,
UnexpectedValueException.
9. Which of the following is/are an exception?
i) BadFunctionCallException
ii) BadMethodCallException
iii) LogicException
iv) DomainException
a) Only ii)
b) Only iii)
c) i), ii), iii) and iv)
d) Only iv)
View Answer
Answer: c
Explanation: There are total 13 types of predefined exceptions SPL provide access to.
BadFunctionCallException,BadMethodCallExceptio, LogicException, DomainException
are also included in the exceptions.
10. Which of the following is/are an exception?
i) OutOfBoundException
ii) OutOfRangeException
iii) OverflowException
iv) UnderflowException
a) i)
b) i) and iii)
c) i) and ii)
d) i), ii), iii) and iv)
View Answer
Answer: d
Explanation: The exception is thrown if a value is not a valid key. This represents errors
that cannot be detected at compile time. OutOfBoundException, OutOfRangeException,
OverflowException, UnderflowException are valid exceptions in PHP.
1. Which of the following is/are an external data?
i) Cookies
a) Only ii)
b) ii) and iii)
c) Only iii)
d) i), ii), iii) and iv)
View Answer
Answer: d
Explanation: The web applications receives external input. External input/data can be:
User input from a form, Cookies, Web services data, Server variables and, Database
query results.
2. How many types of filtering are present in PHP?
a) 3
b) 2
c) 4
d) None
View Answer
Answer: b
Explanation: There are two main types of filtering: validation and sanitization.
3. Which one of the following filter is used to filter several variables with the same or
different filters?
a) filter_var_array()
b) filter_var()
c) filter_input
d) filter_input_array
View Answer
Answer: a
Explanation: The function filter_var_array() can get multiple variables and it optionally
filters them. The function is useful for filtering many values without calling filter_var().
4. What will be the output of the following PHP code?
advertisement
1. <?php
2. $num = "123";
3. if (!filter_var($num, FILTER_VALIDATE_INT))
4. echo("Integer is not valid");
5. else
6. echo("Integer is valid");
7. ?>
a) No output is returned
b) Integer is not valid
c) Integer is valid
d) Error
View Answer
Answer: c
Explanation: The function filter_var() can validate and sanitize data. This function filters a
single variable with a specified filter.
5. Which one of the following does not describe a validating filter?
a) Are used to allow or disallow specific characters in a string
b) Are used to validate user input
c) Strict format rules
d) Returns the expected type on success or FALSE on failure
View Answer
Answer: a
Explanation: Validate filter are used to validate user input, it have strict format rules and
it returns the expected type on success or FALSE on failure but ‘are used to allow or
disallow specific characters in a string’ describes sanitizing filters.
6. What will be the output of the following PHP code?
1. <?php
2. $var=300;
3. $int_options = array("options"=>array ("min_range"=>0,
"max_range"=>256));
4. if (!filter_var($var, FILTER_VALIDATE_INT, $int_options))
5. echo("Integer is not valid");
6. else
7. echo("Integer is valid");
8. ?>
a) No output is returned
b) Integer is not valid
c) Integer is valid
d) Error
View Answer
Answer: b
Explanation: Since the integer is “300” it is not in the specified range, and the output of
the code above will be: “Integer is not valid”.
7. If the input variable is a string like this “http://www.saåånfoøøundry.com/”, the $url
variable after the sanitizing will look like?
a) http://www.saåånfoøøundry.com/
b) http://www.saaanfoooundry.com/
c) http://www.saånfoøundry.com/
d) https://www.sanfoundry.com/
View Answer
Answer: d
Explanation: Sanitize is nothing but take away invalid characters so therefore the invalid
characters like å and ø will be removed.
8. Which one of the following filter checks if the variable of the specified type exists?
a) filter_has_var
b) filter_var
c) filter_id
d) filter_var_array
View Answer
Answer: a
Explanation: The filter filter_has_var checks if the variable of the specified type exists.
Whereas the function filter_id() returns filter ID of a specified filter name. The function
filter_var() can validate and sanitize data. The function filter_var_array() can get multiple
variables and it optionally filters them.
9. What will be the output of the following PHP code?
1. <?php
2. $value = 'car';
3. $result = filter_var($value, FILTER_VALIDATE_BOOLEAN,
FILTER_NULL_ON_FAILURE);
4. ?>
a) FALSE
b) TRUE
c) NULL
d) ERROR
View Answer
Answer: c
Explanation: There is an undocumented filter flag for FILTER_VALIDATE_BOOLEAN.
The documentation implies that it will return NULL if the value doesn’t match the allowed
true/false values. However this doesn’t happen unless you give it the
FILTER_NULL_ON_FAILURE flag.
10. What will be the output of the following PHP code?
1. <?php
2. function convertSpace($string)
3. {
4. return str_replace("_", " ", $string);
5. }
6. $string = "Peter_is_a_great_guy!";
7. echo filter_var($string, FILTER_CALLBACK,
array("options"=>"convertSpace"));
8. ?>
a) Peter_is_a_great_guy!
b) Peterisagreatguy!
c) Peter is a great guy!
d) Error
View Answer
Answer: c
Explanation: The code above converts all “_” to white spaces. Call the filter_var()
function with the FILTER_CALLBACK filter and an array containing our function.
1. PHP has long supported two regular expression implementations known as _______
and _______
i) Perl
ii) PEAR
iii) Pearl
iv) POSIX
a) i) and ii)
b) ii) and iv)
c) i) and iv)
d) ii) and iii)
View Answer
Answer: c
Explanation: None.
2. Which one of the following regular expression matches any string containing zero or
one p?
a) p+
b) p*
c) P?
d) p#
View Answer
Answer: c
Explanation: None.
3. [:alpha:] can also be specified as ________
a) [A-Za-z0-9]
b) [A-za-z]
c) [A-z]
d) [a-z]
View Answer
Answer: b
Explanation:[:alpha:] is nothing but Lowercase and uppercase alphabetical characters.
4. How many functions does PHP offer for searching strings using POSIX style regular
expression?
a) 7
b) 8
c) 9
d) 10
View Answer
Answer: a
Explanation: ereg(), ereg_replace(), eregi(), eregi_replace(), split(), spliti(), and
sql_regcase() are the functions offered.
advertisement
1. <?php
2. $username = "jasoN";
3. if (ereg("([^a-z])",$username))
4. echo "Username must be all lowercase!";
5. else
6. echo "Username is all lowercase!";
7. ?>
a) Error
b) Username must be all lowercase!
c) Username is all lowercase!
d) No Output is returned
View Answer
Answer: b
Explanation: Because the provided username is not all lowercase, ereg() will not return
FALSE (instead returning the length of the matched string, which PHP will treat as
TRUE), causing the message to output.
6. POSIX implementation was deprecated in which version of PHP?
a) PHP 4
b) PHP 5
c) PHP 5.2
d) PHP 5.3
View Answer
Answer: d
Explanation: None.
7. POSIX stands for ____________
a) Portable Operating System Interface for Unix
b) Portable Operating System Interface for Linux
c) Portative Operating System Interface for Unix
d) Portative Operating System Interface for Linux
View Answer
Answer: a
Explanation: None.
8. What will be the output of the following PHP code?
1. <?php
2. $text = "this is\tsome text that\nwe might like to parse.";
3. print_r(split("[\n\t]",$text));
4. ?>
i) fol
ii) fool
iii) fooool
iv) fooooool
a) Only i)
b) ii) and iii)
c) i), iii) and iv)
d) i) and iv)
View Answer
Answer: b
Explanation: This matches f followed by two to four occurrences of o.
10. Which among the following is/are not a metacharacter?
i) \a
ii) \A
iii) \b
iv) \B
a) Only i)
b) i) and iii)
c) ii), iii) and iv)
d) ii) and iv)
View Answer
Answer: a
Explanation: /A, /b and /B are metacharacters. \A: Matches only at the beginning of the
string. \b: Matches a word boundary. \B: Matches anything but a word boundary.
1. How many functions does PHP offer for searching and modifying strings using Perl-
compatible regular expressions.
a) 7
b) 8
c) 9
d) 10
View Answer
Answer: b
Explanation: The functions are preg_filter(), preg_grep(), preg_match(),
preg_match_all(), preg_quote(), preg_replace(), preg_replace_callback(), and
preg_split().
2. What will be the output of the following PHP code?
1. <?php
2. $foods = array("pasta", "steak", "fish", "potatoes");
3. $food = preg_grep("/^s/", $foods);
4. print_r($food);
5. ?>
a) Array ( [0] => pasta [1] => steak [2] => fish [3] => potatoes )
b) Array ( [3] => potatoes )
c) Array ( [1] => steak )
d) Array ( [0] => potatoes )
View Answer
Answer: c
Explanation: This function is used to search an array for foods beginning with s.
3. Say we have two compare two strings which of the following function/functions can
you use?
i) strcmp()
ii) strcasecmp()
iii) strspn()
iv) strcspn()
advertisement
a) i) and ii)
b) iii) and iv)
c) only i)
d) i), ii), iii) and iv)
View Answer
Answer: d
Explanation: All of the functions mentioned above can be used to compare strings in
some or the other way.
4. Which one of the following functions will convert a string to all uppercase?
a) strtoupper()
b) uppercase()
c) str_uppercase()
d) struppercase()
View Answer
Answer: a
Explanation: Its prototype follows string strtoupper(string str).
5. What will be the output of the following PHP code?
1. <?php
2. $title = "O'malley wins the heavyweight championship!";
3. echo ucwords($title);
4. ?>
1. <?php
2. echo str_pad("Salad", 5)." is good.";
3. ?>
a) SaladSaladSaladSaladSalad is good
b) is good SaladSaladSaladSaladSalad
c) is good Salad
d) Salad is good
View Answer
Answer: d
Explanation: The str_pad() function pads a string with a specified number of characters.
7. Which one of the following functions can be used to concatenate array elements to
form a single delimited string?
a) explode()
b) implode()
c) concat()
d) concatenate()
View Answer
Answer: b
Explanation: None.
8. Which one of the following functions finds the last occurrence of a string, returning its
numerical position?
a) strlastpos()
b) strpos()
c) strlast()
d) strrpos()
View Answer
Answer: d
Explanation: None.
9. What will be the output of the following PHP code?
1. <?php
2. $author = "[email protected]";
3. $author = str_replace("a","@",$author);
4. echo "Contact the author of this article at $author.";
5. ?>
1. <?php
2. $url = "[email protected]";
3. echo ltrim(strstr($url, "@"),"@");
4. ?>
a) [email protected]
b) nachiketh
c) nachiketh@
d) example.com
View Answer
Answer: d
Explanation: The strstr() function returns the remainder of a string beginning with the first
occurrence of a predefined string.
1. The filesize() function returns the file size in ___________
a) bits
b) bytes
c) kilobytes
d) gigabytes
View Answer
Answer: b
Explanation: The function filesize() returns the size of the specified file and it returns the
file size in bytes on success or FALSE on failure.
2. Which one of the following PHP function is used to determine a file’s last access time?
a) fileltime()
b) filectime()
c) fileatime()
d) filetime()
View Answer
Answer: c
Explanation: The fileatime() function returns a file’s last access time in Unix timestamp
format or FALSE on error.
3. Which one of the following function is capable of reading a file into an array?
a) file()
b) arrfile()
c) arr_file()
d) file_arr()
View Answer
Answer: a
Explanation: The function file() will read the entire file into an array.
4. Which one of the following function is capable of reading a file into a string variable?
a) file_contents()
b) file_get_contents()
c) file_content()
d) file_get_content()
View Answer
Answer: b
Explanation: The function file_get_contents() reads a file into a string. This is the
preferred way to read the contents of a file into a string as it will use memory mapping
techniques.
5. Which one of the following function is capable of reading a specific number of
characters from a file?
a) fgets()
b) fget()
c) fileget()
d) filegets()
View Answer
Answer: a
Explanation: The function fgets() will return a line from an open file. This stops returning
on a new line, at the specified length, or at EOF, whichever comes first. Its prototype is
string fgets(resource handle [, int length]). If the optional length parameter is omitted,
1024 character is assumed.
advertisement
6. Which one of the following function operates similarly to fgets(), except that it also
strips any HTML and PHP tags form the input?
a) fgetsh()
b) fgetsp()
c) fgetsa()
d) fgetss()
View Answer
Answer: d
Explanation: The function fgetss() returns a line, with HTML and PHP tags removed,
from an open file. This function operates similarly to fgets(), except that it also strips any
HTML and PHP tags form the input.
7. Which one of the following function outputs the contents of a string variable to the
specified resource?
a) filewrite()
b) fwrite()
c) filewrites()
d) fwrites()
View Answer
Answer: b
Explanation: The function fwrite() writes to an open file. This will stop at the end of the
file or when it reaches the specified length, whichever comes first.
8. Which function sets the file filename last-modified and last-accessed times?
a) sets()
b) set()
c) touch()
d) touched()
View Answer
Answer: c
Explanation: The function touch() will set the access and modification time of the
specified file. Syntax is touch(filename, time, atime).
9. Which function is useful when you want to output the executed command result?
a) out_cmm()
b) out_system()
c) cmm()
d) system()
View Answer
Answer: d
Explanation: The function system() in PHP is same as the C version of the function as in
that it executes the given command and outputs the result. This function also tries to
automatically flush the web server’s output buffer after each line of output if PHP is
running as a server module.
10. Which one of the following function reads a directory into an Array?
a) scandir()
b) readdir()
c) scandirectory()
d) readdirectory()
View Answer
Answer: a
Explanation: It returns an array consisting of files and directories found in directory or
returns FALSE on error.
1. What will be the output of the following PHP code?
1. <?php
2. echo (checkdate(4,31,2010) ? 'Valid' : 'Invalid');
3. ?>
a) TRUE
b) FALSE
c) Valid
d) Invalid
View Answer
Answer: d
Explanation: The function checkdate() is used to validate a Gregorian date. In the
program, April has 30 days and the above date is 31 therefore Invalid is returned.
2. The date() function returns ___ representation of the current date and/or time.
a) Integer
b) String
c) Boolean
d) Float
View Answer
Answer: b
Explanation: The function date() is used to format a local date and time, and it will return
the formatted date string. The syntax of this function is string date(string format [, int
timestamp]).
3. Which one of the following format parameter can be used to identify timezone?
a) T
b) N
c) E
d) I
View Answer
Answer: c
Explanation: When the format is E the time zone is identified and returned, for example,
America/New York. N denotes ISO-8601 numeric representation of the day of the week,
T denotes time zone abbreviation, I denotes whether or not the date is in daylight saving
time.
advertisement
1. <?php
2. echo "Today is ".date("F d, Y");
3. ?>
1. <?php
2. printf( date("t") );
3. ?>
a) 30
b) 22
c) JUNE
d) 2013
View Answer
Answer: a
Explanation: The t parameter is used to determine the number of days in the current
month.
9. Suppose you want to calculate the date 45 days from the present date which one of
the following statement will you use?
a) totime(“+45”)
b) totime(“+45 days”)
c) strtotime(“+45 days”)
d) strtotime(“-45 days”)
View Answer
Answer: c
Explanation: The strtotime() function and GNU date syntax is used to calculating the
date x days from the present date.
10. To create an object and set the date to JUNE 22, 2013, which one of the following
statement should be executed?
a) $date = Date(“22 JUNE 2013”)
b) $date = new Date(“JUNE 22 2013”)
c) $date = DateTime(“22 JUNE 2013”)
d) $date = new DateTime(“22 JUNE 2013”)
View Answer
Answer: d
Explanation: The dateTime() method is class constructor. You can set the date either at
the time of instantiation or later by using a variety of mutators.
1. How many methods does the DateTime class have?
a) 8
b) 9
c) 10
d) 11
View Answer
Answer: b
Explanation: The DateTime class has nine methods, all of which are public.
2. How many constants does the DateTime class have?
a) 8
b) 9
c) 10
d) 11
View Answer
Answer: d
Explanation: The DateTime class has 11 constants, no static properties or methods.
3. Which method is simply an object-oriented version of date()?
a) DateTime::format()
b) DateTime::modify()
c) DateTime::setTime()
d) DateTime::setDate()
View Answer
Answer: a
Explanation: The format() method is simply an object-oriented version of date(). It takes
$dateFormat as an argument. $dateFormat is a string consisting of the same date
formatting characters accepted by the procedural date() function.
4. Which of the following is the right way to use the DateTime class?
a) $date = get_Class(DateTime);
b) $date = class DateTime;
c) $date = new DateTime();
d) $date = new class DateTime();
View Answer
Answer: c
Explanation: The way you use the DateTime class is like any other class: instantiate an
object, and store it in a variable.
5. What will be the output of the following PHP code if date is 24/02/2008?
advertisement
1. <?php
2. $date = new DateTime();
3. echo $date->format('l,F,js,Y');
4. ?>
i) listAbbreviations()
ii) getName()
iii) getOffset()
iv) listIdentifiers()
a) Only i)
b) Only ii)
c) i) and iv)
d) iii) and iv)
View Answer
Answer: c
Explanation: listAbbreviations() and listIdentifiers() are static methods.
8. Which of the following DateTimeZone classes are non-static?
i) _construct()
ii) getName()
iii) getOffset()
iv) getTransitions()
a) Only i)
b) Only ii)
c) i), ii), iii) and iv)
d) iii) and iv)
View Answer
Answer: c
Explanation: All of the given methods are non static.
9. Which of the following statements can be used to add two months to the existing
date?
a) $date->modify(‘+2 months’);
b) $date = modify(‘+2 months’);
c) $date = modify(‘2+ months’);
d) $date->modify(‘2+ months’);
View Answer
Answer: a
Explanation: To change the date stored by a DateTime object after it has been created,
you use DateTime::modify() with a natural language expression.
10. Which method enables you to calculate whether daylight saving time is in force at a
specific date and time?
a) getOffset()
b) getTranitions()
c) ISODate()
d) savingTime()
View Answer
Answer: b
Explanation: This outputs a multidimensional array listing past and future changes to the
offset from UTC for a DateTimeZone object.
1. Which two predefined variables are used to retrieve information from forms?
a) $GET & $SET
b) $_GET & $_SET
c) $__GET & $__SET
d) GET & SET
View Answer
Answer: b
Explanation: The global variables $_GET is used to collect form data after submitting an
HTML form with the method=”get”. The variable $_SET is also used to retrieve
information from forms.
2. The attack which involves the insertion of malicious code into a page frequented by
other users is known as _______________
a) basic sql injection
b) advanced sql injection
c) cross-site scripting
d) scripting
View Answer
Answer: c
Explanation: The cross-site scripting attack is among one of the top five security attacks
carried out across the Internet. It is also known as XSS, this attack is a type of code
injection attack which is made possible by incorrectly validating user data, which usually
gets inserted into the page through a web form or using an altered hyperlink.
3. When you use the $_GET variable to collect data, the data is visible to ___________
a) none
b) only you
c) everyone
d) selected few
View Answer
Answer: c
Explanation: The information sent from a form with the method GET is visible to
everyone i.e. all variable names and values are displayed in the URL.
4. When you use the $_POST variable to collect data, the data is visible to
___________
a) none
b) only you
c) everyone
d) selected few
View Answer
Answer: b
Explanation: The information sent from a form with the method POST is invisible to
others i.e. all names/values are embedded within the body of the HTTP request.
5. Which variable is used to collect form data sent with both the GET and POST
methods?
a) $BOTH
b) $_BOTH
c) $REQUEST
d) $_REQUEST
View Answer
Answer: d
Explanation: In PHP the global variable $_REQUEST is used to collect data after
submitting an HTML form.
advertisement
6. Which one of the following should not be used while sending passwords or other
sensitive information?
a) GET
b) POST
c) REQUEST
d) NEXT
View Answer
Answer: a
Explanation: The information sent from a form with the method GET is visible to
everyone i.e. all variable names and values are displayed in the URL. So, it should not
be used while sending passwords or other sensitive information.
7. Which function is used to remove all HTML tags from a string passed to a form?
a) remove_tags()
b) strip_tags()
c) tags_strip()
d) tags_remove()
View Answer
Answer: b
Explanation: The function strip_tags() is used to strip a string from HTML, XML, and
PHP tags.
8. What will be the value of the variable $input in the following PHP code?
1. <?php
2. $input = "Swapna<td>Lawrence</td>you are really<i>pretty</i>!";
3. $input = strip_tags($input,"<i></i>");
4. echo $input;
5. ?>
i) $_SERVER['PHP_AUTH_USER'].
ii) $_SERVER['PHP_AUTH_USERS'].
iii) $_SERVER['PHP_AUTH_PU'].
iv) $_SERVER['PHP_AUTH_PW'].
a) i) and ii)
b) ii) and iv)
c) i) and iv)
d) ii) and iii)
View Answer
Answer: c
Explanation: $_SERVER[‘PHP_AUTH_USER’] and $_SERVER[‘PHP_AUTH_PW’] store
the username and password values, respectively.
3. Which of the following PHP function is commonly used when handling authentication
via PHP?
i) header()
ii) footer()
iii) inset()
iv) isset()
advertisement
a) i) and iv)
b) ii) and iv)
c) ii) and iii)
d) i) and iii)
View Answer
Answer: a
Explanation: The function isset () is used to check whether a variable is set or not and
the function header() sends a raw HTTP header to a client.
4. Which function is used to verify whether a variable contains a value?
a) header()
b) footer()
c) inset()
d) isset()
View Answer
Answer: d
Explanation: The isset() function determines whether a variable has been assigned a
value. Its prototype follows: boolean isset(mixed var [,mixed var [,…]]).
5. Which of the following are types of PHP authentication implementation
methodologies?
i) small list
a) i) and iv)
b) i) and iii)
c) ii) and iii)
d) ii) and iv)
View Answer
Answer: c
Explanation: Such requirements are better satisfied by implementing a database based
solution.
10. Which is the most powerful authentication method among the four?
a) Hard-coding a login pair directly into the script
b) File-based authentication
c) Data-based authentication
d) PEAR’S HTTP authentication
View Answer
Answer: c
Explanation: It not only enhances administrative convenience and scalability but also
can be integrated into a larger database infrastructure.
1. Which directive determines whether PHP scripts on the server can accept file
uploads?
a) file_uploads
b) file_upload
c) file_input
d) file_intake
View Answer
Answer: a
Explanation: With PHP, it is easy to upload files to the server. We need to ensure that
PHP is configured to allow file uploads. In the “php.ini” file, search for the file_uploads
directive, and set it to On. By default, its value is on.
2. Which of the following directive determines the maximum amount of time that a PHP
script will spend attempting to parse input before registering a fatal error?
a) max_take_time
b) max_intake_time
c) max_input_time
d) max_parse_time
View Answer
Answer: c
Explanation: This is relevant because particularly large files can take some time to
upload, eclipsing the time set by this directive.
3. What is the default value of max_input_time directive?
a) 30 seconds
b) 60 seconds
c) 120 seconds
d) 1 second
View Answer
Answer: b
Explanation: The default value of the max_input_time directive is 60 seconds.
4. Since which version of PHP was the directive max_file_limit available.
a) PHP 5.2.1
b) PHP 5.2.2
c) PHP 5.2.12
d) PHP 5.2.21
View Answer
Answer: c
Explanation: The max_file_limit directive sets an upper limit on the number of files which
can be simultaneously uploaded.
5. What is the default value of the directive max_file_limit?
a) 10 files
b) 15 files
c) 20 files
d) 25 files
View Answer
Answer: c
Explanation: The default value of the directive max_file_limit is 20 files.
advertisement
6. If the directive session.cookie_lifetime is set to 3600, the cookie will live until
____________
a) 3600 sec
b) 3600 min
c) 3600 hrs
d) the browser is restarted
View Answer
Answer: a
Explanation: The lifetime is specified in seconds, so if the cookie should live 1 hour, this
directive should be set to 3600.
7. Neglecting to set which of the following cookie will result in the cookie’s domain being
set to the host name of the server which generated it.
a) session.domain
b) session.path
c) session.cookie_path
d) session.cookie_domain
View Answer
Answer: d
Explanation: The directive session.cookie_domain determines the domain for which the
cookie is valid.
8. What is the default number of seconds that cached session pages are made available
before the new pages are created?
a) 360
b) 180
c) 3600
d) 1800
View Answer
Answer: b
Explanation: The directive which determines this is session.cache_expire.
9. What is the default time(in seconds) for which session data is considered valid?
a) 1800
b) 3600
c) 1440
d) 1540
View Answer
Answer: c
Explanation: The session.gc_maxlifetime directive determines this duration. It can be set
to any required value.
10. Which one of the following function is used to start a session?
a) start_session()
b) session_start()
c) session_begin()
d) begin_session()
View Answer
Answer: b
Explanation: A session is started with the function session_start() . The session
variables are set with the PHP global variable which is $_SESSION.
1. Which function is used to erase all session variables stored in the current session?
a) session_destroy()
b) session_change()
c) session_remove()
d) session_unset()
View Answer
Answer: d
Explanation: The function session_unset() frees all session variables that is currently
registered. This will not completely remove the session from the storage mechanism. If
you want to completely destroy the session, you need to use the function
session_destroy().
2. What will the function session_id() return is no parameter is passed?
a) Current Session Identification Number
b) Previous Session Identification Number
c) Last Session Identification Number
d) Error
View Answer
Answer: a
Explanation: The function session_id() will return the session id for the current session or
the empty string (” “) if there is no current session.
3. Which one of the following statements should you use to set the session username to
Nachi?
a) $SESSION[‘username’] = “Nachi”;
b) $_SESSION[‘username’] = “Nachi”;
c) session_start(“nachi”);
d) $SESSION_START[“username”] = “Nachi”;
View Answer
Answer: b
Explanation: You need to refer the session variable ‘username’ in the context of the
$_SESSION superglobal.
4. What will be the output of the following PHP code? (Say your previous session
username was nachi.)
1. unset($_SESSION['username']);
2. printf("Username now set to: %s", $_SESSION['username']);
6. Which Apache directive outputs Apache’s server version, server name, port and
compile-in modules?
a) ServerSignature
b) ServerName
c) ServerDetails
d) ServerInfo
View Answer
Answer: a
Explanation: It is capable of displaying output like: Apache/2.2.11 (Ubuntu) Server at
localhost Port 80.
7. Which directive determines which degree of server details is provided if the
ServerSignature directive is enabled?
a) ServerAddons
b) ServerExtra
c) ServerTokens
d) ServerDetails
View Answer
Answer: c
Explanation: Six options are available: Full, Major, Minimal, Minior, OS, and Prod. If it is
set to Full ‘Apache/2.2.11 (Ubuntu) PHP/5.3.2 Server’ will be displayed.
8. Which directive should we disable to obscure the fact that PHP is being used on our
server?
a) show_php
b) expose_php
c) print_php
d) info_php
View Answer
Answer: b
Explanation: Using the expose_php directive we can prevent PHP version details from
being appended on our web server signature. When expose_php is disabled, the server
signature will look like: Apache/2.2.11 (Ubuntu) Server.
9. Say I want to change the extension of a PHP file, which of the following statements
should I edit to change from .php to .html in the httpd.conf file?
a) AddType application/x-httpd-php .php
b) AddType application/x-httpd-php .asp
c) AddType application/x-httpd-asp .php
d) AddType application/x-httpd-asp .asp
View Answer
Answer: a
Explanation: Just change the .php to .html and your extension will change. This is used
for security purpose.
10. The developers of PHP deprecated the safe mode feature as of which PHP version.
a) PHP 5.1.0
b) PHP 5.2.0
c) PHP 5.3.0
d) PHP 5.3.1
View Answer
Answer: c
Explanation: This happened because safe mode often creates many problems as it
resolves, largely due to the need for enterprise applications to use many of the features
safe mode disables.
1. <?php
2. $number = array(0,1,two,three,four,5);
3. $num = preg_grep("/[0-5]/", $number);
4. print_r($num);
5. ?>
1. <?php
2. $name = "What is your name?";
3. if (preg_match("/name/"),$name)
4. echo "My name is Will Pitt ";
5. else
6. echo "My name is not Will Pitt ";
7. if (preg_match("/are/"))
8. echo "I am great";
9. else
10. echo "I am not great";
11. ?>
1. <?php
2. $str = "Hello! My name is Cameron Fox. Coffee?";
3. $find = array('/is/','/coffee/');
4. $replace = array('/was/','/tea/');
5. echo preg_replace ($find, $replace, $str);
6. ?>
1. <?php
2. $line = "You like dogs. I hate dogs. We should marry.";
3. $sen = preg_split('/\./', $line);
4. print_r($sen);
5. ?>
SID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
advertisement
2. Which one of the following methods is responsible for sending the query to the
database?
a) query()
b) send_query()
c) sendquery()
d) mysqli_query()
View Answer
Answer: d
Explanation: The method mysqli_query() is responsible for sending the query to the
database. Query() method was previously used in older versions of PHP.
3. Which one of the following methods recuperates any memory consumed by a result
set?
a) destroy()
b) mysqli_free_result()
c) alloc()
d) free()
View Answer
Answer: b
Explanation: The function mysqli_free_result() is used to free the memory which is
associated with the result. Once this method is executed, the result set is no longer
available. Free() function was used in the previous version of PHP.
4. Which of the methods are used to manage result sets using both associative and
indexed arrays?
a) get_array() and get_row()
b) get_array() and get_column()
c) fetch_array() and fetch_row()
d) mysqli_fetch_array() and mysqli_fetch_row()
View Answer
Answer: d
Explanation: The method mysqli_fetch_array() is used to fetch a result row as an
associative array or a numeric array.
And the function mysqli_fetch_row() is used to fetche one row from a result-set and
returns it as an enumerated array.
The method fetch_array() and fetch_row() were used in the previous version of PHP.
advertisement
5. Which one of the following method is used to retrieve the number of rows affected by
an INSERT, UPDATE, or DELETE query?
a) num_rows()
b) affected_rows()
c) changed_rows()
d) mysqli_affected_rows()
View Answer
Answer: d
Explanation: The method mysqli_num_rows() is only useful for determining the number
of rows retrieved by a SELECT query. But to retrieve the number of rows affected by
INSERT, UPDATE, or DELETE query, use mysqli_affected_rows(). Num_rows() and
affected_rows() were used in previous version of PHP.
6. Which version of MySQL introduced the prepared statements?
a) MySQL 4.0
b) MySQL 4.1
c) MySQL 4.2
d) MySQL 4.3
View Answer
Answer: b
Explanation: When the query() method is looped repeatedly it comes at a cost of both
overhead, because of the need to repeatedly parsing of the almost identical query for
validity, and coding convenience, because of the need to repeatedly reconfigure the
query using the new values for each iteration. To help resolve the issues incurred by
repeatedly executed queries, MySQL introduced prepared statements.
7. Which of the following methods is used to execute the statement after the parameters
have been bound?
a) bind_param()
b) bind_result()
c) bound_param()
d) bound_result()
View Answer
Answer: a
Explanation: Once the statement has been prepared, it needs to be executed. Exactly
when it’s executed depends upon whether you want to work with bound parameters or
bound results. In the case of bound parameters, you’d execute the statement after the
parameters have been bound with the bind_param() method.
8. Which one of the following methods is used to recuperating prepared statements
resources?
a) end()
b) finish()
c) mysqli_close()
d) close()
View Answer
Answer: c
Explanation: The function mysqli_close() is used to close an opened database
connection. Once you’ve finished using a prepared statement, the resources it requires
can be recuperated with the mysqli_close() method. Close() was used in previous
version of PHP.
9. Which method retrieves each row from the prepared statement result and assigns the
fields to the bound results?
a) get_row()
b) fetch_row()
c) fetch()
d) mysqli_fetch_row()
View Answer
Answer: d
Explanation: The function mysqli_fetch_row() is used to fetche row from a result-set and
returns it as an enumerated array.
Its syntax is mysqli_fetch_row(result);
10. Which method rolls back the present transaction?
a) commit()
b) undo()
c) mysqli_rollback()
d) rollback()
View Answer
Answer: c
Explanation: The function mysqli_rollback() is used to roll back from the current
transaction for the specified database connection. Its syntax is:
mysqli_rollback(connection);
Rollback() was used in previous version of PHP.
1. Which one of the following is not a valid class name?
a) ShopProduct
b) Shopproduct
c) Shopproduct1
d) 1shopproduct
View Answer
Answer: d
Explanation: You declare a class with the class keyword and an arbitrary class name.
Class names can be any combination of numbers and letters, although they must not
begin with a number.
2. Fill in the blank with the best option. An Object is a/an ________ of a class.
a) type
b) prototype
c) instance
d) object
View Answer
Answer: c
Explanation: An object is said to be an instance of its class. It is of the type defined by
the class.
3. There are two objects-
$product1 = new Shop();
$product2 = new Shop();
Which one of the following statements is right about them?
a) $product1 and $product2 are same objects of the same type generated from a single
class
b) $product1 and $product2 are different objects of the same type generated from a
single class
c) $product1 and $product2 are same objects of the different type generated from a
single class
d) $product1 and $product2 are different objects of the different type generated from a
single class
View Answer
Answer: b
Explanation: In PHP object is created using new operator. $product1 and $product2 are
different objects of the same type generated from a single class.
4. Which version of PHP introduced the visibility keywords i.e public, private, and
protected?
a) PHP 4
b) PHP 5
c) PHP 5.1
d) PHP 5.3
View Answer
Answer: b
Explanation: In PHP 4, all properties were declared with var keyword, which is identical
in effect to using public. For the sake of backward compatibility, PHP 5 accepts var in
place of public for properties.
5. Which characters is used to access property variables on an object-by-object basis?
a) ::
b) =
c) ->
d) .
View Answer
Answer: c
Explanation: Example: $product1->title=”My Life”;
advertisement
6. Code that uses a class, function, or method is often described as the ____________
a) client code
b) user code
c) object code
d) class code
View Answer
Answer: a
Explanation: Code that uses a class, function, or method is often described as the
class’s, function, or method client or as client code.
7. Which keyword precedes a method name?
a) method
b) function
c) public
d) protected
View Answer
Answer: b
Explanation: A method declaration resembles a function declaration. The function
keyword precedes a method name, followed by an optional list of argument variables in
parentheses.
8. If you omit the visibility keyword in your method declaration, by default the method will
be declared as ____________
a) public
b) private
c) protected
d) friendly
View Answer
Answer: a
Explanation: By declaring a method public, you ensure that it can be invoked from
outside of the current object.
9. Which function is used to determine whether the variable’s value is either TRUE or
FALSE?
a) boolean()
b) is_boolean()
c) bool()
d) is_bool()
View Answer
Answer: d
Explanation: None.
10. What will be the output of the following PHP code?
1. <?php
2. class ShopProductWriter
3. {
4. public function write( $shopProduct )
5. {
6. $str = "{$shopProduct->title}: " .$shopProduct-
>getProducer() ." ({$shopProduct->price})\n";
7. print $str;
8. }
9. }
10. $product1 = new ShopProduct( "My Antonia", "Willa",
"Cather", 5.99 );
11. $writer = new ShopProductWriter();
12. $writer->write( $product1 );
13. ?>
a) Error
b) Cather: Willa My Antonia (5.99)
c) Willa: Cather My Antonia (5.99)
d) My Antonia: Willa Cather (5.99)
View Answer
Answer: d
Explanation: None.
1. Which version of PHP introduced class type hints?
a) PHP 4
b) PHP 4.3
c) PHP 5
d) PHP 5.3
View Answer
Answer: c
Explanation: None.
2. Inheritance is the means by which one or more classes can be derived from a/an
___________ class.
a) base
b) abstract
c) null
d) predefined
View Answer
Answer: a
Explanation: A class that inherits from another is said to be a subclass of it. This
relationship is often described in terms of parents and children. A child class is derived
from and inherits characteristics from the parent.
3. What will be the output of the following PHP code?
1. <?php
2. class MyClass
3. {
4. }
5.
6. class NotMyClass
7. {
8. }
9. $a = new MyClass;
10.
11. var_dump($a instanceof MyClass);
12. var_dump($a instanceof NotMyClass);
13. ?>
a)
bool(true)
bool(true)
b)
bool(false)
bool(false)
c)
bool(true)
bool(false)
advertisement
d)
bool(false)
bool(true)
View Answer
Answer: c
Explanation: instanceof is used to determine whether a PHP variable is an instantiated
object of a certain class.
1. <?php
2. class ParentClass
3. {
4. }
5.
6. class MyClass extends ParentClass
7. {
8. }
9.
10. $a = new MyClass;
11.
12. var_dump($a instanceof MyClass);
13. var_dump($a instanceof ParentClass);
14. ?>
a)
bool(false)
bool(false)
b)
bool(true)
bool(true)
c)
bool(false)
bool(true)
d)
bool(true)
bool(false)
View Answer
Answer: b
Explanation: instanceof can also be used to determine whether a variable is an
instantiated object of a class that inherits from a parent class.
1. <?php
2. class MyClass
3. {
4. }
5.
6. $a = new MyClass;
7. var_dump(!($a instanceof stdClass));
8. ?>
a) bool(true)
b) bool(false)
c) error
d) none of the mentioned
View Answer
Answer: a
Explanation: To check if an object is not an instanceof a class, the logical not operator
can be used.
6. What will be the output of the following PHP code?
1. <?php
2. interface MyInterface
3. {
4. }
5.
6. class MyClass implements MyInterface
7. {
8. }
9.
10. $a = new MyClass;
11.
12. var_dump($a instanceof MyClass);
13. var_dump($a instanceof MyInterface);
14. ?>
a)
bool(false)
bool(false)
b)
bool(true)
bool(true)
c)
bool(false)
bool(true)
d)
bool(true)
bool(false)
View Answer
Answer: b
Explanation: instanceof can also be used to determine whether a variable is an
instantiated object of a class that implements an interface.
7. What should be used to refer to a method in the context of a class rather than an
object you use?
a) ->
b) __
c) $
d) ::
View Answer
Answer: d
Explanation: Example- parent::__construct()
8. Prior to which version of PHP did constructors took the name of the enclosing class.
a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 5.4
View Answer
Answer: b
Explanation: The new unified constructors use the name __construct(). Using the old
syntax, a call to a parent constructor would tie you to that particular class:
parent::ShopProduct();
9. Which method or property can only be accessed from within the enclosing class?
Even subclasses have no access.
a) public
b) friendly
c) private
d) protected
View Answer
Answer: c
Explanation: None.
10. A mutator method is also called as ___________
a) Setter
b) Accessor
c) Getter
d) Destructor
View Answer
Answer: a
Explanation: An accessor method is called getter. The common use of a mutator method
is to initialise the value of member variables of a class.
1. When you are building administrative links you’ll need to accept two arguments, which
of the following are they?
a) URL of previous entry and URL of the entry you are working with
b) The current page and previous page
c) URL of previous entry and previous page
d) The current page and URL of the entry you are working with
View Answer
Answer: d
Explanation: Your function should look like this:
1. function adminLinks($page, $url)
2. {
3. //Build admin links here
4. }
2. Once your application can generate administrative links, you need to load those links
into _________
a) php.ini
b) index.ini
c) index.php
d) start.php
View Ansewr
Answer: c
Explanation: You place your administrative links only on the full display of an entry, so
you must place the call to load information from adminLinks() within a conditional
statement
3. The URLs in the administrative links won’t mean anything to admin.php unless you
modify _________
a) .htaccess
b) .adminaccess
c) .htmlaccess
d) .urlaccess
View answer
Answer: a
Explanation: You need to modify .htaccess with an additional rule that handles URLs
passed in a link to admin.php.
4. The (|/) tells the server to match ___________
a) nothing
b) forward slash
c) backward slash
d) either nothing or a forward slash
View Answer
Answer: d
Explanation: The vertical pipe character (|) is the regular expression equivalent of “or”.
advertisement
9. Your confirmation form submits your choice, via the _______ method, to ________
a) GET index.php
b) GET admin.php
c) POST index.php
d) POST admin.php
View Answer
Answer: d
Explanation: To process this, you need to add an additional block of code to the top of
admin.php that determines what choices you’ve made and act accordingly.
10. When a user confirms that he wishes to delete an entry, that entry’s URL is passed
to a function which removes the entry from the __________
a) index.php
b) function.inc.php
c) database
d) admin.php
View Answer
Answer: c
Explanation: If the function is successful, you send the user to the main page. If it fails,
you stop the execution of the script and display an error, letting the user know that
something went wrong.
1. Before you can start processing images with PHP, you must first add the ability to
upload images to your administrative form on ________
a) .htaccess
b) function.inc.php
c) index.php
d) admin.php
View Answer
Answer: d
Explanation: To do this, you’ need to add a file upload input to your administrative form.
2. When you’re uploading files you need to set the enctype of the form to __________
a) text
b) text/file
c) multipart/form-data
d) multimedia/form-data
View Answer
Answer: c
Explanation: Set the enctype of the form to multipart/form-data, which can accept files
and standard form values.
3. To check whether a file was uploaded, you look in the _______ superglobal array.
a) $_FILES
b) $_DOCS
c) $_DOCUMENTS
d) $_FOLDERS
View Answer
Answer: a
Explanation: Whenever a file is uploaded via an HTML form, that file is stored in
temporary memory and information about the file is passed in the $_FILES superglobal.
4. To make the ImageHandler class portable you should create a separate file for it
called __________
a) imagehandler.inc.php
b) images.inc.php
c) handler.inc.php
d) imghandler.inc.php
View Answer
Answer: b
Explanation: You save this file in the inc folder (full path:
/xampp/htdocs/simple_blog/inc/images.inc.php).
5. DocBlocks are indicated by opening a comment using _________
a) /*
b) //*
c) /**
d) /*/
View Answer
Answer: c
Explanation: This is a special comment that provides information about a class, property,
or method.
advertisement
6. To process the file, you need to break the array from $_FILES into individual values.
You can do this using the ________ function.
a) divide()
b) list()
c) break()
d) indi()
View Answer
Answer: b
Explanation: The list() function allows you to create named variables for each array
index as a comma-separated list.
7. Before you try to process the file, you need to make sure that your $err value is
equivalent to _________
a) UPLOAD_ERR_OK
b) UPLOAD_NO_ERR
c) UPLOAD_ERR_NO_OK
d) UPLOAD_ERR
View Answer
Answer: a
Explanation: When you’re dealing with files uploaded through an HTML form, you have
access to a special constant called UPLOAD_ERR_OK that tells you whether a file
uploaded successfully.
8. You use the $_SERVER superglobal and your _______ property to create your path
to check.
a) $load_dir
b) $load
c) $save
d) $save_dir
View Answer
Answer: d
Explanation: // Determines the path to check
$path = $_SERVER['DOCUMENT_ROOT'] . $this->save_dir;
9. Which function do you have to use to check whether the $path you’ve stored exists?
a) path_dir()
b) path()
c) is_dir()
d) path_dir()
View Answer
Answer: c
Explanation: If the path exists, is_dir() returns TRUE; otherwise, it returns FALSE.
10. Which one of the following is true about the following line – $obj = new
ImageHandler(‘/images/’, array(400, 300));?
a) This snippet sets the maximum dimensions allowed to 400 pixels wide by 300 pixels
high
b) This snippet sets the minimum dimensions allowed to 300 pixels wide by 400 pixels
high
c) This snippet sets the minimum dimensions allowed to 400 pixels wide by 300 pixels
high
d) This snippet sets the maximum dimensions allowed to 300 pixels wide by 400 pixels
high
View Answer
Answer: a
Explanation: If you needed to change the size of your images, you can change the
dimensions using the above instantiation of ImageHandler.
1. Which version of PHP introduced the static keyword?
a) PHP 4
b) PHP 5
c) PHP 5.2
d) PHP 5.3
View Answer
Answer: b
Explanation: The static keyword was introduced with PHP 5. It cannot be used in PHP 4
scripts.
2. Which keyword is used to access a static method or property from within the same
class(rather than from child)?
a) static
b) strat
c) self
d) set
View Answer
Answer: c
Explanation: Self is to classes what the $this pseudo-variable is to objects.
3. In which of the following circumstance should you use a static reference to a non
static method?
a) Making a method call using parent
b) Making a method call using child
c) Making an object call using parent
d) Making an object call using child
View Answer
Answer: a
Explanation: Making a method call using parent is the only circumstance in which you
should use a static reference to a non-static method.
4. Which one of the following variable cannot be used inside a static method?
a) $this
b) $get
c) $set
d) $date
View Answer
Answer: a
Explanation: By definition, static methods are not invoked in the context of an object. For
this reason, static methods and properties are often referred to as class variables and
properties.
5. What does PDO stand for?
a) PHP Data Orientation
b) PHP Database Object
c) PHP Database Orientation
d) PHP Data Object
View Answer
Answer: d
Explanation: The PDO class provides a common interface to different database
applications.
advertisement
6. Which version of PHP allows you to define constant properties within a class?
a) PHP 4
b) PHP 4.1
c) PHP 4.3
d) PHP 5
View Answer
Answer: d
Explanation: None.
7. Which keyword is used to declare a constant property?
a) const
b) con
c) constant
d) _constant
View Answer
Answer: a
Explanation: A constant property is declared with the const keyword. Like global
constants, class constants cannot be changed once they are set.
8. Which one of the following is a constant variable?
a) const $name
b) const $NAME
c) constant NAME
d) const NAME
View Answer
Answer: d
Explanation: Constants are not prefixed with a dollar sign like regular properties. By
convention, they are often named using only uppercase characters
9. What will happen if you try to set a value to a constant once it has been declared?
a) The value of the variable will change
b) The value of the variable will not change
c) Parse Error
d) Nothing
View Answer
Answer: c
Explanation: You should use constants when your property needs to be available across
all instances of a class, and when the property value needs to be fixed and unchanging.
10. How many of the following can be contained in constants?
i) boolean
ii) integer
iii) float
iv) string
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: d
Explanation: All scalar data i.e boolean, integer, float and string can be contained in
constants.
1. Which one of the following class can not be instantiated?
a) inherited class
b) abstract class
c) constant class
d) every class
View Answer
Answer: b
Explanation: An abstract class cannot be instantiated. Instead, it defines (and, optionally,
partially implements) the interface for any class that might extend it.
2. Which one of the following keywords are used to define an abstract class?
a) extends
b) implements
c) abstract
d) new
View Answer
Answer: c
Explanation: The introduction of abstract classes was one of the major changes ushered
in with PHP 5. Its inclusion in the list of new features was another sign of PHP’s
extended commitment to object-oriented design.
3. Which one of the following is the correct abstract method?
a) public function write()
b) abstract function write()
c) abstract public write();
d) abstract public function write();
View Answer
Answer: d
Explanation: An abstract method cannot have an implementation. You declare it in the
normal way, but end the declaration with a semicolon rather than a method body.
4. At least how many abstract methods must an abstract class contain?
a) None
b) One
c) Two
d) Five
View Answer
Answer: b
Explanation: Classes defined as abstract may not be instantiated, and any class that
contains at least one abstract method must also be abstract.
5. Which one of the following keyword is used to implement an interface?
a) interface
b) get
c) inherit
d) implements
View Answer
Answer: d
Explanation: A class can implement an interface using the implements keyword in its
declaration.
advertisement
6. Which version of PHP introduced the concept called late static binding?
a) PHP 4
b) PHP 5
c) PHP 5.1
d) PHP 5.3
View Answer
Answer: d
Explanation: None.
7. Which one of the following methods in the exception class, is used to get a nested
exception object?
a) getPrevious()
b) getCode()
c) getFile()
d) getLine()
View Answer
Answer: a
Explanation: getCode() – Get the code integer that was passed to the constructor.
getFile() – Get the file in which the exception was generated. getLine() – Get the line
number at which the exception was generated.
8. Which one of the following keyword is used in conjunction with an Exception object?
a) throws
b) exception
c) throw
d) final
View Answer
Answer: c
Explanation: The throw keyword is used in conjunction with an Exception object. It halts
the execution of the current method and passes responsibility for handling the error back
to the calling code.
9. Which keyword is used to put a stop on inheritance?
a) stop
b) end
c) break
d) final
View Answer
Answer: d
Explanation: A final class cannot be subclassed.
10. PHP provides built-in interceptor methods, which can intercept messages sent to
undefined methods and properties. This is also known as _________
a) overloading
b) overriding
c) overbending
d) overbinding
View Answer
Answer: a
Explanation: None.
1. Which one of the following method is invoked when a value is assigned to an
undefined property?
a) __get()
b) __set()
c) __isset()
d) __call()
View Answer
Answer: b
Explanation: The __set() method is invoked when client code attempts to assign to an
undefined property. It is passed two arguments: the name of the property, and the value
the client is attempting to set.
2. Which one of the following method is invoked when an undefined method is called by
client code?
a) __get()
b) __isset()
c) __unset()
d) __call()
View Answer
Answer: d
Explanation: The __call() method is probably the most useful of all the interceptor
methods. The __call() method can be useful for delegation. Delegation is the
mechanism by which one object passes method invocations on to a second.
3. Which method introduced in PHP 5, is invoked just before an object is a garbage
collected?
a) __collect()
b) __garbage()
c) __destruct()
d) __destructor()
View Answer
Answer: c
Explanation: You can use this method to perform any final cleaning up that might be
necessary. Imagine, for example, a class that saves itself to a database when so
ordered. I could use the __destruct() method to ensure that an instance saves its data
when it is deleted.
4. Which one of the following PHP statements is true?
1. class CopyMe {}
2. $first = new CopyMe();
3. $second = $first;
5. Which keyword must be added before $first variable on the third line of the above
question to make $second and $first as distinct objects in PHP 5?
a) copy
b) clone
c) cut
d) Can’t add any word to make them distinct
View Answer
Answer: b
Explanation: Clone operates on an object instance, producing a by-value copy.
6. What will be the output of the following PHP code? (Before the version PHP 5.2)
1. class StringThing {}
2. $st = new StringThing();
3. print $st;
1. class Person
2. {
3. function getName() { return "Bob"; }
4. function getAge() { return 44; }
5. function __toString() {
6. $desc = $this->getName();
7. $desc .= " (age ".$this->getAge().")";
8. return $desc;
9. }
10. }
11. $person = new Person();
12. print $person;
1. class Checkout
2. {
3. final function totalize()
4. {
5. // calculate bill
6. }
7. }
8.
9. class IllegalCheckout extends Checkout
10. {
11. final function totalize()
12. {
13. // change bill calculation
14. }
15. }
a) PHP Fatal error: Class IllegalCheckout may not inherit from final class
b) Value of the bill calculated
c) PHP Fatal error: Cannot find object
d) PHP Fatal error: Cannot override final method
View Answer
Answer: d
Explanation: A final class cannot be subclassed. Less drastically, a final method cannot
be overridden.
1. A package is a set of related _________
a) Objects
b) Classes
c) Programs
d) Functions
View Answer
Answer: b
Explanation: A package is a set of related classes, usually grouped together in some
way. Packages can be used to separate parts of a system from one another.
2. Till which version of PHP, developers were forced to name their files in a global
context?
a) PHP 4
b) PHP 5
c) PHP 5.2
d) PHP 5.3
View Answer
Answer: d
Explanation: If you named a class ShoppingBasket, it would become instantly available
across your system.
3. Which of the following can you place inside a namespace?
i) classes
ii) functions
iii) variables
a) i)
b) ii)
c) iii)
d) i), ii) & iii)
View Answer
Answer: d
Explanation: A namespace is a bucket in which you can place your classes, functions
and variables.
4. Which one of the following is the correct way of declaring a namespace?
a) namespace my;
b) namespace my();
c) my namespace;
d) namespace(my);
View Answer
Answer: a
Explanation: The namespace declaration must be the first statement in its file.
advertisement
namespace main;
com\getinstance\util\Debug::helloWorld()
PHP Fatal error: Class 'main\com\getinstance\util\Debug' not found in ...
Using which one of the following PHP lines will the error be removed?
a) \\com\getinstance\util\Debug::helloWorld();
b) getinstance\util\Debug::helloWorld();
c) main.com\getinstance\util\Debug::helloWorld();
d) \com\getinstance\util\Debug::helloWorld();
View Answer
Answer: d
Explanation: PHP is looking below the namespace main for com\getinstance\util and not
finding it. That’s because we are using a relative namespace here. Just as you can
make absolute URLs and filepaths by starting off with a separator so you can with
namespaces.
7. Which keyword can be used to fix the following PHP error?
namespace main;
com\getinstance\util\Debug::helloWorld()
PHP Fatal error: Class 'main\com\getinstance\util\Debug' not found in ...
a) fix
b) join
c) use
d) namespace
View Answer
Answer: c
Explanation: Use keyword allows you to alias other namespaces within the current
namespace.
Example – namespace main;
use com\getinstance\util;
util\Debug::helloWorld();
8. If I already had a Debug class in the main namespace. What will be the output of the
following PHP code?
1. namespace main;
2. use com\getinstance\util\Debug;
3.
4. class Debug {
5. static function helloWorld() {
6. print "hello from main\Debug";
7. }
8. }
9.
10. Debug::helloWorld();
a) error
b) hello from main
c) hello from main\Debug
d) debug
View Answer
Answer: a
Explanation: PHP Fatal error: Cannot declare class main\Debug because the name is
already in use.
9. Which one of the following statements is true for include_once() and require_once()?
a) Both are exactly the same
b) include_once is used for files where as require_once() is not
c) Both Handle the errors in the same way
d) Both do not handle the errors in the same way
View Answer
Answer: d
Explanation: The only difference between the include() and require() statements lies in
their handling of errors. A file invoked using require() will bring down your entire process
when you meet an error. The same error encountered via a call to include() will merely
generate a warning and end execution of the included file.
10. Which one of the following statements is true for require() and require_once()?
a) They are functions
b) They are statements
c) They’ll not work if the () is not present
d) They can not be used to require files
View Answer
Answer: b
Explanation: require() and require_once() are actually statements, not functions. This
means that you can omit the brackets when using them.
1. Which function was introduced to help automate the inclusion of class files?
a) __load()
b) __preload()
c) __autoload()
d) __inload()
View Answer
Answer: c
Explanation: When the PHP engine encounters an attempt to instantiate an unknown
class, it invokes the __autoload() function, passing it the class name as a string. It is up
to the implementer to define a strategy for locating and including the missing class file.
2. How many times can you define _________ autoload in a process?
a) once
b) twice
c) thrice
d) as many times as needed
View Answer
Answer: a
Explanation: __autoload is a powerful tool, but it does have some limitations. In
particular, you can only define it once in a process. If you need to change your autoload
function dynamically you should look at the spl_autoload_register function, which
supports that functionality.
3. Which one of the following functions will you use to check that the class exists before
you work with it?
a) class_exist()
b) class_exists()
c) exist()
d) exists_class()
View Answer
Answer: b
Explanation: The class_exists() function accepts a string representing the class to check
for and returns a Boolean true value if the class exists and false otherwise.
4. Which one of the following will you use to check the class of an object?
a) class()
b) _class()
c) class_check()
d) get_class()
View Answer
Answer: d
Explanation: This function accepts any object as an argument and returns its class name
as a string.
5. PHP 4 did not support instanceof. Instead, which function did it provide?
a) is()
b) get_class()
c) is_a()
d) is_the()
View Answer
Answer: c
Explanation: As of PHP 5.3 is_a() no longer deprecated.
advertisement
6. You use the get_class_methods() function to return the names of all the methods in
the class. Which function will you use to print it on the screen?
a) printf()
b) print_ar
c) print_r
d) echo
View Answer
Answer: c
Explanation: The function get_class_methods returns an array therefore we cannot use
echo.
7. If you call a method and it doesn’t exist it’ll cause a problem. To check the method
which function will you use?
a) _method()
b) methodexists()
c) is_callable()
d) is_method()
View Answer
Answer: c
Explanation: There is another method which you can use – method_exists() for similar
purpose.
8. What will be the output if a protected method is given as the argument to the function
method_exist()?
a) Method does not exist
b) False
c) Error
d) True
View Answer
Answer: d
Explanation: method_exists() returns true for private and protected methods as well as
for public ones. Remember that the fact that a method exists does not mean that it will
be callable.
9. Which one of the following function should I use to find the parent class of a class?
a) get_parent_class()
b) parent_class()
c) class_parent()
d) get_class_parent()
View Answer
Answer: a
Explanation: The class functions also allow us to chart inheritance relationships.This
function requires either an object or a class name, and it returns the name of the
superclass, if any. If no such class exists, that is, if the class we are testing does not
have a parent, then the function returns false.
10. Which class accepts a class name or an object reference and returns an array of
interface name?
a) class_implements()
b) is_subclass()
c) is_subclass_of()
d) class_interface()
View Answer
Answer: a
Explanation: is_subclass_of() will tell you only about class inheritance relationships. It
will not tell you that a class implements an interface.
1. Object-oriented code tries to minimize dependencies by moving responsibility for
handling tasks away from ___ and toward the objects in the system.
a) server code
b) client code
c) machine code
d) procedural code
View Answer
Answer: b
Explanation: Procedural code takes the form of a sequential series of commands and
method calls. The controlling code tends to take responsibility for handling differing
conditions. This top-down control can result in the development of duplications and
dependencies across a project. Object-oriented code tries to minimize these
dependencies by moving responsibility for handling tasks away from client code and
toward the objects in the system.
2. Placing a static method for generating ________ objects in the ____________ class
is convenient.
a) child parent
b) parent child
c) final static
d) static final
View Answer
Answer: a
Explanation: Such a design decision does have its own consequences, however.
3. The extent to which proximate procedures are related to one another is known as
________
a) Coupling
b) Balancing
c) Cohesion
d) Co-relation
View Answer
Answer: c
Explanation: Ideally, you should create components that share a clear responsibility. If
your code spreads related routines widely, you will find them harder to maintain as you
have to hunt around to make changes. Our Param Handler classes collect related
procedures into a common context.
4. ______ occurs when discrete parts of a system’s code are tightly bound up with one
another so that a change in one part necessitates changes in the others.
a) Loose Coupling
b) Tight Coupling
c) Co-relation
d) Balancing
View Answer
Answer: b
Explanation: Tight coupling is by no means unique to procedural code, though the
sequential nature of such code makes it prone to the problem.
5. ________ code makes change easier because the impact of altering an
implementation will be localized to the component being altered.
a) Orthogonal
b) Cohesion
c) Coupling
d) Balancing
View Answer
Answer: a
Explanation: Orthogonality, it is argued, promotes reuse in that components can be
plugged into new systems without needing any special configuration. Such components
will have clear inputs and outputs independent of any wider context.
advertisement
1. <?php
2. "Hello World"
3. ?>
a) Error
b) Hello World
c) Nothing
d) Missing semicolon error
View Answer
Answer: c
Explanation: If you need to output something onto the screen you’ll need to use echo or
print_r.
2. What will be the output of the following PHP code?
1. <?php
2. print_r "Hello world"
3. ?>
a) Error
b) Hello World
c) Nothing
d) Missing semicolon error
View Answer
Answer: a
Explanation: The statement should be print_r(‘Hello World’) to print Hello world. Also if
there is only one line then there is no requirement of a semicolon, but it is better to use
it.
3. What will be the output of the following PHP code?
advertisement
1. <?php
2. echo 'Hello World';
3. <html>
4. Hello world
5. </html>
6. ?>
a) Hello world
b) Hello World Hello World
c)
Hello world
Hello World
d) Syntax Error
View Answer
Answer: d
Explanation: Parse error: syntax error, unexpected ‘<‘ on line 2. You can not use the
html tag inside php tags.
4. What will be the output of the following PHP code?
1. <?php
2. Echo "Hello World1";
3. echo " Hello world2";
4. ECHO " Hello world3";
5. ?>
Hello world1
Hello world2
Hello World3
c) Error
d) Hello world1 Hello world3
View Answer
Answer: a
Explanation: In PHP, all user-defined functions, classes, and keywords (e.g. if, else,
while, echo, etc.) are case-insensitive.
5. What will be the output of the following PHP code?
1. <?php
2. $color = "red";
3. echo "$color";
4. echo "$COLOR";
5. echo "$Color";
6. ?>
a) redredred
b) redred
c) red
d) Error
View Answer
Answer: c
Explanation: In PHP, all variables are case-sensitive.
6. What will be the output of the following PHP code?
1. <?php
2. # echo "Hello world";
3. echo "# Hello world";
4. ?>
a) # Hello world
b) Hello world# Hello world
c) Hello world
d) Error
View Answer
Answer: a
Explanation: # is a single line comment.
7. What will be the output of the following PHP code?
1. <?php
2. echo "<i>Hello World</i>"
3. ?>
a) Hello world
b) Hello world in italics
c) Nothing
d) Error
View Answer
Answer: b
Explanation: You can use tags like italics, bold etc. inside php script.
8. What will be the output of the following PHP code?
1. <?php
2. echo "echo "Hello World"";
3. ?>
a) Hello world
b) echo “Hello world”
c) echo Hello world
d) Error
View Answer
Answer: d
Explanation: It would have printed echo “Hello world” if we have put backslash
doublequotes just before and just after Hello World string.
9. What will be the output of the following PHP code?
1. <?php
2. <?php
3. echo "Hello world";
4. ?>
5. ?>
a) HELLO WORLD
b) Hello world
c) Nothing
d) Error
View Answer
Answer: d
Explanation: You can not have php tags inside a php tag.
10. What will be the output of the following PHP code?
1. <?php
2. $color = red;
3. echo "\$color";
4. ?>
a) red
b) $color
c) \red
d) Error
View Answer
Answer: b
Explanation: To print red remove the \.
1. What will be the output of the following PHP code?
1. <?php
2. /*
3. echo "Hello world";
4. */
5. ?>
a) Hello world
b) Nothing
c) Error
d)
/*
Hello world
*/
View Answer
Answer: b
Explanation: /* */ is used for commenting multiple lines.
1. <?php
2. $color = red;
3. echo "$color" . red ;
4. ?>
a) red red
b) red
c) error
d) nothing
View Answer
Answer: c
Explanation: Use of undefined constant red.
3. What will be the output of the following PHP code?
advertisement
1. <?php
2. $color1 = red;
3. $color2 = green;
4. echo "$color1"."$color2";
5. ?>
a) red green
b) red
c) green
d) error
View Answer
Answer: d
Explanation: It has to be $color1 = “red”; and $color2 = “green”; therefore the error.
4. What will be the output of the following PHP code?
1. <?php
2. $color = "red";
3. $color = "green";
4. echo "$color";
5. ?>
a) red
b) green
c) red green
d) error
View Answer
Answer: b
Explanation: The variable contains the last value which has been assigned.
5. What will be the output of the following PHP code?
1. <?php
2. $color1 = "red";
3. $color2 = "green";
4. echo "$color1" . "$color2";
5. ?>
a) red
b) green
c) red green
d) redgreen
View Answer
Answer: d
Explanation: The . operator is used to join to strings.
6. What will be the output of the following PHP code?
1. <?php
2. $color1 = "red";
3. $color2 = "green";
4. echo "$color1" + "$color2";
5. ?>
a) redgreen
b) red green
c) 0
d) error
View Answer
Answer: c
Explanation: + operator does not join both the strings.
7. What will be the output of the following PHP code?
1. <?php
2. $color1 = "red";
3. $color2 = "red";
4. echo "$color1" + "$color2";
5. ?>
a) redgreen
b) red green
c) 0
d) 1
View Answer
Answer: c
Explanation: + does not return 1 if the variables are equal.
8. What will be the output of the following PHP code?
1. <?php
2. $color1 = "red";
3. $color2 = "1";
4. echo "$color1" + "$color2";
5. ?>
a) red1
b) red 1
c) 0
d) 1
View Answer
Answer: d
Explanation: + just returns the numeric value even though it is inside double quotes.
9. What will be the output of the following PHP code?
1. <?php
2. $color1 = "1";
3. $color2 = "1";
4. echo "$color1" + "$color2";
5. ?>
a) 11
b) 2
c) 0
d) 1
View Answer
Answer: b
Explanation: + can be used to add to integer values which are enclosed by double-
quotes.
10. What will be the output of the following PHP code?
1. <?php
2. $color1 = "red";
3. $color2 = "1";
4. $color3 = "grey"
5. echo "$color1" + "$color2" . "$color3";
6. ?>
a) 1grey
b) grey
c) 0
d) red1grey
View Answer
Answer: a
Explanation: + gives the value 1 and . is used to give join 1 and grey.
. What will be the output of the following PHP code?
1. <?php
2. $x = 5;
3. $y = 10;
4. $z = "$x + $y";
5. echo "$z";
6. ?>
a) 15
b) 10 + 5
c) $z
d) $x + $y
View Answer
Answer: b
Explanation: Variable z will store 10 + 5 because 10 + 5 is given in double-quotes.
2. What will be the output of the following PHP code?
1. <?php
2. $x = 4;
3. $y = 3;
4. $z = 1;
5. echo "$x = $x + $y + $z";
6. ?>
a) 4 = 4 + 3 + 1
b) 8
c) 8 = 4 + 3 +1
d) Error
View Answer
Answer: a
Explanation: Again since the variables are inside double quotes we get this result.
3. What will be the output of the following PHP code?
advertisement
1. <?php
2. $x = 4;
3. $y = 3
4. $z = 1;
5. $z = $z + $x + $y;
6. echo "$z";
7. ?>
a) $z
b) 15
c) 8
d) 1
View Answer
Answer: c
Explanation: Normal addition of variables x, y and z occurs and result of 8 will be
displayed.
4. What will be the output of the following PHP code?
1. <?php
2. $x = 3.3;
3. $y = 2;
4. echo $x % $y;
5. ?>
a) 0
b) 1
c) 2
d) Error
View Answer
Answer: b
Explanation: % is the modulo operator. Unlike in C we can use it get reminder or floating
point numbers in PHP.
5. What will be the output of the following PHP code?
1. <?php
2. $x = 10;
3. $y = 4;
4. $z = 3;
5. echo $x % $y % $z;
6. ?>
a) 0
b) 1
c) 2
d) Error
View Answer
Answer: c
Explanation: The expression is considered as ($x%$y)%z in this case (10%4)%3 which
is 2.
6. What will be the output of the following PHP code?
1. <?php
2. $x = 10;
3. $y = 4;
4. $z = 3;
5. echo ($x % ($y) + $z);
6. ?>
a) 5
b) 3
c) 0
d) 1
View Answer
Answer: a
Explanation: The innermost bracket is evaluated first, since it covers only variable y it is
as good as not using brackets.
7. What will be the output of the following PHP code?
1. <?php
2. $x = 30;
3. $y = 20;
4. $z = 10;
5. echo $x + $y - $z / ($z - $y);
6. ?>
a) 41
b) -4
c) -5
d) 51
View Answer
Answer: d
Explanation: First ($z – $y) is evaluated then -$z/($z – $y) is evaluated this results in 1
which is added to $x + $y therefore we get 51.
8. What will be the output of the following PHP code?
1. <?php
2. $x = -1;
3. $y = 1;
4. $z = $x * $y + $z;
5. echo $z;
6. ?>
a) Undefined variable z
b) -1
c)
Undefined variable z
-1
1. <?php
2. $x = 4;
3. $y = -3;
4. $z = 11;
5. echo 4 + $y * $z / $x;
6. ?>
a) 4.25
b) 3.25
c) -3.25
d) -4.25
View Answer
Answer: d
Explanation: First the * is evaluated then / followed by + therefore we can rewrite this
expression as 4 +((- 3 * 11) / 4) which results in -4.25.
10. What will be the output of the following PHP code?
1. <?php
2. $x = 3.5;
3. $y = 2;
4. $z = 2;
5. echo $x / $y / $z;
6. ?>
a) 1.75
b) 0.875
c) 3.5
d) Error
View Answer
Answer: b
Explanation: First $x / $y is evaluated then this is divided by $z therefore we get 0.875.
1. What will be the output of the following PHP code?
1. <?php
2. one = 1;
3. two = 2;
4. three = 3;
5. four = 4;
6. echo "one / two + three / four";
7. ?>
a) 0.75
b) 0.05
c) 1.25
d) Error
View Answer
Answer: d
Explanation: Variables should start with a $ symbol, since one, two, three, four don’t
begin with $ symbol we’ll get an error.
2. What will be the output of the following PHP code?
1. <?php
2. $on$e = 1;
3. $tw$o = 2;
4. $thre$e = 3;
5. $fou$r = 4;
6. echo "$on$e / $tw$o + $thre$e / $fou$r";
7. ?>
a) 0.75
b) 0.05
c) 1.25
d) Error
View Answer
Answer: d
Explanation: You can not use the $ in between the variable name.
3. What will be the output of the following PHP code?
advertisement
1. <?php
2. $on_e = 1;
3. $tw_o = 2;
4. $thre_e = 3;
5. $fou_r = 4;
6. echo $on_e / $tw_o + $thre_e / $fou_r;
7. ?>
a) 0.75
b) 0.05
c) 1.25
d) Error
View Answer
Answer: c
Explanation: You can use _ in a variable name.
4. What will be the output of the following PHP code?
1. <?php
2. $On_e = 1;
3. $tw_o = 2;
4. $thre_e = 3;
5. $fou_r = 4;
6. echo $on_e / $tw_o + $thre_e / $fou_r;
7. ?>
a) 0.75
b) 0.05
c) 1.25
d) Error
View Answer
Answer: a
Explanation: Since the variable initialised is $On_e and the variable in the echo
statement is $on_e the echo statement treats $on_e as 0;
5. What will be the output of the following PHP code?
1. <?php
2. echo $red;
3. ?>
a) 0
b) Nothing
c) True
d) Error
View Answer
Answer: b
Explanation: There will no output returned as the variable $red does not hold any value.
6. What will be the output of the following PHP code?
1. <?php
2. $four4 = 4;
3. $three3 = 3;
4. $two2 = 2;
5. echo $four4 + $three3 / $two2 - 1;
6. ?>
a) 4.5
b) 7
c) 3.5
d) Error
View Answer
Answer: a
Explanation: You can use numbers in a variable name.
7. What will be the output of the following PHP code?
1. <?php
2. $4four = 4;
3. $3three = 3;
4. $2two = 2;
5. echo $4four + $3three / $2two - 1;
6. ?>
a) 4.5
b) 7
c) 3.5
d) Error
View Answer
Answer: d
Explanation: A variable name can not start with a numeric value.
8. What will be the output of the following PHP code?
1. <?php
2. int $one = 1;
3. echo "$one";
4. ?>
a) 0
b) 1
c) $one
d) Error
View Answer
Answer: d
Explanation: Unlike other programming languages there are no data types in PHP.
9. What will be the output of the following PHP code?
1. <?php
2. var $one = 1;
3. var $two = 2;
4. echo $one / $two * $one / $two * $two;
5. ?>
a) 1
b) 0
c) 0.5
d) Error
View Answer
Answer: d
Explanation: You can not use var before a variable name.
10. What will be the output of the following PHP code?
1. <?php
2. $hello = "Hello World";
3. $bye = "Bye";
4. echo $hello;"$bye";
5. ?>
a) Hello World
b) Bye
c) Hello worldBye
d) Error
View Answer
Answer: a
Explanation: Since there is a semi-colon in between $hello and $bye, the line ends at
$hello. However $bye would have printed if a echo was present before “$bye”.
1. What will be the output of the following PHP code?
1. <?php
2. $x;
3. echo "$x";
4. ?>
a) 0
b) 1
c) Nothing
d) Error
View Answer
Answer: c
Explanation: Since the variable x is not initialised it is not storing any value, therefore
nothing will be printed on the screen.
2. What will be the output of the following PHP code?
1. <?php
2. $x = 5;
3. {
4. $x = 10;
5. echo "$x";
6. }
7. echo "$x";
8. ?>
a) 1010
b) 105
c) 510
d) error
View Answer
Answer: a
Explanation: Variable x stores the value 10 and not 5.
3. What will be the output of the following PHP code?
advertisement
1. <?php
2. $x = 5;
3. {
4. echo "$x";
5. }
6. ?>
a) 0
b) 5
c) Nothing
d) Error
View Answer
Answer: b
Explanation: The variable x stores the value 5 and therefore the value 5 is printed on the
screen.
4. What will be the output of the following PHP code?
1. <?php
2. $x = 5;
3. function fun()
4. {
5. echo "$x";
6. }
7. fun();
8. ?>
a) 0
b) 5
c) Nothing
d) Error
View Answer
Answer: c
Explanation: The variable x is not defined inside the function fun(), therefore nothing is
printed on the screen.
5. What will be the output of the following PHP code?
1. <?php
2. $x = 5;
3. function fun()
4. {
5. $x = 10;
6. echo "$x";
7. }
8. fun();
9. echo "$x";
10. ?>
a) 0
b) 105
c) 510
d) Error
View Answer
Answer: b
Explanation: First when the function is called variable x is initialised to 10 so 10 is printed
later the global value 5 is printed.
6. What will be the output of the following PHP code?
1. <?php
2. $x = 4;
3. $y = 3;
4. function fun($x = 3, $y = 4)
5. {
6. $z = $x+$y/$y+$x;
7. echo "$z";
8. }
9. echo $x;
10. echo $y;
11. echo $z;
12. fun($x, $y);
13. ?>
a) 43
b) 943
c) 349
d) 439
View Answer
Answer: d
Explanation: Firstly, the statements outside the function are printed, since z is not
defined it’ll no value is printed for z. Next the function is called and the value of z inside
the function is printed.
7. What will be the output of the following PHP code?
1. <?php
2. $x = 4;
3. $y = 3;
4. function fun($x, $y)
5. {
6. $z = $x + $y / $y + $x;
7. echo "$z";
8. }
9. echo $x;
10. echo $y;
11. echo $z;
12. fun(3, 4);
13. ?>
a) 437
b) 439
c) 349
d) 347
View Answer
Answer: a
Explanation: It is same as above but the value passed into the function is 3,4 and not
4,3. Therefore the difference in answer.
8. What will be the output of the following PHP code?
1. <?php
2. function fun($x,$y)
3. {
4. $x = 4;
5. $y = 3;
6. $z = $x + $y / $y + $x;
7. echo "$z";
8. }
9. fun(3, 4);
10. ?>
a) 7
b) 9
c) 0
d) Error
View Answer
Answer: b
Explanation: Value 3, 4 is passed to the function but that is lost because x and y are
initialised to 4 and 3 inside the function. Therefore we get the given result.
9. What will be the output of the following PHP code?
1. <?php
2. $x = 3, 4, 5, 6;
3. echo "$x";
4. ?>
a) 3
b) 4
c) 6
d) Error
View Answer
Answer: d
Explanation: In C you won’t get an error but in PHP you’ll get a syntax error.
10. What will be the output of the following PHP code?
1. <?php
2. $a = 10;
3. $b = 4;
4. $c = fun(10,4);
5. function fun($a,$b)
6. {
7. $b = 3;
8. return $a - $b + $b - $a;
9. }
10. echo $a;
11. echo $b;
12. echo $c;
13. ?>
a) 104
b) 410
c) 1400
d) 4100
View Answer
Answer: c
Explanation: The value returned from the function is 0, and value of a is 10, value of b is
4 and c is 0.
1. What will be the output of the following PHP code?
1. <?php
2. $a = "$winner";
3. $b = "/$looser";
4. echo $a,$b;
5. ?>
a) $winner/$looser
b) /$looser
c) /
d) $looser
View Answer
Answer: c
Explanation: Since variables $winner and $looser is not defined we only see / as output.
2. What will be the output of the following PHP code?
1. <?php
2. $a = "$winner";
3. $b = "\$looser";
4. echo $a, $b;
5. ?>
a) $winner\$looser
b) \$looser
c) \
d) $looser
View Answer
Answer: d
Explanation: As there is a backslash before $ it takes it as a string and not a variable
therefore we get $looser as the output.
3. What will be the output of the following PHP code?
advertisement
1. <?php
2. $a = "$winner";
3. $b = "\\$looser";
4. echo $a, $b;
5. ?>
a) $winner\\$looser
b) \\$looser
c) \
d) $looser
View Answer
Answer: c
Explanation: Since two backslashes are used together, a single backslash is printed on
the screen and as $looser is not initialised only single backslash is printed.
4. What will be the output of the following PHP code?
1. <?php
2. $x = 5;
3. $y = 10;
4. function fun()
5. {
6. $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
7. }
8. fun();
9. echo $y;
10. ?>
a) 5
b) 10
c) 15
d) Error
View Answer
Answer: c
Explanation: You can access the global variable using
$GLOBALS[‘globalvariablename’].
5. What will be the output of the following PHP code?
1. <?php
2. $x = 5;
3. $y = 10;
4. function fun()
5. {
6. $y = $GLOBALS['x'] + $GLOBALS['y'];
7. }
8. fun();
9. echo $y;
10. ?>
a) 5
b) 10
c) 15
d) Error
View Answer
Answer: b
Explanation: The value of global variable y does not change therefore it’ll print 10;
6. What will be the output of the following PHP code?
1. <?php
2. function fun()
3. {
4. $x = 0;
5. echo $x;
6. $x++;
7. }
8. fun();
9. fun();
10. fun();
11. ?>
a) 012
b) 123
c) 000
d) 111
View Answer
Answer: c
Explanation: Every time the function is called the value of x becomes 0, therefore we get
0 on every function call.
7. What will be the output of the following PHP code?
1. <?php
2. function fun()
3. {
4. static $x = 0;
5. echo $x;
6. $x++;
7. }
8. fun();
9. fun();
10. fun();
11. ?>
a) 012
b) 123
c) 111
d) Error
View Answer
Answer: a
Explanation: When static is used, each time the function is called, that variable will still
have the information it contained from the last time the function was called.
8. What will be the output of the following PHP code?
1. <?php
2. static $x = 0;
3. function fun()
4. {
5. echo $x;
6. $x++;
7. }
8. fun();
9. fun();
10. fun();
11. ?>
a) 012
b) 123
c) Nothing
d) Error
View Answer
Answer: c
Explanation: Since variable x is not defined inside the function fun(), nothing will be
printed.
9. What will be the output of the following PHP code?
1. <?php
2. $x=0;
3. function fun()
4. {
5. echo $GLOBALS['x'];
6. $GLOBALS['x']++;
7. }
8. fun();
9. fun();
10. fun();
11. ?>
a) 000
b) 012
c) 123
d) Error
View Answer
Answer: b
Explanation: Since, we are using $GLOBALS[‘x’].
10. What will be the output of the following PHP code?
1. <?php
2. $x = 0;
3. function fun()
4. {
5. echo $GLOBALS['x'];
6. $x++;
7. }
8. fun();
9. fun();
10. fun();
11. ?>
a) 000
b) 012
c) Nothing
d) Error
View Answer
Answer: a
Explanation: Every time the function is called the value of x becomes 0, therefore we get
0 on every function call.
1. What will be the output of the following PHP code?
1. <?php
2. define("GREETING", "PHP is a scripting language", true);
3. echo GREETING;
4. echo "<br>"
5. echo GREETING;
6. ?>
GREETING
GREEtING
c) GREETING
d)
View Answer
Answer: d
Explanation: Since the third parameter is true in define(“GREETING”, “PHP is a scripting
language”, true) is true GREETING becomes case insensitive.
1. <?php
2. define("GREETING", "PHP is a scripting language");
3. echo $GREETING;
4. ?>
a) $GREETING
b) no output
c) PHP is a scripting language
d) GREETING
View Answer
Answer: b
Explanation: Constants do not need a $ before them, they are referenced by their
variable names itself.
advertisement
volume is gedempt
3. What will be the output of the following PHP code?
1. <?php
2. define('GREETING_TEST', 'PHP is a scripting language', true);
3. echo GREETING_TESt;
4. $changing_variable = 'test';
5. echo constant('GREETING_' . strtoupper($changing_variable));
6. ?>
a)
b) GREETING_TESt
c) PHP is a scripting language
d)
GREETING_TEST
View Answer
Answer: a
Explanation: echo constant(x) output x, and x here is the concatenation of GREETING_
and $changing variable with. operator.
1. <?php
2. class Constants
3. {
4. define('MIN_VALUE', '0.0');
5. define('MAX_VALUE', '1.0');
6. public static function getMinValue()
7. {
8. return self::MIN_VALUE;
9. }
10. public static function getMaxValue()
11. {
12. return self::MAX_VALUE;
13. }
14. }
15. echo Constants::getMinValue();
16. echo Constants::getMaxValue();
17. ?>
a) 0.01.0
b) 01
c) No output
d) ERROR
View Answer
Answer: d
Explanation: In a class constants should be defined const MIN_VALUE = 0.0;const
MAX_VALUE = 1.0; instead.
5. What will be the output of the following PHP code?
1. <?php
2. define("__LINE__", "PHP is a scripting language");
3. echo __LINE__;
4. ?>
1. <?php
2. define('IF', 42);
3. echo "IF: ", IF;
4. ?>
a) IF:42
b) No output
c) IF:
d) ERROR
View Answer
Answer: d
Explanation: Keyword like IF cannot be used as constant names.
7. What will be the output of the following PHP code?
1. <?php
2. define("NEW_GOOD_NAME_CONSTANT", "I have a value");
3. define("OLD_BAD_NAME_CONSTANT", NEW_GOOD_NAME_CONSTANT);
4.
5. echo NEW_GOOD_NAME_CONSTANT;
6. echo OLD_BAD_NAME_CONSTANT;
7. ?>
a) I have a value
b) I have a valueI have a value
c) ERROR
d) I have a valueNEW_GOO_NAME_CONSTANTS
View Answer
Answer: b
Explanation: Constants can be set as values for other constants.
8. What will be the output of the following PHP code?
1. <?php
2. define('GOOD_OCTAL', 0700);
3. define('BAD_OCTAL', 0600);
4. print GOOD_OCTAL;
5. print '<br>';
6. print BAD_OCTAL;
7. ?>
a)
448
384
b)
0700
0800
c) ERROR
d) No output
View Answer
Answer: a
Explanation: Anything starting from 0 is evaluated as an octal.
9. What will be the output of the following PHP code?
1. <?php
2. define("VAR_NAME","test");
3. ${VAR_NAME} = "value";
4. echo VAR_NAME;
5. echo ${VAR_NAME};
6. ?>
a) test
b) testtest
c) testvalue
d) error, constant value cannot be changed
View Answer
Answer: c
Explanation: ${VAR_NAME} creates a new variable which is not same as VAR_NAME.
10. What will be the output of the following PHP code?
1. <?php
2. class myObject { }
3. define('myObject::CONSTANT', 'test');
4. echo myObject::CONSTANT;
5. ?>
a) test
b) error
c) myObject::CONSTANT
d) no output
View Answer
Answer: b
Explanation: Class constants cannot be defined outside class.