Questions On Arrays and Functions
Questions On Arrays and Functions
Questions On Arrays and Functions
a.hello
b.helloblabla
c.Error
d.helloblablablabla
1. Which one of the following is the right way of defining a function in PHP?
Answer:d
Explanation: None.
Answer:b
Explanation: Type hinting gives you the ability to force parameters to be objects of certain class
or to be arrays. PHP 5 introduced this feature.
a) Call By Value
b) Call By Reference
c) Default Argument Value
d) Type Hinting
View Answer
Answer:a
Explanation: When you pass an argument in the above manner or say we pass 15 and 3 directly,
it is called passing by value or call by value.
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.
5. Which of the following are valid function names?
i)function()
ii)€()
iii).function()
iv)$function()
a)Only ii)
b)None of the mentioned.
c) All of the mentioned.
d) iii) and iv)
View Answer
a) I am b
b) I am bI am a
c) Error
d) I am a Error
View Answer
Answer: d
Explanation: This will be the output- I am a Fatal error: Cannot redeclare b()
a) I am b
b) I am bI am a
c) Error
d) I am a Error
View Answer
Answer: c
Explanation: This will be the output- Fatal error: Call to undefined function b(). You cannot call a
function which is inside a function without calling the outside function.
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 know as..
a) Magic Function
b) Inbuilt Function
c) Default Function
d) User Defined Function
View Answer
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.
1. Which one of the following PHP functions can be used to build a function that accepts
any number of arguments?
a) func_get_argv()
b) func_get_argc()
c) get_argv()
d) get_argc()
View Answer
Answer: b
Explanation: Here is an example-
1. function foo()
2. {
3. $args = func_get_args();
4. foreach ($args as $k => $v)
5. {
6. echo "arg".($k+1).": $v\n";
7. }
8. }
9. foo();
10. /* will print nothing */
11.
12. foo("Hello");
13. /* will print Hello */
14.
15. foo("Hello","World","Bye");
16. /* will print Hello World Bye */
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: 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) get_memory_usage()
d) get_memory_peak_usage()
View Answer
Answer: c
Explanation: We can use the memory_get_usage() function, and 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 id’s?
a) uniqueid()
b) id()
c) md5()
d) mdid()
View Answer
Answer: a
Explanation: Many people use the md5() function for this, even though it’s not exactly meant for
this purpose. uniqueid() is the function that is to be used.
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: We will be able to achieve almost 50% size reduction using this function. The
gzuncompress() function is used to uncompress the string.
6. What will be the output of the following PHP code?
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. 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.
a) karnataka 11,35,000
b) 11,35,000
c) population 11,35,000
d) karnataka population
View Answer
Answer: b
Explanation: Treat states as a multidimensional array and accordingly traverse it to get the value.
4. Which function will return true if a variable is an array or false if it is not?
a) this_array()
b) is_array()
c) do_array()
d) in_array()
View Answer
Answer: b
Explanation: A built-in function, is_array(), is available for testing an array. 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
elementsin 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 array value residing at the position immediately
following that of the current array pointer.
8. Which function can be used to move the pointer to the previous array position?
a) last()
b) before()
c) prev()
d) previous()
View Answer
Answer: c
Explanation: None.
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 array entity holding pear and mango is counted as an item, just as its contents
are.
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: None.
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: 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
Array ( [a] => china )
b) india
Array ( [a] => brazil )
c) china
Array ( [a] => india )
d) china
Array ( [a] => brazil )
View Answer
Answer: b
Explanation: array_shift() removes first element and array_pop() removes the last.
7. What will be the output of the following PHP code?
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 )
Array ( [5] => php )
b) Array ( [1] => hello [2] => hello [3] => hello [4] => hello )
Array ( [5] => php )
c) Array ( [1] => hello [2] => hello [3] => hello [4] => hello [5] => php )
Array ( [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.
8. What will be the output of the following PHP code?
1.<?php
2.$names = array("Sam", "Bob", "Jack");
3.echo $names[0] . "is the brother of " . $names[1] . " and " .
$names[1] . ".";
4.?>
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 array_flip() function flips/exchanges all keys with their associated values in an
array.
3. What will be the output of the following PHP code ?
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 array_intersect() function compares the values of two (or more) arrays, and
returns the matches.
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", "c" => "Audi", "d"
=> "Maseratti");
3.echo array_search("Audi", $a);
4.?>
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.?>
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 array_push() function inserts one or more elements to the end of an array.
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) 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.?>
a) green
b) red
c) blue
d) none of the mentioned
View Answer
Answer: b
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.?>
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: sort() function is used to sort in ascending order where as rsort() meaning reverse
sort is used for sorting in descending order.
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: a
Explanation: The function asort() sorts the array in ascending order, except that the key/value
corresponding is maintained.
4. What will be the output of the following PHP code?
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: While sorting each character is compared with the others and sorted using ascii
values therefore we the sorted array to be like option c.
5. Say in the above question you need to get the array sorted in the manner we humans
would have done it i.e picture1 then picture2 etc.. Which of the following function should
be used?
a) dsort()
b) casesort()
c) natcasesort()
d) naturalsort()
View Answer
Answer: c
Explanation: None.
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 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. ?>
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.