Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Questions On Arrays and Functions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

QUESTION 5 ASS1

a.hello

b.helloblabla

c.Error

d.helloblablablabla

PHP Questions & Answers – Functions


This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Functions”.

1. Which one of the following is the right way of defining a function in PHP?

a) function { function body }


b) data type functionName(parameters) { function body }
c) functionName(parameters) { function body }
d) function fumctionName(parameters) { function body }

Answer:d
Explanation: None.

2.Type Hinting was introduced in which version of PHP?


a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 6

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.

3. What will happen in this function call?


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

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

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 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()

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: 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

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.

PHP Questions & Answers – In-Built Functions in PHP


This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “In-Built
Functions in PHP”.

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.

PHP Questions & Answers – Arrays-1


This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Arrays”.

1. PHP’s numerically indexed array begin with position ___________


a) 1
b) 2
c) 0
d) -1
View Answer
Answer: c
Explanation: Like many programming languages, the first element of an array has an index value
of 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”);
a) iii) and iv)
b) ii) and iii)
c) Only i)
d) ii), iii) and iv)
View Answer
Answer: a
Explanation: A variable name should start with $ symbol which is not present in i) and you need
not put the square brackets when you use the array() constructor.
3. What will be the output of the following php code?
1. <?php
2. $states = array("karnataka" => array
3. ( "population" => "11,35,000", "captial" => "Bangalore"),
4. "Tamil Nadu" => array( "population" => "17,90,000",
5. "captial" => "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: 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.

PHP Coding Questions and Answers – Arrays – 2


This set of Tricky PHP Questions & Answers focuses on “Arrays – 2”.

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

a) I like Volvo, Toyota and BMW


b) I like Volvo, BMW and Toyota
c) I like BMW, Volvo and Toyota
d) I like Toyota, BMW and Volvo
View Answer
Answer: d
Explanation: The order of elements defined.
2. 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($age, $fname);
5.print_r($c);
6.?>

a) Array ( Peter Ben Joe )


b) Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
c) Array ( 35 37 43 )
d) Array ( [35] => Peter [37] => Ben [43] => Joe )
View Answer
Answer: d
Explanation: Here “keys” array is $age and “values” array is $fname.
3. What will be the output of the following PHP code?
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.?>

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


b) Array ( [A] => 2 [Cat] => 2 [Dog] => 1 [Tiger] => 1 )
c) Array ( [A] => 6 [Cat] => 1 [Dog] => 2 [Tiger] => 1 )
d) Array ( [A] => 2 [Cat] => 1 [Dog] => 4 [Tiger] => 1 )
View Answer
Answer: a
Explanation: The array_count_values() function counts all the values of an array and
array_combine() combines arrays.
4. 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", "h" =>
"orange");
4.$a3 = array("i" => "orange");
5.$a4 = array_combine($a2, $a3);
6.$result = array_diff($a4, $a2);
7.print_r($result);
8.?>

a) Array ( [d] => yellow )


b) Array ( [i] => orange )
c) Array ( [h] => orange )
d) Array ( [d] => yellow [h] => orange )
View Answer
Answer: a
Explanation: The array_diff() function compares the values of two (or more) arrays, and returns
the differences.
5. What will be the output of the following PHP code?
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: 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) Sam is the brother of Bob and Jack


b) Sam is the brother of Bob and Bob)
c) Sam is the brother of Jack and Bob)
d) Error
View Answer
Answer: b
Explanation: Simple definition of array and using it in a string. We have used $names[1] twice
and hence Bob appears twice.
9. 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] . ".".$brother;
4.?>

a) Sam is the brother of Bob and Bob) $brother


b) Sam is the brother of Bob and Bob)
c) $brother
d) Error
View Answer
Answer: d
Explanation: $brother undeclared.
10. What will be the output of the following PHP code?
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.?>

a) Array ( [0] => LA [1] => Paris [2] => Paris )


b) Array ( [0] => NYC [1] => LA [2] => Paris)
c) Array ( [0] => NYC [1] => LA [2] => Paris [3] => Paris )
d) None of the mentioned
View Answer
Answer: a
Explanation: array_merge() and array_pop() yields that result.

PHP Coding Questions and Answers – Arrays – 3


This set of Tough PHP Questions & Answers focuses on “Arrays – 3”.

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

a) Array ( [Harry] => 21 [Ron] => 23 [Malfoy] => 21 )


b) Array ( [HARRY] => 21 [RON] => 23 [MALFOY] => 21 )
c) Array ( [HARRY] => 21 [RON] => 23 )
d) Array ( [Harry] => 21 [Ron] => 23 )
View Answer
Answer: c
Explanation: The array_change_key_case() function changes all keys in an array to lowercase or
uppercase and arry_pop() removes last element
2. 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.?>

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) Array ( [1] => Mumbai [0] => Beijing )


b) Array ( [0] => NYC [1] => London )
c) Array ( [1] => NYC [0] => London )
d) Array ( [0] => Mumbai [1] => Beijing )
View Answer
Answer: d
Explanation: The array_replace() function replaces the values of the first array with the values
from following arrays
7. What will be the output of the following PHP code ?
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 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) Key = Harry, Value = 21


Key = Ron, Value = 21
Key = Malfoy, Value = 23
b) Key = Harry, Value = 21
Key = Ron, Value = 19
Key = Malfoy, Value = 23
c) Key = Harry, Value = 21
Key = Malfoy, Value = 23
Key = Ron, Value = 19
d) Key = Ron, Value = 19
Key = Harry, Value = 21
Key = Malfoy, Value = 23
View Answer
Answer: c
Explanation: The ksort() function sorts an associative array in ascending order, according to the
key.
PHP Coding Questions and Answers – Arrays – 4
This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Arrays – 4”.

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

a) I like Volvo BMW and Toyota)


b) I like Volvo, BMW and Toyota)
c) I like Volvo, BMW and Toyota
d) I like. Volvo.,. BMW. and) Toyota)
View Answer
Answer: b
Explanation: The array() function is used to create an array.
2. 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.?>

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 )
View Answer
Answer: c
Explanation: The array_change_key_case() function changes all keys in an array to lowercase or
uppercase.
3. 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.?>

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) Array ( Peter Ben Joe )


b) Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
c) Array ( 35 37 43 )
d) Array ( “[Peter] => 35” “[Ben] => 37” “[Joe] => 43” )
View Answer
Answer: b
Explanation: The array_combine() function creates an array by using the elements from one
“keys” array and one “values” array.
5. 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.?>

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


b) Array ( [A] => 2 [Cat] => 2 [Dog] => 1 )
c) Array ( [A] => 1 [Cat] => 1 [Dog] => 2 )
d) Array ( [A] => 2 [Cat] => 1 [Dog] => 1)
View Answer
Answer: a
Explanation: The array_count_values() function counts all the values of an array.
6. 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.?>

a) Array ( [d] => yellow )


b) Array ( [c] => blue )
c) Array ( [a] => red )
d) Array ( [e] => yellow )
View Answer
Answer: a
Explanation: The array_diff() function compares the values of two (or more) arrays, and returns
the differences.
7. 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 "<br>";
6.print_r($b1);
7.?>

a) Array ( [3] => blue [4] => blue)


Array ( [0] => red )
b) Array ( [4] => blue [5] => blue [6] => blue)
Array ( [0] => red )
c) Array ( [3] => blue [4] => blue [5] => blue [6] => blue )
Array ()
d) Array ( [3] => blue [4] => blue [5] => blue [6] => blue )
Array ( [0] => red )
View Answer
Answer: d
Explanation: The array_fill() function fills an array with values.
8. What will be the output of the following PHP code?
1.<?php
2.$a1 = array("red", "green");
3.$a2 = array("blue", "yellow");
4.print_r(array_merge($a1, $a2));
5.?>

a) Array ( [0] => red [1] => green)


b) Array ( [0] => blue [1] => yellow [2] => red [3] => green )
c) Array ( [0] => red [1] => green [2] => blue [3] => yellow )
d) Array ( [0] => blue [1] => yellow )
View Answer
Answer: c
Explanation: The array_merge() function merges one or more arrays into one array.
9. 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.?>

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) Array ( [0] => red [1] => green )


b) Array ( [0] => green [1] => blue )
c) Array ( [0] => red [1] => blue )
d) Array ( [0] => blue [1] => blue )
View Answer
Answer: a
Explanation: The array_pop() function deletes the last element of an array
PHP Questions & Answers – Arrays-5
This set of PHP Interview Questions and Answers for freshers focuses on “Arrays-5”.

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

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) Array ( [0] => peach )


b) Array ( [0] => apple [1] => mango [2] => peach )
c) Array ( [0] => apple [1] => mango )
d) Array ( [0] => peach [1] => pear [2] => orange )
View Answer
Answer: d
Explanation: The array_slice() function returns a section of an array based on a starting and
ending offset value.
8. 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. ?>

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.

You might also like