MCQs on Arrays | PHP Basics

Mastering PHP arrays is crucial for efficient data management in web development. Understanding indexed, associative, and multidimensional arrays, along with PHP’s powerful array functions and sorting techniques, can significantly enhance coding skills.


Multiple Choice Questions (MCQs)

1. Indexed Arrays

  1. What is the default index for the first element in a PHP indexed array?
    a) 1
    b) 0
    c) -1
    d) None
  2. How do you create an indexed array in PHP?
    a) $arr = array("apple", "banana", "cherry");
    b) $arr = {"apple", "banana", "cherry"};
    c) $arr = ["apple" => 1, "banana" => 2];
    d) $arr = ("apple", "banana", "cherry");
  3. Which of the following will return the length of an indexed array $fruits?
    a) length($fruits)
    b) count($fruits)
    c) size($fruits)
    d) total($fruits)
  4. What will the following code output?
    $arr = array(5, 10, 15); echo $arr[1]; a) 5
    b) 10
    c) 15
    d) Undefined index error
  5. How do you add a new element “orange” to the end of an indexed array $fruits?
    a) $fruits[] = "orange";
    b) array_add($fruits, "orange");
    c) $fruits->push("orange");
    d) $fruits = "orange";

2. Associative Arrays

  1. Which of the following correctly creates an associative array?
    a) $arr = array("key" => "value");
    b) $arr = {"key": "value"};
    c) $arr = ["key" => "value"];
    d) Both a and c
  2. How can you access the value of an associative array with key “name”?
    a) $arr->name
    b) $arr[name]
    c) $arr["name"]
    d) $arr->{"name"}
  3. What will the following code output?
    $arr = array("a" => 1, "b" => 2); echo $arr["b"]; a) a
    b) 2
    c) b
    d) 1
  4. Which function is used to check if a key exists in an associative array?
    a) key_exists()
    b) array_key()
    c) isset()
    d) in_array()
  5. How do you remove an element with a specific key from an associative array?
    a) unset($arr["key"]);
    b) delete($arr["key"]);
    c) remove($arr["key"]);
    d) unset($arr->key);

3. Multidimensional Arrays

  1. What is a multidimensional array in PHP?
    a) An array with keys and values
    b) An array containing other arrays
    c) An array with numeric indexes
    d) An array of functions
  2. How do you access the element “apple” in the following multidimensional array?phpCopy code$fruits = array( array("apple", "banana"), array("cherry", "date") ); a) $fruits[0][0];
    b) $fruits[1][1];
    c) $fruits[0][1];
    d) $fruits[1][0];
  3. What does the following code output?
    $arr = array( "first" => array("x" => 10, "y" => 20), "second" => array("a" => 30, "b" => 40) ); echo $arr["second"]["b"]; a) 10
    b) 20
    c) 30
    d) 40
  4. How can you add a new array element to a multidimensional array?
    a) $arr[][] = "new value";
    b) $arr[] = array("new value");
    c) array_push($arr, "new value");
    d) $arr = "new value";
  5. What function can be used to count all elements in a multidimensional array recursively?
    a) count($arr, 1)
    b) count($arr, COUNT_RECURSIVE)
    c) sizeof($arr, true)
    d) array_count($arr, true)

4. Array Functions

  1. What does the array_merge() function do?
    a) Joins two arrays into a string
    b) Combines two arrays into one
    c) Sorts two arrays
    d) Splits an array into two
  2. What is the output of the following code? $arr1 = array("a", "b"); $arr2 = array("c", "d"); $result = array_merge($arr1, $arr2); echo count($result); a) 2
    b) 4
    c) 1
    d) 3
  3. Which function slices an array?
    a) array_cut()
    b) array_slice()
    c) array_splice()
    d) slice()
  4. How do you get the sum of values in an array?
    a) array_sum()
    b) sum_array()
    c) total()
    d) array_add()
  5. What is the use of in_array() function?
    a) Checks if a value exists in an array
    b) Removes a value from an array
    c) Sorts array in descending order
    d) Converts array to string

5. Sorting Arrays

  1. How do you sort an array in ascending order?
    a) asort($arr)
    b) sort($arr)
    c) rsort($arr)
    d) ksort($arr)
  2. Which function sorts an associative array by keys?
    a) ksort()
    b) arsort()
    c) asort()
    d) usort()
  3. What will the following code output? $arr = array(5, 2, 8, 1); sort($arr); echo $arr[0]; a) 5
    b) 2
    c) 1
    d) 8
  4. Which function is used to sort an array in reverse order?
    a) rsort()
    b) krsort()
    c) reverse_sort()
    d) rksort()
  5. What does the function array_reverse() do?
    a) Reverses keys of an array
    b) Reverses order of array elements
    c) Deletes last element
    d) Randomizes array
  6. How can you sort a multidimensional array by a specific key?
    a) array_sort_by_key()
    b) usort()
    c) array_multisort()
    d) key_sort()
  7. What will the array_unique() function do?
    a) Removes duplicate keys
    b) Removes duplicate values
    c) Merges arrays uniquely
    d) Randomizes array
  8. How do you shuffle elements in an array randomly?
    a) array_random()
    b) shuffle()
    c) randomize_array()
    d) array_sort_random()
  9. Which function can be used to compare values in two arrays?
    a) array_diff()
    b) array_compare()
    c) array_check()
    d) array_search()
  10. What is the output of the following code? $arr = array(1, 2, 3); array_push($arr, 4); echo end($arr); a) 1
    b) 3
    c) 4
    d) 2

Answer Key

QnoAnswer
1b) 0
2a) $arr = array(“apple”, “banana”, “cherry”);
3b) count($fruits)
4b) 10
5a) $fruits[] = “orange”;
6d) Both a and c
7c) $arr[“name”]
8b) 2
9a) key_exists()
10a) unset($arr[“key”]);
11b) An array containing other arrays
12a) $fruits[0][0];
13d) 40
14b) $arr[] = array(“new value”);
15b) count($arr, COUNT_RECURSIVE)
16b) Combines two arrays into one
17b) 4
18b) array_slice()
19a) array_sum()
20a) Checks if a value exists in an array
21b) sort($arr)
22a) ksort()
23c) 1
24a) rsort()
25b) Reverses order of array elements
26b) usort()
27b) Removes duplicate values
28b) shuffle()
29a) array_diff()
30c) 4

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top