This set of PHP Multiple Choice Questions & Answers [MCQs] focuses on “Arrays”. 1. PHP’s numerically indexed array begin with position ___________ Answer: c 2. Which of the following are correct ways of creating an array?
a] 1
b] 2
c] 0
d] -1
View Answer
Explanation: Like all the other programming languages, the first
element of an array always starts with ‘0’.i] state[0] = "karnataka";
ii] $state[] = array["karnataka"];
iii] $state[0] = "karnataka";
iv] $state = array["karnataka"];
a] iii] and iv]
b] ii] and iii]
c] Only i]
d] ii], iii] and iv]
View Answer
Answer: a
Explanation: A variable name should start with $ symbol which is not present in i] and you need not put the square brackets when you use the array[] constructor.
3. What will be the output of the following PHP code?
a] Karnataka 11,35,000
b] 11,35,000
c] population 11,35,000
d] Karnataka population
View Answer
Answer: b
Explanation: In the following PHP code, the variable states are treated as a multidimensional array and accordingly
traverse it to get the value of ‘Karnataka’s population’.
4. Which of the following PHP function will return true if a variable is an array or false if it is not an array?
a] this_array[]
b] is_array[]
c] do_array[]
d] in_array[]
View Answer
Answer: b
Explanation: The function is_array[] is an inbuilt function in PHP which is used to check whether a variable is an array or not. Its prototype
follows: boolean is_array[mixed variable].
5. Which in-built function will add a value to the end of an array?
a] array_unshift[]
b] into_array[]
c] inend_array[]
d] array_push[]
View Answer
Answer: d
Explanation: array_push adds a value to the end of an array, returning the total count of elements in the array after the new value has been added.
6. What will be the output of the following PHP code?
a] True
b] 1
c] False
d] 2
View Answer
Answer: d
Explanation: The array_search[] function searches an array for a specified value, returning its key if located and FALSE otherwise.
7. What will be the output of the following PHP code?
a] orangebanana
b] appleorange
c] orangeorange
d] appleapple
View Answer
Answer: a
Explanation: The next[] function returns the value of the next element in the array. In the first ‘next[$fruits]’ call, it will print orange which is next to apple and so on.
8. Which of the following function is used to get the value of the
previous element in an array?
a] last[]
b] before[]
c] prev[]
d] previous[]
View Answer
Answer: c
Explanation: The prev[] function returns the previous element in the array.
9. What will be the output of the following PHP code?
a] 3
b] 4
c] 5
d] 6
View Answer
Answer: d
Explanation: The function count[] will return the number of elements in an array. The parameter 1 counts the array recursively i.e it will count all the elements of multidimensional arrays.
10. Which function returns an array consisting of associative key/value pairs?
a] count[]
b] array_count[]
c] array_count_values[]
d] count_values[]
View Answer
Answer: c
Explanation: The function array_count_values[] will count all the values of an array. It will return an associative array, where the keys will be the original array’s values, and the values are the number of occurrences.
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all questions on PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
Next Steps:
- Get Free Certificate of Merit in PHP Programming
- Participate in PHP Programming Certification Contest
- Become a Top Ranker in PHP Programming
- Take PHP Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.
Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.