Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Which of the functions is used to sort an array in descending order?(a) sort()(b) asort()(c) rsort()(d) dsort()The question was asked in an online quiz.I want to ask this question from Arrays in section Arrays and Functions of PHP

Answer»

The CORRECT answer is (C) rsort()

To explain: The function sort() will sort thearrays in ascending order, the function rsort() will sort ARRAYS in DESCENDING order. While the function asort() will sort associative arrays in ascending order, ACCORDING to the value.

2.

Which function should we use to sort the array in natural order?(a) dsort()(b) casesort()(c) natcasesort()(d) naturalsort()This question was posed to me in exam.Question is taken from Arrays topic in portion Arrays and Functions of PHP

Answer»

The correct option is (C) natcasesort()

The explanation is: The FUNCTION natcasesort() in PHP SORTS an array by using a “natural order” algorithm. All the values keep their original keys. Eg: In a natural algorithm, as the number 2 is less than the number 10. But in computer sorting, 10 is less than 2, because the first number in “10” is less than 2. The function is case-insensitive.

3.

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()I had been asked this question in unit test.This question is from Arrays-1 in section Arrays and Functions of PHP

Answer» CORRECT answer is (C) PREV()

To explain: The prev() FUNCTION returns the previous element in the ARRAY.
4.

Which function returns an array consisting of associative key/value pairs?(a) count()(b) array_count()(c) array_count_values()(d) count_values()I had been asked this question in examination.Origin of the question is Arrays-1 in chapter Arrays and Functions of PHP

Answer»

Correct CHOICE is (c) array_count_values()

BEST 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.

5.

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()The question was posed to me at a job interview.This intriguing question comes from Arrays-1 in section Arrays and Functions of PHP

Answer»

Right answer is (b) is_array()

To elaborate: 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).

6.

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()The question was asked in semester exam.This intriguing question comes from Arrays-1 topic in section Arrays and Functions of PHP

Answer»

Correct CHOICE is (d) array_push()

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.

7.

PHP’s numerically indexed array begin with position ___________(a) 1(b) 2(c) 0(d) -1I had been asked this question in exam.This interesting question is from Arrays-1 in portion Arrays and Functions of PHP

Answer»

The correct CHOICE is (c) 0

The BEST explanation: Like all the other programming languages, the FIRST element of an array always STARTS with ‘0’.

8.

Which of the following PHP functions can be used to get the current memory usage?(a) get_usage()(b) get_peak_usage()(c) memory_get_usage()(d) memory_get_peak_usage()The question was posed to me in an interview.This intriguing question originated from In-Built Functions in PHP topic in section Arrays and Functions of PHP

Answer»

Correct answer is (c) memory_get_usage()

For explanation I WOULD say: memory_get_usage() returns the amount of MEMORY, in bytes, that’s currently being allocated to the PHP script. We can set the parameter ‘real_usage’ to TRUE to get TOTAL memory allocated from system, including unused pages. If it is not set or FALSE then only the used memory is reported. To get the HIGHEST amount of memory used at any point, we can use the memory_get_peak_usage() function.

9.

Which one of the following PHP functions can be used to find files?(a) glob()(b) file()(c) fold()(d) get_file()The question was asked in an international level competition.My doubt is from In-Built Functions in PHP topic in division Arrays and Functions of PHP

Answer»

Correct answer is (a) glob()

Explanation: The FUNCTION glob() RETURNS an ARRAY of filenames or directories which matches a specified pattern. The function returns an array of files/directories, or it will RETURN FALSE on failure. Here is an example-

10.

Which of the following PHP functions accepts any number of parameters?(a) func_get_argv()(b) func_get_args()(c) get_argv()(d) get_argc()This question was addressed to me in an interview for internship.This key question is from In-Built Functions in PHP topic in portion Arrays and Functions of PHP

Answer»

The correct ANSWER is (b) func_get_args()

Best explanation: func_get_args() returns an array of arguments PROVIDED. One can USE func_get_args() inside the function to parse any NUMBER of passed parameters. Here is an example:

11.

A function in PHP which starts with __ (double underscore) is known as __________(a) Magic Function(b) Inbuilt Function(c) Default Function(d) User Defined FunctionI have been asked this question by my school teacher while I was bunking the class.I'm obligated to ask this question of Functions in portion Arrays and Functions of PHP

Answer» RIGHT OPTION is (a) Magic Function

The best explanation: PHP functions that START with a double underscore – a “__” – are called magic functions in PHP. They are functions that are always defined inside classes, and are not stand-alone functions.
12.

How to define a function in PHP?(a) function {function body}(b) data type functionName(parameters) {function body}(c) functionName(parameters) {function body}(d) function functionName(parameters) {function body}This question was posed to me in class test.The above asked question is from Functions topic in chapter Arrays and Functions of PHP

Answer» CORRECT answer is (d) function functionName(parameters) {function body}

EASIEST explanation: PHP ALLOWS us to create our own user-defined functions. Any name ending with an open and closed PARENTHESIS is a function. The keyword function is always USED to begin a function.
13.

Type Hinting was introduced in which version of PHP?(a) PHP 4(b) PHP 5(c) PHP 5.3(d) PHP 6I had been asked this question in final exam.This intriguing question comes from Functions in chapter Arrays and Functions of PHP

Answer»

Correct OPTION is (b) PHP 5

The best I can explain: PHP 5 introduced the feature of type hinting. With the HELP of type hinting, we can specify the EXPECTED data type of an argument in a function DECLARATION. First valid types can be the class names for arguments that receive objects and the other are array for those that receive arrays.