MCQs on Class6
Arrays in PHP
Basic
Concepts of Arrays
1. What is
an indexed array in PHP?
o
A) An array with numeric keys
o
B) An array with string keys
o
C) An array that stores only integers
o
D) An array that stores only strings
o
Answer: A) An array with numeric keys
Explanation: Indexed arrays use numerical indices to store values.
2. What is
an associative array in PHP?
o
A) An array with numeric keys
o
B) An array with string keys
o
C) An array that can only hold integers
o
D) An array that stores values without keys
o
Answer: B) An array with string keys
Explanation: Associative arrays in PHP use string keys to store values.
3. Which
function is used to create an array in PHP?
o
A) newArray()
o
B) array()
o
C) createArray()
o
D) makeArray()
o
Answer: B) array()
Explanation: The array() function in PHP is used to define a new array.
4. How can
you access the first element of an indexed array $arr?
o
A) $arr[0]
o
B) $arr[1]
o
C) $arr[-1]
o
D) $arr[null]
o
Answer: A) $arr[0]
Explanation: In PHP, arrays are zero-indexed, so the first element is
accessed with index 0.
5. If $arr = ["Apple",
"Banana", "Cherry"], what does $arr[1] return?
o
A) Apple
o
B) Banana
o
C) Cherry
o
D) Array
o
Answer: B) Banana
Explanation: $arr[1] returns the second element in the array, which is "Banana".
6. Which
function returns the number of elements in an array?
o
A) length()
o
B) size()
o
C) count()
o
D) num()
o
Answer: C) count()
Explanation: count() returns the total number of elements in an array.
7. How do
you define an associative array in PHP?
o
A) $arr = array(1, 2, 3);
o
B) $arr = array("key1" =>
"value1", "key2" => "value2");
o
C) $arr = [1, 2, 3];
o
D) $arr = "key1" => "value1";
o
Answer: B) $arr = array("key1"
=> "value1", "key2" => "value2");
Explanation: Associative arrays use string keys to associate values.
8. What will
count(array("a"
=> "apple", "b" => "banana", "c"
=> "cherry")) return?
o
A) 0
o
B) 2
o
C) 3
o
D) 1
o
Answer: C) 3
Explanation: The count() function returns 3 because there are three elements in the array.
9. How would
you add a new element to the end of an indexed array $arr?
o
A) $arr[] = "newElement";
o
B) array_add($arr, "newElement");
o
C) $arr["newElement"]
o
D) $arr[1] = "newElement";
o
Answer: A) $arr[] = "newElement";
Explanation: In PHP, you can add a new element to an array by assigning
it to $arr[].
10.
Which PHP function removes the last element of an
array?
o
A) array_remove()
o
B) remove_last()
o
C) array_pop()
o
D) array_shift()
o
Answer: C) array_pop()
Explanation: array_pop() removes the last element of an array.
Manipulating
Arrays
11.
What does array_push($arr, "Orange") do to
the array $arr?
o
A) Removes the first element
o
B) Adds "Orange" to the end of $arr
o
C) Adds "Orange" to the beginning of $arr
o
D) Clears $arr
o
Answer: B) Adds "Orange" to
the end of $arr
Explanation: array_push() appends an element to the end of the array.
12.
Which function will insert an element at the
beginning of an array?
o
A) array_unshift()
o
B) array_shift()
o
C) array_push()
o
D) array_prepend()
o
Answer: A) array_unshift()
Explanation: array_unshift() adds an element to the start of an array.
13.
Given $arr = array("Apple", "Banana",
"Cherry");, what does array_shift($arr); do?
o
A) Adds an element at the start
o
B) Removes the last element
o
C) Removes the first element
o
D) Returns the last element
o
Answer: C) Removes the first element
Explanation: array_shift() removes the first element of the array.
14.
What function would you use to combine two arrays
into one?
o
A) array_combine()
o
B) array_merge()
o
C) array_union()
o
D) combine_array()
o
Answer: B) array_merge()
Explanation: array_merge() combines two or more arrays.
15.
What does in_array("value", $array) check?
o
A) If "value" is the key in $array
o
B) If "value" exists as a value in $array
o
C) If $array is empty
o
D) The length of $array
o
Answer: B) If "value" exists
as a value in $array
Explanation: in_array() checks if a value exists in an array.
16.
How do you remove duplicates from an array in PHP?
o
A) array_unique($array)
o
B) array_distinct($array)
o
C) array_clear($array)
o
D) array_merge_unique($array)
o
Answer: A) array_unique($array)
Explanation: array_unique() removes duplicate values in an array.
17.
Which function sorts an array in descending order?
o
A) sort($array)
o
B) rsort($array)
o
C) d_sort($array)
o
D) array_reverse($array)
o
Answer: B) rsort($array)
Explanation: rsort() sorts an array in descending order.
18.
How do you sort an associative array by its values?
o
A) sort($array)
o
B) ksort($array)
o
C) asort($array)
o
D) valuesort($array)
o
Answer: C) asort($array)
Explanation: asort() sorts an associative array by its values.
19.
What function would you use to get only the keys of
an array?
o
A) array_keys()
o
B) array_values()
o
C) array_key_value()
o
D) array_key_only()
o
Answer: A) array_keys()
Explanation: array_keys() returns all the keys of an array.
20.
Which function would you use to get the values of
an array without the keys?
o
A) array_keys()
o
B) array_values()
o
C) array_key_value()
o
D) array_only_values()
o
Answer: B) array_values()
Explanation: array_values() returns only the values in an array.
This is a selection of 20 MCQs.
Let me know if you'd like the full set of 100 MCQs across a broader range of
topics within arrays or if you want additional question variations!
4o
You said:
more from the part-1 array
ChatGPT
said:
ChatGPT
Here are additional
multiple-choice questions focusing on the basics of arrays in PHP, specifically
covering array creation, access, manipulation, and basic functions. Each
question includes an explanation.
MCQs on
PHP Arrays: Part 1
Array
Basics and Accessing Elements
21.
What is the output of $arr = [1, 2, 3]; echo $arr[1];?
o
A) 1
o
B) 2
o
C) 3
o
D) Error
o
Answer: B) 2
Explanation: $arr[1] accesses the second element of the array, which is 2.
22.
How do you access the value of the element with the
key "color" in an associative array $arr = ["color" =>
"blue", "size" => "large"]?
o
A) $arr["blue"]
o
B) $arr->color
o
C) $arr["color"]
o
D) $arr->"color"
o
Answer: C) $arr["color"]
Explanation: To access a value in an associative array, use the key in
square brackets.
23.
What is the output of echo count([10, 20, 30, 40, 50]);?
o
A) 5
o
B) 4
o
C) 10
o
D) Error
o
Answer: A) 5
Explanation: count() returns the number of elements in an array, which is 5 in this case.
24.
How would you create an empty array in PHP?
o
A) $arr = array();
o
B) $arr = [];
o
C) Both A and B
o
D) $arr = emptyArray();
o
Answer: C) Both A and B
Explanation: PHP supports creating empty arrays using either array() or [].
25.
What function returns an array’s values as a
comma-separated string?
o
A) implode()
o
B) explode()
o
C) join()
o
D) array_merge()
o
Answer: A) implode()
Explanation: implode() combines array elements into a single string,
separated by a specified delimiter.
26.
If $arr = array("fruit" =>
"apple", "color" => "red");, what
does echo $arr["size"]; output?
o
A) size
o
B) apple
o
C) red
o
D) Undefined index error
o
Answer: D) Undefined index error
Explanation: Accessing a non-existent key in an array generates an
"Undefined index" error.
Adding,
Removing, and Modifying Array Elements
27.
Which function appends one or more elements to the
end of an array?
o
A) array_merge()
o
B) array_push()
o
C) add()
o
D) append()
o
Answer: B) array_push()
Explanation: array_push() appends elements to the end of an array.
28.
What does array_unshift($arr, "New"); do to
the array $arr?
o
A) Removes the last element
o
B) Adds "New" at the start
o
C) Adds "New" at the end
o
D) Removes the first element
o
Answer: B) Adds "New" at the
start
Explanation: array_unshift() adds an element to the beginning of an array.
29.
Which function removes the first element from an
array?
o
A) array_pop()
o
B) array_shift()
o
C) remove()
o
D) unset()
o
Answer: B) array_shift()
Explanation: array_shift() removes the first element from an array.
30.
How can you delete a specific element from an
associative array by key?
o
A) remove($array, "key")
o
B) unset($array["key"])
o
C) delete($array, "key")
o
D) array_delete($array, "key")
o
Answer: B) unset($array["key"])
Explanation: unset() deletes a specific element from an array.
Array
Functions and Manipulations
31.
What is the output of array_pop($arr); if $arr = [10, 20, 30];?
o
A) Removes 10
o
B) Removes 20
o
C) Removes 30
o
D) No output
o
Answer: C) Removes 30
Explanation: array_pop() removes the last element from an array, which is 30.
32.
What function would you use to check if a key
exists in an array?
o
A) key_exists()
o
B) in_array()
o
C) array_key_exists()
o
D) isset()
o
Answer: C) array_key_exists()
Explanation: array_key_exists() checks if a specific key exists in an array.
33.
How can you merge two arrays $arr1 and $arr2 without
overwriting keys?
o
A) array_combine($arr1, $arr2)
o
B) array_merge($arr1, $arr2)
o
C) array_add($arr1, $arr2)
o
D) array_unshift($arr1, $arr2)
o
Answer: B) array_merge($arr1, $arr2)
Explanation: array_merge() combines arrays and assigns keys to prevent
overwriting.
34.
If you want to check if a value exists in an array,
which function should you use?
o
A) value_exists()
o
B) in_array()
o
C) find()
o
D) array_search()
o
Answer: B) in_array()
Explanation: in_array() checks if a specified value exists in an array.
35.
Which function returns the position of a specified
value in an array?
o
A) array_position()
o
B) find_position()
o
C) in_array()
o
D) array_search()
o
Answer: D) array_search()
Explanation: array_search() returns the index or key of a specified value in
an array.
36.
What does sort($array) do to the array?
o
A) Sorts the array in ascending order
o
B) Sorts the array in descending order
o
C) Reverses the array
o
D) Removes duplicates
o
Answer: A) Sorts the array in ascending
order
Explanation: sort() arranges elements in ascending order by default.
Indexed
Arrays vs. Associative Arrays
37.
Which of the following is a valid indexed array?
o
A) $arr = array("key1" =>
"value1", "key2" => "value2");
o
B) $arr = [10, 20, 30];
o
C) $arr = [];
o
D) Both B and C
o
Answer: D) Both B and C
Explanation: Indexed arrays have numeric keys or no initial values at
all.
38.
Which statement is true about associative arrays in
PHP?
o
A) They must have numeric keys.
o
B) They allow both numeric and string keys.
o
C) They only hold string values.
o
D) They cannot hold arrays as values.
o
Answer: B) They allow both numeric and
string keys
Explanation: Associative arrays primarily use string keys, but numeric
keys are also valid.
39.
Which function converts an array to a JSON string
in PHP?
o
A) json_encode()
o
B) array_to_json()
o
C) json_convert()
o
D) encode()
o
Answer: A) json_encode()
Explanation: json_encode() converts arrays or objects into JSON format.
40.
If you want to loop through an associative array,
which loop is commonly used?
o
A) for
o
B) foreach
o
C) while
o
D) do-while
o
Answer: B) foreach
Explanation: foreach loops through each key-value pair in an associative array.
41.
Which of these functions retrieves all values from
an associative array as an indexed array?
o
A) array_values()
o
B) array_keys()
o
C) array_map()
o
D) array_filter()
o
Answer: A) array_values()
Explanation: array_values() returns an array of all values in the original
array.
42.
If $arr = array("a" => "apple",
"b" => "banana", "c" =>
"cherry");, what is the output of echo $arr["b"];?
o
A) apple
o
B) banana
o
C) cherry
o
D) Undefined index
o
Answer: B) banana
Explanation: $arr["b"] accesses the value associated with the key "b" in the
associative array.
Let me know if you'd like more
MCQs for advanced topics!
4o
You said:
yes
ChatGPT
said:
ChatGPT
Here are additional advanced
multiple-choice questions focused on more complex array operations in PHP,
including multidimensional arrays, array functions, and array manipulation
methods.
Advanced
MCQs on PHP Arrays
Multidimensional
Arrays
43.
What is the output of the following code?
php
Copy code
$arr = [
["apple", "banana"],
["cherry", "date"]
];
echo $arr[1][0];
o
A) apple
o
B) banana
o
C) cherry
o
D) date
o
Answer: C) cherry
Explanation: $arr[1][0] accesses the first element of the second array,
which is "cherry".
44.
How do you access the value "php" in the
following array?
php
Copy code
$arr = [
"languages" => [
"web" => "php",
"scripting" => "python"
]
];
o
A) $arr[0]
o
B) $arr["languages"]["web"]
o
C) $arr["languages"][0]
o
D) $arr["web"]
o
Answer: B) $arr["languages"]["web"]
Explanation: ["languages"]["web"] accesses the "web" key
inside the "languages" array, which holds "php".
45.
What is the output of echo count($arr); if $arr is a
multidimensional array defined as [[1, 2], [3, 4], [5, 6]]?
o
A) 2
o
B) 3
o
C) 6
o
D) 5
o
Answer: B) 3
Explanation: count() returns the number of top-level elements in the array, which are 3
subarrays.
Array
Functions
46.
What function would you use to sort an array by its
values in descending order while maintaining key association?
o
A) sort()
o
B) rsort()
o
C) asort()
o
D) arsort()
o
Answer: D) arsort()
Explanation: arsort() sorts array values in descending order, preserving
key associations.
47.
What does array_slice([1, 2, 3, 4, 5], 1, 3) return?
o
A) [1, 2, 3]
o
B) [2, 3, 4]
o
C) [3, 4, 5]
o
D) [4, 5]
o
Answer: B) [2, 3, 4]
Explanation: array_slice() takes a portion starting from index 1 (second element), of length 3.
48.
What is the output of implode("-",
["PHP", "Arrays", "Basics"]);?
o
A) PHP Arrays Basics
o
B) PHP-Arrays-Basics
o
C) PHP_Arrays_Basics
o
D) PHP-Arrays Basics
o
Answer: B) PHP-Arrays-Basics
Explanation: implode() joins array elements with a specified delimiter,
here "-".
49.
How would you shuffle the elements of an array
randomly in PHP?
o
A) array_shuffle()
o
B) array_randomize()
o
C) shuffle()
o
D) random_array()
o
Answer: C) shuffle()
Explanation: shuffle() randomizes the order of elements in an array.
50.
What function can you use to combine two arrays by
using one array as keys and the other as values?
o
A) array_merge()
o
B) array_combine()
o
C) array_key_exists()
o
D) array_join()
o
Answer: B) array_combine()
Explanation: array_combine() creates a new array by combining keys from one
array with values from another.
Array
Manipulation and Searching
51.
What is the output of the following code?
php
Copy code
$arr = ["a" => 1, "b"
=> 2, "c" => 3];
echo array_search(2, $arr);
o
A) a
o
B) b
o
C) c
o
D) 2
o
Answer: B) b
Explanation: array_search() returns the key associated with the specified
value, which is "b" for 2.
52.
Which function removes duplicate values from an
array?
o
A) array_remove_duplicates()
o
B) unique()
o
C) array_unique()
o
D) distinct()
o
Answer: C) array_unique()
Explanation: array_unique() removes duplicate values from an array, keeping
the first occurrence.
53.
What does array_diff([1, 2, 3], [2, 4, 5]) return?
o
A) [2, 4, 5]
o
B) [1, 3]
o
C) [2]
o
D) [1, 2, 3, 4, 5]
o
Answer: B) [1, 3]
Explanation: array_diff() returns values in the first array that are not in
the second, which are 1 and 3.
54.
How can you remove the last element from an array
in PHP?
o
A) array_shift()
o
B) array_pop()
o
C) unset()
o
D) array_delete()
o
Answer: B) array_pop()
Explanation: array_pop() removes the last element from an array.
55.
Which function checks if a specific value exists in
an array?
o
A) array_key_exists()
o
B) array_search()
o
C) in_array()
o
D) array_value_exists()
o
Answer: C) in_array()
Explanation: in_array() checks if a specified value is in an array.
Working
with Associative Arrays
56.
Given $arr = ["x" => 1, "y" =>
2, "z" => 3];, what is the output of echo
array_key_exists("y", $arr);?
o
A) 1
o
B) 0
o
C) y
o
D) Error
o
Answer: A) 1
Explanation: array_key_exists() returns true (or 1) if the specified key exists in
the array.
57.
Which function returns all the keys of an
associative array as a new indexed array?
o
A) array_values()
o
B) array_keys()
o
C) array_key_values()
o
D) key_array()
o
Answer: B) array_keys()
Explanation: array_keys() returns an array containing all keys from the
associative array.
58.
What is the output of the following code?
php
Copy code
$arr = ["a" => "apple",
"b" => "banana"];
echo isset($arr["c"]);
o
A) apple
o
B) banana
o
C) null
o
D) Empty string
o
Answer: D) Empty string
Explanation: isset() returns false, and echoing false outputs an empty string.
Array
Sorting and Reordering
59.
Which function sorts an array in ascending order
but does not maintain key association?
o
A) asort()
o
B) ksort()
o
C) sort()
o
D) natsort()
o
Answer: C) sort()
Explanation: sort() arranges values in ascending order without maintaining key association.
60.
What function can be used to sort an array in
reverse order and maintain key association?
o
A) ksort()
o
B) asort()
o
C) arsort()
o
D) rsort()
o
Answer: C) arsort()
Explanation: arsort() sorts the array in descending order, keeping key
associations intact.
Advanced
Array Functions
61.
Which function applies a specified callback
function to each element of an array?
o
A) array_callback()
o
B) array_map()
o
C) array_apply()
o
D) array_exec()
o
Answer: B) array_map()
Explanation: array_map() applies a callback function to each element in one
or more arrays.
62.
If you want to filter elements of an array based on
a condition, which function should you use?
o
A) array_filter()
o
B) array_map()
o
C) array_sort()
o
D) filter_array()
o
Answer: A) array_filter()
Explanation: array_filter() returns a new array containing elements that pass
a specified condition.