Arrays(Php ) Questions and Answers

Question 1. 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”);
  1.    3 and 4
  2.    2 and 3
  3.    Only 1
  4.    2, 3 and 4
Explanation:-
Answer: Option A. -> 3 and 4
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.

Question 2. PHP’s numerically indexed array begin with position ______.
  1.    1
  2.    2
  3.    0
  4.    -1
Explanation:-
Answer: Option C. -> 0
Like many programming languages, the first element of an array has an index value of 0.

Question 3. What will be the output of the following php code?
  1.    karnataka 11,35,000
  2.    11,35,000
  3.    population 11,35,000
  4.    karnataka population
Explanation:-
Answer: Option B. -> 11,35,000
Treat states as a multidimensional array and accordingly traverse it to get the value.

Question 4. Which function will return true if a variable is an array or false if it is not?
  1.    this_array()
  2.    is_array()
  3.    do_array()
  4.    in_array()
Explanation:-
Answer: Option B. -> is_array()
A built-in function, is_array(), is available for testing an array. Its prototype follows: boolean is_array(mixed variable).

Question 5. Which in-built function will add a value to the end of an array?
  1.    array_unshift()
  2.    into_array()
  3.    inend_array()
  4.    array_push()
Explanation:-
Answer: Option D. -> array_push()
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.

Question 6. What function computes the difference of arrays?
  1.    array_diff
  2.    diff_array
  3.    arrays_diff
  4.    diff_arrays
Explanation:-
Answer: Option A. -> array_diff

Question 7. What array will you get if you convert an object to an array?
  1.    An array with properties of that object as the array's elements
  2.    An array with properties of that array as the object's elements
  3.    An array with properties of that object as the Key elements
  4.    An array with keys of that object as the array's elements
Explanation:-
Answer: Option A. -> An array with properties of that object as the array's elements

Question 8. What functions count elements in an array?
1. count
2. Sizeof
3. Array_Count
4. Count_array
  1.    Option 2 and 4
  2.    Option 1 and 2
  3.    Option 3 and 4
  4.    Option 1 and 4
Explanation:-
Answer: Option B. -> Option 1 and 2

Question 9. What will be the output of the following PHP code?
  1.    I like Volvo, Toyota and BMW
  2.    I like Volvo, BMW and Toyota
  3.    I like BMW, Volvo and Toyota
  4.    I like Toyota, BMW and Volvo
Explanation:-
Answer: Option D. -> I like Toyota, BMW and Volvo
The order of elements defined.

Question 10. What will be the output of the following PHP code?
  1.    Array ( Peter Ben Joe )
  2.    Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
  3.    Array ( 35 37 43 )
  4.    Array ( [35] => Peter [37] => Ben [43] => Joe )
Explanation:-
Answer: Option D. -> Array ( [35] => Peter [37] => Ben [43] => Joe )
Here “keys” array is $age and “values” array is $fname.