PHP is a widely used programming language with an array of features, and arrays are among its most fundamental and essential components. In this post, we’ll provide PHP Array Logical MCQ for Freshers with a set of logical multiple-choice questions (MCQs) focused on PHP arrays. The Array is the most important concept in PHP, and a fresher in the development field must know about this concept. Do you agree with this?
PHP Array Logical MCQ for Freshers
How do you create an empty array in PHP?
- $array = new Array();
- $array = ();
- $array = [];
- $array = array_empty();
Answer: 3. $array = [];
How do you add an element to the end of an array in PHP?
- array_add($myArray, $element);
- $myArray += $element;
- array_push($myArray, $element);
- $myArray->add($element);
Answer: 3. array_push($myArray, $element);
How do you remove the last element of an array in PHP?
- array_pop($myArray);
- unset($myArray[count($myArray) – 1]);
- array_shift($myArray);
- remove_last($myArray);
Answer: 1. array_pop($myArray);
How do you get the length of an array in PHP?
- array_length($myArray);
- count($myArray);
- lengthOf($myArray);
- sizeof($myArray);
Answer: 2. count($myArray);
How do you check if a value exists in an array in PHP?
- array_check($myArray, $value);
- in_array($value, $myArray);
- value_exists($value, $myArray);
- exists_in_array($myArray, $value);
Answer: 2. in_array($value, $myArray);
How do you access an element in an associative array in PHP?
- $myArray->get($key);
- $myArray->$key;
- $myArray[$key];
- get_element($myArray, $key);
Answer: 2. $myArray[$key];
What is an Array in PHP? | 3 Important Types of Array
How do you loop through all the elements in an array in PHP?
- for ($i = 0; $i < count($myArray); $i++) { … }
- foreach ($myArray as $item) { … }
- while ($myArray->hasNext()) { … }
- loop_array($myArray);
Answer: 2. foreach ($myArray as $item) { … }
How do you sort an array in ascending order in PHP?
- sort($myArray);
- rsort($myArray);
- asort($myArray);
- ksort($myArray);
Answer: 1. sort($myArray);
How do you sort an associative array by the value of a specific key in PHP?
- sort_assoc($myArray, ‘key’);
- krsort($myArray, ‘key’);
- asort($myArray, ‘key’);
- usort($myArray, function($a, $b) { return $a[‘key’] – $b[‘key’]; });
Answer: 4. usort($myArray, function($a, $b) { return $a[‘key’] – $b[‘key’]; });
How do you merge two arrays in PHP?
- merge($array1, $array2);
- $array1 + $array2;
- array_concat($array1, $array2);
- array_merge($array1, $array2);
Answer: 4. array_merge($array1, $array2);
What will be the output of the following PHP code snippet?
<?php
$array = array("a" => "apple", "b" => "banana", "c" => "cherry");
unset($array["b"]);
$result = array_values($array);
echo $result[1];
?>
- banana
- cherry
- apple
- Error
Answer 2. cherry
What will be the output of the following PHP code snippet?
<?php
$array = array(10, 20, 30, 40, 50);
echo array_sum($array);
?>
- 150
- 100
- 10
- Error
Answer 1: 150
Which PHP function is used to check if a specified key exists in an array?
- key_exists()
- array_key_exists()
- key_in_array()
- array_contains()
Answer 2. array_key_exists()
Which PHP function is used to extract a slice of an array?
- array_slice()
- array_splice()
- array_chunk()
- array_subset()
Answer 1. array_slice()
Which PHP function is used to retrieve all the keys of an array?
- array_keys()
- array_values()
- array_flip()
- array_extract()
Answer 1. array_keys()
What is the correct way to access the value of an element in an associative array with the key “name”?
- $array->name
- $array[name]
- $array[‘name’]
- $array->get(‘name’)
Answer 2. $array[‘name’]
Follow us on Social Media
FAQ on PHP Array Logical MCQ for Freshers
What are the 3 types of PHP arrays?
Indexed arrays – Arrays with a numeric index.
Associative arrays – Arrays with named keys.
Multidimensional arrays – Arrays containing one or more arrays.What’s the difference between sort() and asort() in PHP?
sort() is used to sort an indexed array in ascending order by its values. It rearranges the elements, modifying the original array.
asort() on the other hand, is used to sort an associative array in ascending order by its values while maintaining the key-value associations. It does not change the keys but only reorders the values.
Follow Us on Telegram
Follow Quiz Lancer Telegram Channel to solve daily interview questions. We share important interview questions which are asked in the interview. Most of the students get good values from this channel and got selected as a developer.