Are you ready?
11 questions to solve
Instructions
- This quiz goes to full-screen once you press the Start button.
- At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
Identify the error in the code below:
<?php
$a = [1, 2, 3];
$a[3] = 'Hello';
There's absolutely no error in the code shown. Hence, the correct choice is (C). For more info, refer to PHP Arrays — Basics — Adding elements.
What is the value of
$a
after the execution of the following code?<?php
$a = [1, 2, 3];
$a[] = 4;
The statement
$a[] = 4
adds the value 4
to the end of the array $a
. This makes $a
equal to [1, 2, 3, 4]
. Hence, the correct choice is (C). For more info, refer to PHP Arrays — Basics — Adding elements.What does the following code output?
<?php
$a = [];
array_push($a, [1, 2]);
print_r($a);
array_push($a, [1, 2])
adds the array [1, 2]
to the end of $a
. The output of $a
therefore resembles the one shown in choice (C). For more info, refer to PHP Arrays — Basics — Adding elements.Which of the following can be used to remove the last element from an array
$arr
?We're not concerned with removing the element in a way such that the subsequent addition of an element happens on the correct index.
array_pop()
is an answer for sure, hence the choice (A). Talking about unset($arr[count($arr) - 1])
, the question's description clearly states that we're not concerned with such a removal that a subsequent addition of element gets done on the correct index; likewise choice (E) is also correct. For more info, refer to PHP Arrays — Basics — Removing elements.Determine the length of
$arr
after the following code executes:<?php
$arr = [1, 0];
unset($arr[0]);
unset($arr[0])
removes the key 0
from $arr
. Consequently, the length of $arr
becomes 1. Thus, the correct choice is (A). For more info, refer to PHP Arrays — Basics — Removing elements.An indexed array in PHP is technically also an associative array. True or false?
That's absolutely true. Indexed arrays in PHP are also associative arrays — their keys are simply indexes while the corresponding values are the elements of the indexed arrays. For more info, refer to PHP Arrays — Basics — Associative arrays.
What does the following code output?
<?php
$a = [1, 2];
print_r(array_keys($a));
array_keys($a)
returns an indexed array containing all the keys of $a
. This gives us [0, 1]
. The output that resembles this array is the one in choice (A). For more info, refer to PHP Arrays — Basics — array_keys()
.Determine the value of
$c
after the following code executes:<?php
$a = [1, 2];
$b = [3, 4];
$c = $a + $b;
$a + $b
performs the union of $a
and $b
based on their keys, with the keys of $a
overriding those of $b
. This gives us [1, 2]
. Hence, the correct choice is (A). For more info, refer to PHP Arrays — Basics — Array operators.What does
[1] === [1]
return?[1] === [1]
returns true
since the identity operator (===
) compares two arrays based on the individual key-value pairs they contain (in this case, both [1]
and [1]
contain the same key-value pairs). For more info, refer to PHP Arrays — Basics — Array operators.What does the following code output?
<?php
$a = [10, 20];
var_dump(in_array(true, $a));
Surprisingly enough,
in_array(true, $a)
returns true
. That's because in_array()
, by default, compares the given value with each array element loosely, i.e. using the semantics of the equality (==
) operator, and true == 10
is a truthy expression! For more info, refer to PHP Arrays — Basics — Searching elements.What does the following code output?
<?php
$nums = [-5, 1];
var_dump(array_search(true, $nums));
var_dump(array_search(true, $nums, true));
The first
Hence, the correct choice is (D). For more info, refer to PHP Arrays — Basics — Searching elements.
array_search()
call indeed finds true
in $nums
in the number -5
, by virtue of loose comparisons, and returns 0
. The second array_search()
call, however, doesn't find a match for true
in $nums
, by virtue of strict comparisons (enabled by the third true
argument), and returns false
.Hence, the correct choice is (D). For more info, refer to PHP Arrays — Basics — Searching elements.