Sample questions
Question 1Medium
What elements will the following script output?
< ?php
$array = array (true => 'a', 1 => 'b');
var_dump ($array);
?>
- 1 => 'b'
- True => 'a', 1 => 'b'
- 0 => 'a', 1 => 'b'
- It will output NULL
Answer: A. 1 => 'b'
Explanation
true becomes key 1, so the array has only 1 => 'b'; the question text has no $aray typo
Step-by-step solution
- In PHP, the key true is converted to the integer key 1.
- The second item also uses key 1, so it replaces the value 'a'.
- Only one element is left: key 1 with value 'b'.
- So the output is 1 => 'b', option A.
Why the other options are wrong
- B. true is converted to key 1, not kept as the text True.
- C. There is no key 0; true becomes key 1.
- D. var_dump prints the array, not NULL.
Tip: In PHP array keys, true becomes 1 and a later key with the same value overwrites the earlier one.
Question 2Medium
- Used to convert a string to an array.
- Used to split a given string into the number of chunks specified
- Used to split a string by a string
- Used to split string into two equal halves.
Answer: C. Used to split a string by a string
Explanation
explode " Split a string by string.
e.g. array explode ( string $delimiter, string $string [, int $limit ] ) Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.
Step-by-step solution
- Explode splits a string into an array, using a delimiter string.
- The explanation says it splits a given string by a string.
- So option C, split a string by a string, is the answer.
Why the other options are wrong
- A. Too vague; explode splits on a delimiter, which option C names exactly.
- B. Explode does not split into a number of chunks; it splits on a delimiter.
- D. Explode splits on a delimiter, not into two equal halves.
Tip: Explode splits a string by a delimiter; implode joins an array back into a string.
Question 3Medium
Array values are keyed by ___ values (called indexed arrays) or using ___ values (called associative arrays). Of course, these key methods can be combined as well.
- Float, string
- Positive number, negative number
- Even number, string
- Integer, string
Answer: D. Integer, string
Explanation
Arrays that are keyed by integer values are called indexed arrays, while those keyed by strings are called associative arrays. The correct answer is, therefore, D.
Step-by-step solution
- Indexed arrays use integer keys.
- Associative arrays use string keys.
- Integer and string are the two key types.
- Combining both is possible.
- Final answer: Integer, string.
Why the other options are wrong
- A. Float keys are not used for indexed arrays; indexed arrays use integer keys.
- B. Positive and negative numbers are not the key types; PHP uses integer and string keys.
- C. Even numbers are not the key type; indexed arrays use integer keys, not only even ones.
Tip: Remember: integer for indexes, string for associations.
Question 4Medium
What function computes the difference of arrays?
- array_diff
- diff_array
- arrays_diff
- diff_arrays
Answer: A. array_diff
Explanation
The array_diff function is used to compute the difference between arrays by returning elements that are present in one array but not in others. This makes it the correct choice for finding the difference of arrays.
Step-by-step solution
- The PHP function that computes the difference between arrays is array_diff.
- The other names (diff_array, arrays_diff, diff_arrays) are not PHP functions.
- So the correct answer is array_diff, option A.
Why the other options are wrong
- B. Diff_array is not a PHP function name.
- C. Arrays_diff is not a PHP function name.
- D. Diff_arrays is not a PHP function name.
Tip: Most PHP array functions start with array_, for example array_diff, array_keys and array_merge.
Question 5Medium
How do I create PHP arrays in HTML <form>?
- < input name= MyArray[]/>
- < input ="MyArray[]" />
- < input name="MyArray[]" />
- < input MyArray[] />
Answer: C. < input name="MyArray[]" />
Explanation
To get your <form> result sent as an array to your PHP script you name the <input>, <select> or <textarea> elements like this:
<input name="MyArray[]" />
Step-by-step solution
- A name ending in [] tells PHP to collect the submitted values into an array.
- The name attribute must be quoted: name="MyArray[]" on the input element.
- Option C has the correct form, so it is the correct answer.
Why the other options are wrong
- A. The name value is not quoted, so the attribute is malformed.
- B. It has no name attribute, so the field is not named MyArray[].
- D. The name value is not quoted, so the attribute is malformed.
Tip: Add [] to a form field name, such as name="MyArray[]", to send repeated values to PHP as an array.
Question 6Medium
Absent any actual need for choosing one method over the other, does passing arrays by value to a read-only function reduce performance compared to passing them by reference?
- Yes, because the interpreter must always create a copy of the array before passing it to the function.
- Yes, but only if the function modifies the contents of the array.
- Yes, but only if the array is large.
- No.
Answer: D. No.
Explanation
As a general rule, however, passing an array by reference to a function that does not modify its contents is actually slower than passing it by value, since PHP must create a set of structures that it uses to maintain the reference.
Because PHP uses a lazy-copy mechanism (also called copy-on-write) that does not actually create a copy of a variable until it is modified, passing an array by value is a very fast and safe method of sharing an array with a function and, therefore answer D is correct.
Step-by-step solution
- The question asks whether passing arrays by value to a read-only function is slower than passing by reference.
- PHP uses copy-on-write, so a copy is made only when the array is changed.
- Passing by reference adds overhead to keep the reference structures, so by value is not slower here.
- The correct answer is option D, No.
Why the other options are wrong
- A. PHP does not copy the array when it is passed; the copy waits until a write happens.
- B. A copy is made only on a write, so a read-only function does not trigger it.
- C. Size does not matter here, because no copy is made for reads at any size.
Tip: With copy-on-write, passing arrays by value is cheap unless the function changes them.
Question 7Medium
Which array function checks if the specified key exists in the array?
- array_key_exist()
- array_key_exists()
- array_keys_exists()
- arrays_key_exists()
Answer: B. array_key_exists()
Explanation
The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.
Step-by-step solution
array_key_exists() is the correct function to check if a key exists in an array.- It returns true if the key exists, otherwise false.
- Other options have incorrect spelling or syntax.
- The function name must be exactly '
array_key_exists()'.
Why the other options are wrong
- A. array_key_exist() is misspelled; the function is array_key_exists(), with an s.
- C. array_keys_exists() is misspelled; the function is array_key_exists().
- D. arrays_key_exists() is misspelled; the function is array_key_exists().
Tip: Remember: 'array_key_exists()' is the correct function name.
Question 8Medium
There are three different kind of arrays:
- Numeric array, String array, Multidimensional array
- Numeric array, Associative array, Dimensional array
- Numeric array, Associative array, Multidimensional array
- Const array, Associative array, Multidimensional array
Answer: C. Numeric array, Associative array, Multidimensional array
Explanation
There are three different kind of arrays and each array value is accessed using an ID c which is called array index.
Numeric array - An array with a numeric index. Values are stored and accessed in linear fashion
Associative array - An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
Multidimensional array - An array containing one or more arrays and values are accessed using multiple indices
Step-by-step solution
- The question asks for the three kinds of arrays in PHP.
- They are numeric arrays (numeric index), associative arrays (string keys) and multidimensional arrays (arrays inside arrays).
- Option C lists Numeric, Associative and Multidimensional, the correct answer.
Why the other options are wrong
- A. String array is not a kind of PHP array; the right name is associative array.
- B. Dimensional array is not a standard kind; the right name is multidimensional array.
- D. Const array is not a kind of PHP array; numeric array is the right name.
Tip: PHP has three array kinds: indexed (numeric), associative (string keys) and multidimensional (arrays of arrays).
Question 9Medium
PHP supports both simple and multi dimensional arrays.
- True
- False
Answer: A. True
Explanation
An array can also contain another array as a value, which in turn can hold other arrays as well. In such a way we can create two- or three-dimensional arrays.
Step-by-step solution
- A PHP array can hold another array as one of its values.
- This nesting gives two-dimensional and three-dimensional arrays, which are multi-dimensional arrays.
- So the statement is True.
Why the other options are wrong
- B. False is wrong, because PHP arrays can be nested to make multi-dimensional arrays.
Tip: A value in a PHP array can itself be an array.
Question 10Medium
Assume you would like to sort an array in ascending order by value while preserving key associations. Which of the following PHP sorting functions would you use?
- ksort()
- asort()
- krsort()
- sort()
Answer: B. asort()
Explanation
Only the asort function sorts an array by value without destroying index associations. Therefore, Answer B is correct.
Step-by-step solution
asort() sorts arrays by values while keeping keys intact.- Other functions like
sort() destroy key associations. ksort() and krsort() sort by keys, not values.- The question asks for sorting by value, so
asort() is correct. - Final answer:
asort()
Why the other options are wrong
- A. ksort() sorts by key, not by value, so it does not sort the values the question asks about.
- C. krsort() sorts by key in descending order, so it does not sort values ascending.
- D. sort() sorts values but drops the key associations, which the question needs kept.
Tip: Use asort() for value-based sorting with key preservation.
Practise all 12 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →