PHP Arrays Aptitude Questions and Answers

PHP Arrays Aptitude: This section contains aptitude questions and answers on PHP Arrays. By Nidhi Last updated : December 15, 2023

This section contains Aptitude Questions and Answers on PHP Arrays.

1) There are the following statements that are given below, which of them are correct about Arrays in PHP?
  1. An array is used to store multiple data elements in a single variable.
  2. In PHP, we can create an array of strings.
  3. In PHP, the index of an array starts from 0.
  4. In PHP, the index of an array starts from 1.

Options:

  1. A
  2. A and C
  3. A, B, and C
  4. A, B, and D

Correct answer: 2
A and C

Explanation:

An array is used to store similar types of data elements in a single variable, the index of array elements starts from 0 to (N-1), where N denotes the total number of elements.


2) Which of the following function is used to create an array in PHP?
  1. arr()
  2. p_array()
  3. array()
  4. php_array()

Correct answer: 3
array()

Explanation:

The array() function is used to create an array in PHP.

$arr_name = array("MP", "UP", "AP");

The array() function is used to create an array in PHP.

Index 0:    "MP"
Index 1:    "UP"
Index 2:    "AP" 

3) Which of the following are correct types of the array used in PHP?
  1. Indexed arrays
  2. Associative arrays
  3. Multidimensional arrays
  4. All of the above

Options:

  1. A and B
  2. A and C
  3. B and C
  4. D

Correct answer: 4
D

Explanation:

PHP supports three types of arrays:

  1. Indexed or numeric arrays
  2. Associative arrays
  3. Multi-dimensional arrays

4) What is the correct output of the given code snippets in PHP?
<?php
$arr_state = array(
    "MP",
    "UP",
    "AP"
);
print "$arr_state[0], $arr_state[2], $arr_state[1]";
?>
  1. MP, UP, AP
  2. MP, AP, UP
  3. Error
  4. NmpageTU of the above

Correct answer: 2
MP, AP, UP

Explanation:

In the above code, we print array element using given print function:

print "$arr_state[0], $arr_state[2], $arr_state[1]";

The above line first prints MP then it prints AP because AP is at 2 indexes and then we print UP. Then the output will be like this:

MP, AP, UP

5) What is the correct output of given code snippets in PHP?
<?php
$arr_state = array(
    "MP",
    "UP",
    "AP"
);
print "$arr_state[0], $arr_state[1], $arr_state[3]";
?>
  1. MP, UP,
  2. MP, UP, AP
  3. Error
  4. NmpageTU of the above

Correct answer: 1
MP, UP,

Explanation:

In the above code we tried to access an element of 3rd index, but there are only three elements from index 0 to 2, and we are trying to access element out of bound, but there is no any error will occur and also will not print anything on the webpage.


6) What is the correct output of given code snippets in PHP?
<?php
$arr_state = array(
    "MP",
    "UP",
    "AP"
);
print "$arr_state[0], $arr_state[1], ";

if ($arr_state[3] == NULL) print "DELHI";
?>
  1. MP, UP,
  2. MP, UP, AP
  3. MP, UP, DELHI
  4. Error

Correct answer: 3
MP, UP, DELHI

Explanation:

In the above code we tried to access the element of the 3rd index, but there are only three elements from index 0 to 2, and we are trying to access elements out of bound.

Here, $arr_state[3] contains a NULL value, then we compared it with a NULL value, and the condition gets true then it will print "DELHI" after "MP, UP, " on the web page.


7) What is the correct output of given code snippets in PHP?
<?php
$arr_state = array(
    "MP",
    "UP",
    "AP"
);
print "$arr_state[0], $arr_state[1], ";

if ($arr_state[3] == '') print "DELHI";
?>
  1. MP, UP,
  2. MP, UP, AP
  3. MP, UP, DELHI
  4. Error

Correct answer: 3
MP, UP, DELHI

Explanation:

In the above code we tried to access the element of the 3rd index, but there are only three elements from index 0 to 2, and we are trying to access elements out of bound.

Here, $arr_state[3] contains a NULL value, then we compared it with an empty single quote(''), and the condition gets true then it will print DELHI after "MP, UP, " on the web page. Because we can also check NULL value with empty single quotes.


8) A NULL is equivalent to space in PHP?
  1. Yes
  2. No

Correct answer: 1
Yes

Explanation:

A NULL is not equivalent to space in PHP. A NULL value denotes the un-availability of the value.


9) Which of the following function is used to get total number of elements present in an array?
  1. length()
  2. count()
  3. arr_len()
  4. arr_count()

Correct answer: 2
count()

Explanation:

The count() function is used to get total number of elements present in the specified array. We can understand count() function using below code,

<?php
$arr_state = array(
    "MP",
    "UP",
    "AP"
);
print count($arr_state);
?>

10) What is the correct output of the given code snippets in PHP?
<?php
$arr_state = array(
    "MP",
    "UP",
    "AP"
);
print "count($arr_state)";
?>
  1. 3
  2. count(Array)
  3. count($arr_state)
  4. Error

Correct answer: 2
count(Array)

Explanation:

The above will not print "3", because in the print statement we enclosed count() function with double quotes then it will be treated as a normal string, the array name print "Array", then final output will be: "count(Array)".


11) What is the correct output of the given code snippets in PHP?
<?php
$arr = array(
    5,
    7,
    6,
    12,
    4,
    16,
    3
);

$large = 0;
for ($i = 0;$i < count($arr);$i++)
{
    if ($large < $arr[$i]) $large = $arr[$i];
}
echo "$large";
?>
  1. 16
  2. 12
  3. Error
  4. NmpageTU of the above

Correct answer: 1
16

Explanation:

The above code will print "16" on the webpage which is the largest number in the array.

To find the largest element, first, we initialized $large with 0, and then we compared with array elements, if any element is greater than $large then we assign greater value to the $large so that after completing the whole iteration we got the largest value in the $large variable.


12) What is the correct output of the given code snippets in PHP?
<?php
$arr = array(
    5,
    7,
    6,
    12,
    4,
    16,
    3
);

$large = $arr[0];
for ($i = 1;$i < count($arr);$i++)
{
    if ($arr[$i] > $large) $large = $arr[$i];
}

echo "$large";
?>
  1. 16
  2. 12
  3. Error
  4. NmpageTU of the above

Correct answer: 1
16

Explanation:

The above code will print "16" on the webpage which is the largest number in the array. Here we optimized the logic.

To find the largest element, here we initialized $large with 1st element of the array, and then we compared with array elements from index 1, if any element is greater than $large then we assign greater value to the $large so that after completing the whole iteration we got the largest value in the $large variable.


13) Can we store different types of data elements in a single array in PHP?
  1. Yes
  2. No

Correct answer: 1
Yes

Explanation:

Yes, we can store different types of data elements in a single array; we can understand it by below example:

<?php
$arr = array(
    1,
    2,
    "india"
);

for ($i = 0;$i < count($arr);$i++)
{
    print $arr[$i] . " ";
}
?>

Here an array $arr contains both integer and string elements. The output of the above code is: "1 2 india".


14) What is the correct output of given code snippets?
<?php
$arr = array(
    1,
    2,
    "india"
);
$r_arr = reverse($arr);

for ($i = 0;$i < count($arr);$i++)
{
    print $r_arr[$i] . " ";
}
?>
  1. 1 2 india
  2. India 2 1
  3. Error
  4. NmpageTU of the above

Correct answer: 3
Error

Explanation:

The above code will generate an error, The reverse() in not a built-in function, if we want to reverser an array then we can use built-in function array_reverse() then the correct code will be like this:

<?php
$arr = array(
    1,
    2,
    "india"
);

$r_arr = array_reverse($arr);
for ($i = 0;$i < count($arr);$i++)
{
    print $r_arr[$i] . " ";
}
?>

15) There are the following statements that are given below, which of them are correct about Associative Arrays in PHP?
  1. Associative arrays are used to store data elements based on specified keys.
  2. The "=>" operator is used in associate arrays to store data elements.
  3. Associative arrays are also known as numeric arrays in PHP.
  4. All of the above

Options:

  1. A and B
  2. A and C
  3. B and C
  4. D

Correct answer: 1
A and B

Explanation:

In the PHP, an Associative array is used to store data elements based on specified keys, and the "=>" operator is used in associate arrays to store data elements.


16) What is the correct output of the given code snippets in PHP?
<?php
$salary = array(
    "EMP1" => "10000",
    "EMP2" => "12000",
    "EMP3" => "15000"
);

echo $salary["EMP1"] . " " . $salary["EMP2"];
?>
  1. 10000 12000
  2. Garbage value
  3. Error
  4. NmpageTU of the above

Correct answer: 1
10000 12000

Explanation:

The above code will print "10000 12000" on the webpage, Here we used an associative array that contains "EMP1" and "EMP2" keys with different values, and then we print them using echo statements.


17) What is the correct output of the given code snippets in PHP?
<?php
$salary = array(
    "EMP1" => "10000",
    "EMP1" => "12000",
    "EMP3" => "15000"
);

echo $salary["EMP1"] . " " . $salary["EMP3"];
?>
  1. 10000 12000
  2. 10000 15000
  3. 12000 15000
  4. Error

Correct answer: 3
12000 15000

Explanation:

The above code will print "12000 15000" on the web page because we used the "EMP1" key twice here "EMP1" key assigned by the latest values to assignment, then it will print "12000 15000" on the web page.


18) What is the correct output of the given code snippets in PHP?
<?php
$salary = array(
    "EMP1" => "10000",
    "EMP2" => "12000",
    "EMP3" => "15000"
);

echo $salary['EMP1'] . " " . $salary['EMP2'];
?>
  1. 10000 12000
  2. Garbage value
  3. Error
  4. NmpageTU of the above

Correct answer: 1
10000 12000

Explanation:

The above code will print "10000 12000" on the webpage, Here we used an associative array that contains "EMP1" and "EMP2" keys with different values.

We can use a single quote and double quote to access elements of an associative array.


19) What is the correct output of the given code snippets in PHP?
<?php
$salary = array(
    "EMP1" => "10000",
    "EMP2" => "12000",
    "EMP3" => "15000"
);

echo "$salary["EMP1"], $salary["EMP2"]";
?>
  1. 10000 12000
  2. Garbage value
  3. Error
  4. NmpageTU of the above

Correct answer: 3
Error

Explanation:

The above code will generate an error due to the echo statement, here we used double quotes to print data using echo statement and we also used a double quote with array keys, this is not a proper way. We must use the below statement to print array elements:

echo $salary['EMP1'].",  ".$salary['EMP2'];

20) What is the correct output of the given code snippets in PHP?
<?php
$salary = array(
    "E1" => "10000",
    "E2" => "12000",
    "E3" => "15000"
);

foreach ($salary as $key => $value)
{
    echo $key . "=>" . $value . " ";
}
?>
  1. E1=>10000 E2=>12000 E3=>15000
  2. Garbage value
  3. Error
  4. NmpageTU of the above

Correct answer: 1
E1=>10000 E2=>12000 E3=>15000

Explanation:

The above code will print "E1=>10000 E2=>12000 E3=>15000 " on the web page, here we access each element of the array. We can access keys and values using the foreach loop.


21) What is the correct output of the given code snippets in PHP?
<?php
$salary = array(
    "E1" => "10000",
    "E1" => "12000",
    "E3" => "15000"
);

foreach ($salary as $key => $value)
{
    echo $key . "=>" . $value . " ";
}
?>
  1. E1=>10000 E2=>12000 E3=>15000
  2. E1=>12000 E3=>15000
  3. Error
  4. NmpageTU of the above

Correct answer: 2
E1=>12000 E3=>15000

The above code will print "E1=>12000 E3=>15000 " on the web page because E1 key assigned with the latest value then here we got only two elements with associative keys E1 and E3. Then we access elements using the foreach loop and print them using the echo statement.


22) What is the correct output of the given code snippets?
<?php
$mult_arr = array(
    array(
        1,
        2,
        3
    ) ,
    array(
        "ABC",
        "PQR",
        "XYZ"
    ) ,
    array(
        10.2,
        11.3,
        12.4
    )
);

print $mult_arr[0][1] . " " . $mult_arr[1][2] . " " . $mult_arr[2][1];
?>
  1. 2 XYZ 11.3
  2. Error
  3. Garbage value
  4. NmpageTU of the above

Correct answer: 1
2 XYZ 11.3

The above code will print "2 XYZ 11.3" on the web page, here we used a multidimensional array, and we access elements using indexes and print them using the print statement.


23) Which of the following function is used to sort an index based array in the ascending order?
  1. asort()
  2. rsort()
  3. sort()
  4. ksort()

Correct answer: 3
sort()

Explanation:

The sort() function is used to sort an index based array in the ascending order. Suppose we have an array $arr then we sort $arr using below statement:
sort($arr);


24) Which of the following function is used to sort an index based array in the descending order?
  1. asort()
  2. rsort()
  3. dsort()
  4. ksort()

Correct answer: 2
rsort()

Explanation:

The rsort() function is used to sort an index based array in the descending order. Suppose we have an array $arr then we sort $arr using below statement:
rsort($arr);


25) Which of the following function is used to sort an associative array in the ascending order?
  1. as_sort()
  2. a_sort()
  3. d_sort()
  4. ksort()

Correct answer: 4
ksort()

Explanation:

The ksort() function is used to sort an associative array in the ascending order. Suppose we have an array $arr then we sort $arr using below statement:
ksort($arr);


Learn PHP programming with our PHP tutorial.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.