PHP Arrays | PHP Questions

Que. What will be the output of the following PHP code?
<?php
$cars = array("Volvo", "BMW", "Toyota", "Honda", "Mercedes", "Opel");
print_r(array_chunk($cars, 2));
?>
a. Array ( [0] => Array ( [0] => Volvo [1] => Volvo ) [1] => Array ( [0] => BMW [1] => BMW ) [2] => Array ( [0] => Toyota [1] => Toyota ) )
b. Array ( [0] => Array ( [0] => Volvo [1] => BMW ) [1] => Array ( [0] => Toyota [1] => Honda ) [2] => Array ( [0] => Mercedes [1] => Opel ) )
c. Array ( [0] => Array ( [1] => Volvo [2] => BMW ) [1] => Array ( [1] => Toyota [2] => Honda ) [2] => Array ( [1] => Mercedes [2] => Opel ) )
d. Array ( [1] => Array ( [1] => Volvo [2] => BMW ) [2] => Array ( [1] => Toyota [2] => Honda ) [3] => Array ( [1] => Mercedes [2] => Opel ) )
Answer: Array ( [0] => Array ( [0] => Volvo [1] => BMW ) [1] => Array ( [0] => Toyota [1] => Honda ) [2] => Array ( [0] => Mercedes [1] => Opel ) )


Que. What will be the output of the following PHP code ?
<?php
$array = array("red", "green");
array_push($array, "blue", "yellow");
print_r($array);
?>
a. Array ( [0] => red [1] => green [2] => blue [3] => yellow )
b. Array ( [0] => blue [1] => yellow [2] => red [3] => green )
c. Array ( [0] => blue [1] => yellow )
d. Array ( [0] => red [1] => green )
Answer: Array ( [0] => red [1] => green [2] => blue [3] => yellow )


Que. What will be the output of the following PHP code?
<?php
$fname = array("Peter", "Ben", "Joe");
$age = array("35", "37", "43");
$c = array_combine($fname, $age);
print_r($c);
?>
a. Array ( “[Peter] => 35” “[Ben] => 37” “[Joe] => 43” )
b. Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
c. Array ( Peter Ben Joe )
d. Array ( 35 37 43 )
Answer: Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )


Que. What will be the output of the following PHP code?
<?php
$fruits = array ("apple", "cherry", "mango", "berry", "orange");
$subset = array_slice ($fruits, 2);
print_r ($subset);
?>
a. Array ( [0] => peach [1] => pear [2] => orange )
b. Array ( [0] => apple [1] => mango [2] => peach )
c. Array ( [0] => peach )
d. Array ( [0] => apple [1] => mango )
Answer: Array ( [0] => peach [1] => pear [2] => orange )


Que. What will be the output of the following PHP code?
<?php $fruits = array ("mango", "apple", "peach", "pear"); $fruits = asort ($fruits); printr ($fruits); ?>
a. Array ( [1] => apple [0] => mango [2] => peach [3] => pear )
b. Array ( [1] => apple [0] => mango [3] => peach [2] => pear )
c. Array ( [0] => apple [1] => mango [2] => peach [3] => pear )
d. Error


Array ( [1] => apple [0] => mango [2] => peach [3] => pear )


Que. What will be the output of the following PHP code?
<?php $state = array ("Karnataka", "Goa", "Tamil Nadu", "Andhra Pradesh"); echo (array_search ("Tamil Nadu", $state) ); ?>
a. True
b. 1
c. False
d. 2
Answer: 2


Que. Which function can be used to move the pointer to the previous array position?
a. prev()
b. last()
c. before()
d. previous()
Answer: prev()


Que. What will be the output of the following PHP code ?
<?php<br> $city_west = array("NYC", "London");<br> $city_east = array("Mumbai", "Beijing");<br> print_r(array_replace($city_west, $city_east));<br> ?><br>
a. Array ( [1] => Mumbai [0] => Beijing )
b. Array ( [1] => NYC [0] => London )
c. Array ( [0] => Mumbai [1] => Beijing )
d. Array ( [0] => NYC [1] => London )
Answer: Array ( [0] => Mumbai [1] => Beijing )


Que. What will be the output of the following PHP code?
<?php<br> $fname = array("Peter", "Ben", "Joe");<br> $age = array("35", "37", "43");<br> $c = array_combine($age, $fname);<br> print_r($c);<br> ?><br>
a. Array ( [35] => Peter [37] => Ben [43] => Joe )
b. Array ( Peter Ben Joe )
c. Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
d. Array ( 35 37 43 )
Answer: Array ( [35] => Peter [37] => Ben [43] => Joe )


Que. What will be the output of the following PHP code ?
<?php<br> $number = range(0, 5);<br> print_r ($number);<br> ?><br>
a. Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 )
b. Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
c. Array ( [0] => 0 [5] => 5 )
d. Array ( [0] => 5 [1] => 5 [2] => 5 [3] => 5 [4] => 5 [5] => 5 )


Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )


Que. What will be the output of the following PHP code ?
<?php<br> $a = array("a" => "Jaguar", "b" => "Land Rover", "c" => "Audi", "d" => "Maseratti");<br> echo array_search("Audi", $a);<br> ?><br>
a. d
b. c
c. b
d. a
Answer: c


Que. What will be the output of the following php code?
<?php<br> $states = array("karnataka" => array( "population" => "11,35,000", "captial" => "Bangalore"),"Tamil Nadu" => array( "population" => "17,90,000","captial" => "Chennai") );<br> echo $states["karnataka"]["population"];<br> ?>
a. 11,35,000
b. karnataka 11,35,000
c. karnataka population
d. population 11,35,000
Answer: 11,35,000


Que. Which function returns an array consisting of associative key/value pairs?
a. count()
b. array_count()
c. array_count_values()
d. count_values()
Answer: array_count_values()


Que. There are three different kind of arrays:
a. Numeric array, Associative array, Multidimensional array
b. Const array, Associative array, Multidimensional array
c. Numeric array, String array, Multidimensional array
d. Numeric array, Associative array, Dimensional array
Answer: Numeric array, Associative array, Multidimensional array


Que. What will be the output of the following PHP code?
<?php<br> $cars = array("Volvo", "BMW", "Toyota");<br> echo "I like " . $cars[2] . ", " . $cars[1] . " and " . $cars[0] . ".";<br> ?><br>
a. I like Toyota, BMW and Volvo
b. I like Volvo, BMW and Toyota
c. I like Volvo, Toyota and BMW
d. I like BMW, Volvo and Toyota


I like Toyota, BMW and Volvo


Que. What will be the output of the following PHP code?
<?php<br> $arr = array ("picture1.JPG", "picture2.jpg", "Picture10.jpg", "picture20.jpg");<br> sort($arr);<br> print_r($arr);<br> ?><br>
a. Array ( [0] => picture1.JPG [1] => Picture10.jpg [2] => picture2.jpg [3] => picture20.jpg )
b. Array ( [0] => Picture10.jpg [1] => picture1.JPG [2] => picture2.jpg [3] => picture20.jpg )
c. Array ( [0] => Picture10.jpg [1] => picture1.JPG [2] => picture20.jpg [3] => picture2.jpg )
d. Array ( [0] => picture1.JPG [1] => picture2.jpg [2] => Picture10.jpg [3] => picture20.jpg )
Answer: Array ( [0] => Picture10.jpg [1] => picture1.JPG [2] => picture2.jpg [3] => picture20.jpg )


Que. Which function will return true if a variable is an array or false if it is not?
a. this_array()
b. is_array()
c. in_array()
d. do_array()
Answer: is_array()


Que. What will be the output of the following PHP code?
<?php<br> $fruits = array ("apple", "orange", "banana");<br> echo (next($fruits)); <br> echo (next($fruits));<br> ?><br>
a. appleorange
b. orangebanana
c. orangeorange
d. appleapple
Answer: orangebanana


Que. Which of the following are correct ways of creating an array?
1. state[0] = “karnataka”;
2. $state[] = array(“karnataka”);
3. $state[0] = “karnataka”;
4. $state = array(“karnataka”);
a. Only 1
b. 3 and 4
c. 2 and 3
d. 2, 3 and 4
Answer: 3 and 4


Que. What will be the output of the following PHP code ?
<?php<br> $a = array(12, 5, 2);<br> echo(array_product($a));<br> ?><br>
a. 010
b. 060
c. 120
d. 024


120


Que. What will be the output of the following PHP code?
<?php<br> $fruits = array ("mango", "apple", "pear", "peach");<br> $fruits = array_flip($fruits);<br> echo ($fruits[0]);<br> ?><br>
a. 0
b. error
c. peach
d. mango
Answer: error


Que. What will be the output of the following PHP code?
<?php<br> $names = array("Sam", "Bob", "Jack");<br> echo $names[0] . "is the brother of " . $names[1] . " and " . $names[1] . ".".$brother;<br> ?><br>
a. Error
b. Sam is the brother of Bob and Bob)
c. Sam is the brother of Bob and Bob) $brother
d. $brother
Answer: Error


Que. What will be the output of the following PHP code?
<?php<br> $a1 = array("a"=>"red", "b"=>"green", "c"=>"blue", "d"=>"yellow");<br> $a2 = array("e"=>"red", "f"=>"green", "g"=>"blue");<br> $result = array_diff($a1, $a2);<br> print_r($result);<br> ?><br>
a. Array ( [d] => yellow )
b. Array ( [c] => blue )
c. Array ( [e] => yellow )
d. Array ( [a] => red )
Answer: Array ( [d] => yellow )


Que. What will be the output of the following PHP code?
<?php<br> $a=array("A","Cat","Dog","A","Dog");<br> $b=array("A","A","Cat","A","Tiger");<br> $c=array_combine($a,$b);<br> print_r(array_count_values($c));<br> ?><br>
a. Array ( [A] => 5 [Cat] => 2 [Dog] => 2 [Tiger] => 1 )
b. Array ( [A] => 2 [Cat] => 2 [Dog] => 1 [Tiger] => 1 )
c. Array ( [A] => 2 [Cat] => 1 [Dog] => 4 [Tiger] => 1 )
d. Array ( [A] => 6 [Cat] => 1 [Dog] => 2 [Tiger] => 1 )
Answer: Array ( [A] => 5 [Cat] => 2 [Dog] => 2 [Tiger] => 1 )


Que. What will be the output of the following PHP code?
<?php<br> $a1 = array("red", "green");<br> $a2 = array("blue", "yellow");<br> $a3 = array_merge($a1, $a2);<br> $a4 = array("a", "b", "c", "d");<br> $a = array_combine($a4, $a3);<br> print_r($a);<br> ?><br>
a. Array ( [a] => red [b] => green [c] => blue [d] => yellow )
b. Array ( [0] => red [1] => green [2] => blue [3] => yellow )
c. Array ( [a] => blue [b] => yellow [c] => red [d] => green )
d. Array ( [0] => blue [1] => yellow [2] => red [3] => green )


Array ( [a] => red [b] => green [c] => blue [d] => yellow )


Que. What will be the output of the following PHP code?
<?php<br> $a1 = array("a" => "red", "b" => "green", "c" => "blue", "d" => "yellow");<br> $a2 = array("e" => "red", "f" => "green", "g" => "blue", "h" => "orange");<br> $a3 = array("i" => "orange");<br> $a4 = array_combine($a2, $a3);<br> $result = array_diff($a4, $a2);<br> print_r($result);<br> ?><br>
a. Array ( [d] => yellow )
b. Array ( [d] => yellow [h] => orange )
c. Array ( [h] => orange )
d. Array ( [i] => orange )
Answer: Array ( [d] => yellow )


Que. What will be the output of the following PHP code ?
<?php<br> $people = array("Peter", "Susan", "Edmund", "Lucy");<br> echo pos($people);<br> ?><br>
a. Peter
b. Edmund
c. Lucy
d. Susan
Answer: Peter


Que. Absent any actual need for choosing one method over the other, does passing arrays by value to a read-only function reduce performance compared to passing them by reference?
a. Yes, because PHP must monitor the execution of the function to determine if changes are made to the array.
b. Yes, but only if the function modifies the contents of the array.
c. Yes, because the interpreter must always create a copy of the array before passing it to the function.
d. Yes, but only if the array is large.
Answer: no


Que. What elements will the following script output?
<?php<br> $array = array (true => 'a', 1 => 'b');<br> var_dump ($array);<br> ?><br>
a. True => 'a', 1 => 'b'
b. 1 => 'b'
c. 0 => 'a', 1 => 'b'
d. None
Answer: null


Que. What will be the output of the following PHP code?
<?php<br> $a = array("a"=>"red", "b"=>"green", "c"=>"blue");<br> echo array_shift($a);<br> print_r ($a);<br> ?><br>
a. blue
b. red
c. green
d. none of the mentioned


red


Que. What will be the output of the following PHP code?
<?php<br> $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");<br> print_r(array_change_key_case($age, CASE_UPPER));<br> ?><br>
a. Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
b. Array ( [peter] => 35 [ben] => 37 [joe] => 43 )
c. Array ( [PETER] => 35 [BEN] => 37 [JOE] => 43 )
d. Array ( [PeTeR] => 35 [BeN] => 37 [Joe] => 43 )
Answer: Array ( [PETER] => 35 [BEN] => 37 [JOE] => 43 )


Que. What will be the output of the following PHP code ?
<?php<br> $a1 = array("a" => "red", "b" => "green", "c" => "blue", "d" => "yellow");<br> $a2 = array("e" => "red","f" => "green", "g" => "blue");<br> $result = array_intersect($a1, $a2);<br> print_r($result);<br> ?>
a. Array ( [a] => red [b] => green [c] => blue [d] => yellow )
b. Array ( [e] => red [f] => green [g] => blue )
c. Array ( [a] => red [b] => green [c] => blue )
d. Array ( [a] => red [b] => green [c] => blue [d] => yellow [e] => red [f] => green [g] => blue )
Answer: Array ( [a] => red [b] => green [c] => blue )


Que. Array values are keyed by ______ values (called indexed arrays) or using ______ values (called associative arrays). Of course, these key methods can be combined as well.
a. String, Boolean
b. Positive number, negative number
c. Float, string
d. Even number, string
Answer: string


Que. PHP’s numerically indexed array begin with position ______.
a. -1
b. 0
c. 2
d. 1
Answer: 0


Que. What will be the output of the following PHP code?
<?php<br> $number = array ("4", "hello", 2);<br> echo (array_sum ($number));<br> ?><br>
a. 2
b. 4
c. 6
d. 4hello2


6


Que. What will be the output of the following PHP code?
<?php<br> $face = array ("A", "J", "Q", "K");<br> $number = array ("2","3","4", "5", "6", "7", "8", "9", "10");<br> $cards = array_merge ($face, $number);<br> print_r ($cards);<br> ?><br>
a. 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 )
b. Error
c. 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 )
d. 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 )
Answer: 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 )


Que. What will be the output of the following PHP code ?
<?php<br> $age = array("Harry" => "21", "Ron" => "23","Malfoy" => "21");<br> array_change_key_case($age, CASE_UPPER);<br> array_pop($age);<br> print_r($age);<br> ?>
a. Array ( [HARRY] => 21 [RON] => 23 )
b. Array ( [Harry] => 21 [Ron] => 23 [Malfoy] => 21 )
c. Array ( [Harry] => 21 [Ron] => 23 )
d. Array ( [HARRY] => 21 [RON] => 23 [MALFOY] => 21 )
Answer: Array ( [HARRY] => 21 [RON] => 23 )


Que. What will be the output of the following PHP code?
<?php<br> $place = array("NYC", "LA", "Paris");<br> array_pop($place);<br> $place1 = array("Paris");<br> $place = array_merge($place, $place1);<br> print_r($place);<br> ?><br>
a. Array ( [0] => NYC [1] => LA [2] => Paris)
b. Array ( [0] => NYC [1] => LA [2] => Paris [3] => Paris )
c. Array ( [0] => LA [1] => Paris [2] => Paris )
d. None of the mentioned
Answer: Array ( [0] => LA [1] => Paris [2] => Paris )


Que. Which array function checks if the specified key exists in the array?
a. array_key_exists()
b. arrays_key_exists()
c. array_keys_exists()
d. array_key_exist()
Answer: array_key_exists()


Que. Assume you would like to sort an array in ascending order by value while preserving key associations. Which of the following PHP sorting functions would you use?
a. asort()
b. ksort()
c. krsort()
d. sort()


usort()


Que. Which in-built function will add a value to the end of an array?
a. inend_array()
b. array_unshift()
c. array_push()
d. into_array()
Answer: array_push()


Que. What will be the output of the following PHP code?
<?php<br> $array1 = array ("KA", "LA", "CA", "MA", "TA");<br> $array2 = array ("KA", "IA", "CA", "GA", "TA");<br> $inter = array_intersect ($array1, $array2);<br> print_r ($inter);<br> ?><br>
a. Array ( [0] => KA [2] => CA [4] => TA )
b. Array ( [0] => KA [1] => LA [2] => CA [3] => MA [4] => TA [5] => IA [6] => GA )
c. Array ( [1] => IA [3] => GA )
d. Array ( [1] => LA [3] => MA )
Answer: Array ( [0] => KA [2] => CA [4] => TA )


Que. What will the following script output?
<?php<br> $array = array (1, 2, 3, 5, 8, 13, 21, 34, ;<br> $sum = 0;<br> for ($i = 0; $i < 5; $i++) {<br> $sum += $array[$array[$i]];<br> }<br> echo $sum;<br> ?>
a. NULL
b. 19
c. 78
d. 5
Answer: 78


Que. What will be the output of the following PHP code ?
<?php<br> $a1 = array("a" => "red", "b" => "green", "c" => "blue", "d" => "yellow");<br> $result = array_flip($a1);<br> print_r($result);<br> ?>
a. Array ( [a] => a [b] => b [c] => c [d] => d )
b. Array ( [red] => red [green] => green [blue] => blue [yellow] => yellow )
c. Array ( [a] => red [b] => green [c] => blue [d] => yellow )
d. Array ( [red] => a [green] => b [blue] => c [yellow] => d )
Answer: Array ( [red] => a [green] => b [blue] => c [yellow] => d )


Que. What will be the output of the following PHP code?
<?php<br> $a = array("A", "Cat", "Dog", "A", "Dog");<br> print_r(array_count_values($a));<br> ?><br>
a. Array ( [A] => 2 [Cat] => 1 [Dog] => 1)
b. Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )
c. Array ( [A] => 1 [Cat] => 1 [Dog] => 2 )
d. Array ( [A] => 2 [Cat] => 2 [Dog] => 1 )


Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )


Que. What will be the output of the following PHP code?
<?php<br> $a1 = array("red", "green");<br> $a2 = array("blue", "yellow");<br> print_r(array_merge($a1, $a2));<br> ?><br>
a. Array ( [0] => red [1] => green [2] => blue [3] => yellow )
b. Array ( [0] => blue [1] => yellow [2] => red [3] => green )
c. Array ( [0] => blue [1] => yellow )
d. Array ( [0] => red [1] => green)
Answer: Array ( [0] => red [1] => green [2] => blue [3] => yellow )


Que. What will be the output of the following PHP code?
<?php<br> $fruits = array ("apple", "mango", "peach", "pear", "orange");<br> $subset = array_splice ($fruits, 2);<br> print_r ($fruits);<br> ?><br>
a. Array ( [0] => pear [1] => orange )
b. Array ( [0] => apple [1] => mango [2] => peach )
c. Array ( [0] => apple [1] => mango )
d. Error
Answer: Array ( [0] => apple [1] => mango )


Que. What will be the output of the following PHP code?
<?php<br> $a = array("red", "green", "blue");<br> array_pop($a);<br> print_r($a);<br> ?><br>
a. Array ( [0] => green [1] => blue )
b. Array ( [0] => blue [1] => blue )
c. Array ( [0] => red [1] => blue )
d. Array ( [0] => red [1] => green )
Answer: Array ( [0] => red [1] => green )


Comments