}
? | PHP | MYTAT"> }
? | PHP | MYTAT">
Q
What will be the output of the following PHP code?
<?php
$arr = array(10, 20, 30, 40, 50);
$slice = array_slice($arr, 2);
foreach ($slice as $item) {
echo $item . ' ';
}
?

Answer & Solution

Answer: Option C
Solution:
The output of the code will be 30 40 50 because the array_slice() function extracts a slice of the array $arr starting from the element at index 2 (30) to the end, creating a new array $slice. The foreach loop then iterates over the sliced array, printing each item, resulting in the output 30 40 50.
Related Questions on Average

Which PHP function is used to merge two or more arrays into a single array?

A). merge()

B). combine()

C). concat()

D). array_merge()

Which PHP construct is commonly used to iterate over elements of an array?

A). foreach loop

B). while loop

C). do-while loop

D). for loop

What will be the output of the following PHP code?
<?php
$colors = array('Red', 'Green', 'Blue');
array_push($colors, 'Yellow', 'Orange');
foreach ($colors as $color) {
echo $color . ' ';
}
?

A). Red Green Blue Yellow Orange

B). Yellow Orange Red Green Blue

C). Green Blue Red Yellow Orange

D). Yellow Orange

What is the correct way to access the first element of an indexed array in PHP?

A). $array[0]

B). $array[1]

C). $array['first']

D). $array['0']

Which PHP function is used to determine the number of elements in an array?

A). size()

B). count()

C). length()

D). elements()

What will be the output of the following PHP code?
<?php
$numbers = array(10, 20, 30, 40, 50);
array_pop($numbers);
foreach ($numbers as $number) {
echo $number . ' ';
}
?

A). 20 30 40 50

B). 10 20 30 40

C). 10 20 30 40

D). 10 20 30 40 50

Which of the following statements is true about associative arrays in PHP?

A). They only allow numeric indices

B). They store elements in a sequential order

C). They use key-value pairs to access elements

D). They cannot be accessed using loop constructs

What type of index do indexed arrays in PHP use?

A). String

B). Integer

C). Key-value pairs

D). Floating-point number

Which PHP function is used to remove the last element from an array?

A). remove()

B). pop()

C). delete()

D). unset()

What is the output of the following PHP code?
<?php
$colors = array('Red', 'Green', 'Blue');
echo $colors[1];
?

A). Red

B). Green

C). Blue

D). 1