MCQs on Arrays and Advanced Array Techniques | PHP Intermediate

PHP arrays are essential in data management for dynamic websites. Exploring advanced array techniques like associative arrays, iteration, multidimensional handling, and powerful filtering and mapping functions will elevate your PHP proficiency.


Multiple Choice Questions (MCQs)

1. Associative Arrays Advanced Concepts

  1. How do you add a new key-value pair to an existing associative array in PHP?
    a) $arr = ["key" => "value"];
    b) $arr["key"] = "value";
    c) array_push($arr, "key", "value");
    d) add_key($arr, "key", "value");
  2. Which function is used to check if a key exists in an associative array?
    a) key_exists()
    b) array_key_exists()
    c) in_array()
    d) isset()
  3. How do you remove a key-value pair from an associative array?
    a) remove($arr["key"]);
    b) array_delete($arr, "key");
    c) unset($arr["key"]);
    d) array_pop($arr["key"]);
  4. How do you change the value of an existing key in an associative array?
    a) $arr["key"] = new_value;
    b) update($arr["key"], new_value);
    c) $arr->key = new_value;
    d) replace($arr["key"], new_value);
  5. Which function is used to get the keys of an associative array?
    a) array_keys()
    b) key_values()
    c) array_get_keys()
    d) assoc_keys()

2. Array Iteration with foreach

  1. Which of the following is the correct syntax to iterate over an indexed array with foreach?
    a) foreach($arr as value) { ... }
    b) foreach($arr, value) { ... }
    c) foreach($arr => value) { ... }
    d) foreach(value as $arr) { ... }
  2. How do you iterate over both keys and values in an associative array using foreach?
    a) foreach($arr as $key => $value) { ... }
    b) foreach($arr as $value => $key) { ... }
    c) foreach($arr as $key, $value) { ... }
    d) foreach($arr as $key + $value) { ... }
  3. Which statement correctly iterates over a multidimensional array in PHP?
    a) foreach($arr as $key => $value) { foreach($value as $subKey => $subValue) { ... } }
    b) foreach($arr as $key) { foreach($arr[$key] as $subKey => $subValue) { ... } }
    c) foreach($arr[$key] as $subValue) { ... }
    d) foreach($arr as $subKey => $subValue) { ... }
  4. How can you access both key and value of an indexed array during iteration with foreach?
    a) $arr[$key] => $value
    b) $arr[] => $value
    c) foreach($arr as $value) { echo $value; }
    d) foreach($arr as $key => $value) { ... }
  5. What happens if you modify an array element inside a foreach loop?
    a) The array will be modified permanently
    b) The change will only apply within the loop
    c) It throws an error
    d) The loop will skip the element

3. Multidimensional Arrays Handling

  1. Which function allows you to sort a multidimensional array by a particular column?
    a) array_sort_by()
    b) array_column_sort()
    c) array_multisort()
    d) multisort()
  2. How can you retrieve a specific value from a multidimensional array?
    a) $arr[key1][key2];
    b) $arr[key1, key2];
    c) $arr->key1->key2;
    d) array[$key1, $key2];
  3. Which of the following methods helps in adding elements to a specific index of a multidimensional array?
    a) array_push($arr[$index], $value);
    b) array_insert($arr[$index], $value);
    c) $arr[$index][] = $value;
    d) $arr[$index] = $value;
  4. How do you count the total number of elements in a multidimensional array?
    a) count($arr, COUNT_RECURSIVE);
    b) count($arr);
    c) size($arr);
    d) total($arr);
  5. What will be the output of the following code? $arr = array( "first" => array("x" => 10, "y" => 20), "second" => array("a" => 30, "b" => 40) ); echo $arr["first"]["x"]; a) 10
    b) 20
    c) 30
    d) 40

4. Array Functions for Filtering and Mapping

  1. Which function is used to filter an array using a callback function?
    a) filter_array()
    b) array_map()
    c) array_filter()
    d) map_array()
  2. What does the array_map() function do?
    a) Sorts the array
    b) Modifies each element in an array based on a callback
    c) Merges two arrays
    d) Filters elements based on conditions
  3. Which of the following is the correct syntax for using array_filter()?
    a) array_filter($arr, function($value) { return $value > 10; });
    b) array_filter($arr, function($key) { return $key == "name"; });
    c) array_filter(function($value) { return $value; }, $arr);
    d) array_filter($arr, "key");
  4. How can you remove null values from an array using PHP?
    a) array_remove($arr, null);
    b) array_filter($arr, function($value) { return !is_null($value); });
    c) array_delete_null($arr);
    d) array_trim($arr);
  5. Which function will return all unique elements in an array?
    a) array_filter()
    b) array_unique()
    c) array_deduplicate()
    d) array_trim()

5. Miscellaneous Array Operations

  1. How do you merge two arrays in PHP?
    a) array_combine($arr1, $arr2);
    b) array_add($arr1, $arr2);
    c) array_merge($arr1, $arr2);
    d) merge($arr1, $arr2);
  2. What will the following code output?
    $arr = array(1, 2, 3); $arr2 = array(4, 5); $result = array_merge($arr, $arr2); echo count($result); a) 3
    b) 4
    c) 5
    d) 6
  3. What is the purpose of the array_splice() function?
    a) Sorts an array
    b) Adds elements to an array
    c) Removes or replaces a portion of an array
    d) Combines arrays
  4. How can you check if an array is empty?
    a) is_empty($arr);
    b) empty($arr);
    c) is_null($arr);
    d) check_empty($arr);
  5. Which function will check if a value exists in an array?
    a) array_in()
    b) exists_in_array()
    c) in_array()
    d) array_find()
  6. How do you get the first element of an array?
    a) array_first($arr);
    b) array_shift($arr);
    c) $arr[0];
    d) array_pop($arr);
  7. Which function is used to reverse an array in PHP?
    a) array_reverse()
    b) reverse_array()
    c) array_reverse_elements()
    d) array_revert()
  8. What will be the output of the following code?phpCopy code$arr = array(1, 2, 3); $arr[] = 4; array_pop($arr); echo end($arr); a) 3
    b) 4
    c) 2
    d) 1
  9. How do you reset the array pointer to the first element?
    a) reset($arr);
    b) array_reset($arr);
    c) array_first($arr);
    d) pointer_reset($arr);
  10. How do you extract a portion of an array from a specific position?
    a) array_slice($arr, 1);
    b) slice($arr, 1);
    c) array_part($arr, 1);
    d) extract($arr, 1);

Answer Key

QnoAnswer
1b) $arr["key"] = "value";
2b) array_key_exists()
3c) unset($arr["key"]);
4a) $arr["key"] = new_value;
5a) array_keys()
6a) foreach($arr as value) { ... }
7a) foreach($arr as $key => $value) { ... }
8a) foreach($arr as $key => $value) { foreach($value as $subKey => $subValue) { ... } }
9d) foreach($arr as $key => $value) { ... }
10a) The array will be modified permanently
11c) array_multisort()
12a) $arr[key1][key2];
13a) array_push($arr[$index], $value);
14a) count($arr, COUNT_RECURSIVE);
15a) 10
16c) array_filter()
17b) Modifies each element in an array based on a callback
18a) array_filter($arr, function($value) { return $value > 10; });
19b) array_filter($arr, function($value) { return !is_null($value); });
20b) array_unique()
21c) array_merge($arr1, $arr2);
22b) 4
23c) Removes or replaces a portion of an array
24b) empty($arr);
25c) in_array()
26b) array_shift($arr);
27a) array_reverse()
28a) 3
29a) reset($arr);
30a) array_slice($arr, 1);

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