PHP

PHP

Arrays - 2 has 25 Questions

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. ?>

I like Volvo BMW and Toyota)
I like Volvo, BMW and Toyota)
I like Volvo, BMW and Toyota
I like. Volvo.,. BMW. an Toyota) .
What will be the output of the following PHP code?
  1. <?php
  2. $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
  3. print_r(array_change_key_case($age, CASE_UPPER));
  4. ?>

Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
Array ( [peter] => 35 [ben] => 37 [joe] => 43 )
Array ( [PETER] => 35 [BEN] => 37 [JOE] => 43 )
Array ( [PeTeR] => 35 [BeN] => 37 [Joe] => 43 )
What will be the output of the following PHP code?
  1. <?php
  2. $cars = array("Volvo", "BMW", "Toyota", "Honda", "Mercedes", "Opel");
  3. print_r(array_chunk($cars, 2));
  4. ?>

Array ( [0] => Array ( [1] => Volvo [2] => BMW ) [1] => Array ( [1] => Toyota [2] => Honda ) [2] => Array ( [1] => Mercedes [2] => Opel ) )
Array ( [1] => Array ( [1] => Volvo [2] => BMW ) [2] => Array ( [1] => Toyota [2] => Honda ) [3] => Array ( [1] => Mercedes [2] => Opel ) )
Array ( [0] => Array ( [0] => Volvo [1] => Volvo ) [1] => Array ( [0] => BMW [1] => BMW ) [2] => Array ( [0] => Toyota [1] => Toyota ) )
Array ( [0] => Array ( [0] => Volvo [1] => BMW ) [1] => Array ( [0] => Toyota [1] => Honda ) [2] => Array ( [0] => Mercedes [1] => Opel ) )
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. ?>

Array ( Peter Ben Joe )
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
Array ( 35 37 43 )
Array ( "[Peter] => 35"? "[Ben] => 37"? "[Joe] => 43"? )
What will be the output of the following PHP code?
  1. <?php
  2. $a = array("A", "Cat", "Dog", "A", "Dog");
  3. print_r(array_count_values($a));
  4. ?>

Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )
Array ( [A] => 2 [Cat] => 2 [Dog] => 1 )
Array ( [A] => 1 [Cat] => 1 [Dog] => 2 )
Array ( [A] => 2 [Cat] => 1 [Dog] => 1)
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_diff($a1, $a2);
  5. print_r($result);
  6. ?>

Array ( [d] => yellow )
Array ( [c] => blue )
Array ( [a] => red )
Array ( [e] => yellow )
What will be the output of the following PHP code?
  1. <?php
  2. $a1 = array_fill(3, 4, "blue");
  3. $b1 = array_fill(0, 1, "red");
  4. print_r($a1);
  5. echo "
    "
    ;
  6. print_r($b1);
  7. ?>

Array ( [3] => blue [4] => blue) Array ( [0] => red )
Array ( [4] => blue [5] => blue [6] => blue) Array ( [0] => red )
Array ( [3] => blue [4] => blue [5] => blue [6] => blue ) Array ()
Array ( [3] => blue [4] => blue [5] => blue [6] => blue ) Array ( [0] => red )
What will be the output of the following PHP code?
  1. <?php
  2. $a = array("a"=>"red", "b"=>"green", "c"=>"blue");
  3. echo array_shift($a);
  4. print_r ($a);
  5. ?>

green
red
blue
none of the above
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. ?>

Array ( [0] => red [1] => green )
Array ( [0] => green [1] => blue )
Array ( [0] => red [1] => blue )
Array ( [0] => blue [1] => blue )
What will be the output of the following PHP code?
  1.     <?php
  2.     $fruits = array ("mango", "apple", "pear", "peach");
  3.     $fruits = array_flip($fruits);
  4.     echo ($fruits[0]);
  5.     ?>

mango
Error
peach
0
Which of the functions is used to sort an array in descending order?

sort()
asort()
rsort()
dsort()
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.     ?>

Array ( [1] => apple [0] => mango [2] => peach [3] => pear )
Array ( [0] => apple [1] => mango [2] => peach [3] => pear )
Error
Array ( [1] => apple [0] => mango [3] => peach [2] => pear )
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.     ?>

Array ( [0] => picture1.JPG [1] => Picture10.jpg [2] => picture2.jpg [3] => picture20.jpg )
Array ( [0] => picture1.JPG [1] => picture2.jpg [2] => Picture10.jpg [3] => picture20.jpg )
Array ( [0] => Picture10.jpg [1] => picture1.JPG [2] => picture2.jpg [3] => picture20.jpg )
Array ( [0] => Picture10.jpg [1] => picture1.JPG [2] => picture20.jpg [3] => picture2.jpg )
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?

dsort()
casesort()
natcasesort()
naturalsort()
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.     ?>

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 )
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 )
Error
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 )
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.     ?>

Array ( [0] => peach )
Array ( [0] => apple [1] => mango [2] => peach )
Array ( [0] => apple [1] => mango )
Array ( [0] => peach [1] => pear [2] => orange )
What will be the output of the following PHP code?
  1.     <?php
  2.     $fruits = array ("apple", "mango", "peach", "pear",
  3.     "orange");
  4.     $subset = array_splice ($fruits, 2);
  5.     print_r ($fruits);
  6.     ?>

Error
Array ( [0] => apple [1] => mango [2] => peach )
Array ( [0] => apple [1] => mango )
Array ( [0] => pear [1] => orange )
What will be the output of the following PHP code?
  1.     <?php
  2.     $number = array ("4", "hello", 2);
  3.     echo (array_sum ($number));
  4.     ?>

4hello2
4
2
6
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.     ?>

Array ( [0] => KA [1] => LA [2] => CA [3] => MA [4] => TA [5] => IA [6] => GA )
Array ( [0] => KA [2] => CA [4] => TA )
Array ( [1] => IA [3] => GA )
Array ( [1] => LA [3] => MA )
What will be the output of the following PHP code ?
  1. <?php
  2. $age = array("Harry" => "21", "Ron" => "23","Malfoy" => "21");
  3. array_change_key_case($age, CASE_UPPER);
  4. array_pop($age);
  5. print_r($age);
  6. ?>

Array ( [Harry] => 21 [Ron] => 23 [Malfoy] => 21 )
Array ( [HARRY] => 21 [RON] => 23 [MALFOY] => 21 )
Array ( [HARRY] => 21 [RON] => 23 )
Array ( [Harry] => 21 [Ron] => 23 )
What will be the output of the following PHP code ?
  1. <?php
  2. $a1 = array("a" => "red", "b" => "green", "c" => "blue", "d" => "yellow");
  3. $result = array_flip($a1);
  4. print_r($result);
  5. ?>

Array ( [red] => red [green] => green [blue] => blue [yellow] => yellow )
Array ( [a] => a [b] => b [c] => c [d] => d )
Array ( [red] => a [green] => b [blue] => c [yellow] => d )
Array ( [a] => red [b] => green [c] => blue [d] => yellow )
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. ?>

Array ( [a] => red [b] => green [c] => blue )
Array ( [a] => red [b] => green [c] => blue [d] => yellow )
Array ( [e] => red [f] => green [g] => blue )
Array ( [a] => red [b] => green [c] => blue [d] => yellow [e] => red [f] => green [g] => blue )
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
b
c
d
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. ?>

Array ( [1] => Mumbai [0] => Beijing )
Array ( [0] => NYC [1] => London )
Array ( [1] => NYC [0] => London )
Array ( [0] => Mumbai [1] => Beijing )
What will be the output of the following PHP code ?
  1. <?php
  2. $people = array("Peter", "Susan", "Edmund", "Lucy");
  3. echo pos($people);
  4. ?>

Lucy
Peter
Susan
Edmund