Sample questions
Question 1Medium
What will be the output of the following PHP code?
< ?php
echo ord ("hi");
? >
- 106
- 103
- 104
- 209
Answer: C. 104
Explanation
The ord() function returns the ASCII value of the first character of a string. The ASCII value of h is 104, thus 104 was displayed.
Step-by-step solution
- The
ord() function returns the ASCII value of the first character in a string. - The first character in the string 'hi' is 'h'.
h has an ASCII value of 104.- So, the output will be 104.
- Final answer: 104.
Why the other options are wrong
- A. 106 is the code for j, not for the first letter h of the string hi.
- B. 103 is the code for g, one less than the code for h.
- D. 209 is not the code for h; ord() returns 104 for the first character h.
Tip: Remember ASCII values for common characters to quickly solve similar questions.
Question 2Medium
Can I have new lines in $subject tag of php's mail function?
- Yes by specifying /n
- No
Answer: B. No
Explanation
subject Specifies the subject of the email. Note: This parameter cannot contain any newline characters
Step-by-step solution
- The question asks whether the subject of PHP's mail function can contain new lines.
- The PHP manual says the subject parameter cannot contain any newline characters.
- So the answer is No, and the correct answer is option B.
Why the other options are wrong
- A. The subject cannot contain newlines, and /n is not a newline escape in any case.
Tip: Mail headers such as the subject must stay on one line.
Question 3Medium
How do I find out the number of parameters passed into function?
- By using
func_num_args() - By using
func_get_arg() - By using
func_get_args() - None of the above
Answer: A. By using func_num_args()
Explanation
func_num_args Returns the number of arguments passed to the function.
e.g. int func_num_args ( void )
Step-by-step solution
- func_num_args() returns the count of arguments passed to a function.
- It is the correct built-in function for this purpose in PHP.
- Other options like func_get_arg() or func_get_args() do not return the count.
- Therefore, the right answer is func_num_args().
- Final answer: By using func_num_args()
Why the other options are wrong
- B. func_get_arg() returns one argument by its index, not the number of arguments.
- C. func_get_args() returns all arguments as an array, not the count of them.
- D. None is wrong because func_num_args() returns the number of parameters passed.
Tip: Remember: func_num_args() gives the count, while func_get_args() returns all arguments as an array.
Question 4Medium
What is the strpos() function used for?
- Find the last character of a string
- Replace part of a string
- Search for character within a string
- Convert a string to lower case
Answer: C. Search for character within a string
Explanation
The strpos() function finds the position of the first occurrence of a string inside another string.
Note: The strpos() function is case-sensitive.
Step-by-step solution
- The name strops is a typo for strpos, which the explanation describes.
- strpos() finds the position of the first occurrence of a string inside another string.
- Searching for a character inside a string is what it does, so option C is the answer.
Why the other options are wrong
- A. Strpos does not find the last character; it finds the first match.
- B. Option B points to options b and c, and option C alone describes the function.
- D. Strpos returns where a match starts, not the position of a string's first character.
Tip: Strpos returns the position of the first match and returns false when nothing is found.
Question 5Medium
What is the use of friend function?
- friend functions can use all attributes of the class which names them as a friend, as if they were themselves members of that class.
- friend declared items can be accessed everywhere.
- friend limits visibility only to the class that defines the item.
- friend function does not lose its value when program execution leaves this scope.
Answer: A. friend functions can use all attributes of the class which names them as a friend, as if they were themselves members of that class.
Explanation
Sometimes a function is best shared among a number of different classes. Such functions can be declared either as member functions of one class or as global functions. In either case they can be set to be friends of other classes, by using a friend specifier in the class that is admitting them. Such functions can use all attributes of the class which names them as a friend, as if they were themselves members of that class.
Step-by-step solution
- Friend function can access private and protected members of a class.
- It behaves like a member function of that class.
- Friend function is declared inside the class definition.
- Friend function can be used by other classes as well.
- Friend function does not have access to the class's members unless explicitly allowed.
- Final answer: friend functions can use all attributes of the class which names them as a friend, as if they were themselves members of that class.
Why the other options are wrong
- B. Friend declared items are not accessible everywhere; only the named friend class or function gets access.
- C. Friend does not limit visibility to the defining class; it extends access to the named friends.
- D. That describes static or persistent local variables, not what a friend function does.
Tip: Friend functions are useful for sharing functionality across multiple classes without making them members.
Question 6Medium
Which one of the following functions can be used to compress a string?
- zip_compress()
- zip()
- compress()
- gzcompress()
Answer: D. gzcompress()
Explanation
We will be able to achieve almost 50% size reduction using this function. The gzuncompress() function is used to uncompress the string.
Step-by-step solution
gzcompress() is a built-in PHP function for compressing strings.- It uses GZIP compression algorithm which reduces file size.
- Other options like
zip() or compress() are not standard functions for this purpose. - The correct function to compress a string is
gzcompress(). - Final answer: D (gzcompress()).
Why the other options are wrong
- A. zip_compress() is not a standard PHP function for compressing a string.
- B. zip() is not a standard PHP function for compressing a string.
- C. compress() is not a standard PHP function; gzcompress() compresses strings.
Tip: Remember, gzcompress() is the standard function for string compression in PHP.
Question 7Medium
what happens if no path is given in include() function?
- PHP continues to execute the script.
- Results in a fatal error
- Include_path is made use of
- It haults the script.
Answer: C. Include_path is made use of
Explanation
Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing. The include construct will emit a warning if it cannot find a file; this is different behavior from require, which will emit a fatal error.
Step-by-step solution
- include() checks the given path first.
- If no path is given, it uses the include_path.
- If file not found in include_path, it checks other directories.
- It emits a warning but does not halt the script.
- Final answer: Include_path is made use of.
Why the other options are wrong
- A. The script continues only after a warning when the file is not found; with no path, include_path is used first.
- B. No fatal error happens just because the path is missing; include() uses include_path instead.
- D. The script is not halted because of a missing path; include() only warns when the file is not found.
Tip: include() uses include_path when no path is given; it doesn't stop the script.
Question 8Medium
Which of the following PHP functions can be used for generating unique id's?
uniqid()id()md5()mdid()
Answer: A. uniqid()
Explanation
Many people use the md5() function for this, even though its not exactly meant for this purpose. uniqid() is the function that is to be used.
Step-by-step solution
- The question asks which PHP function generates unique IDs.
- uniqid() returns a unique ID based on the current time in microseconds, so option A is the answer.
- md5() makes a hash of data and is not meant for unique IDs, and id() and mdid() are not PHP functions.
Why the other options are wrong
- B. id() is not a PHP function for generating unique IDs.
- C. md5() makes a hash of data and is not meant for unique IDs.
- D. mdid() is not a PHP function.
Tip: uniqid() is the PHP function to use when you need a unique string.
Question 9Medium
____ function in PHP returns a list of response headers sent(or ready to send)
header()headers_list()header_sent()header_send()
Answer: B. headers_list()
Explanation
headers_list Returns a list of response headers sent (or ready to send).
e.g. array headers_list ( void )
Step-by-step solution
- The question asks which function returns the list of response headers sent or ready to send.
- The explanation says headers_list returns that list.
- So option B, headers_list(), is the answer.
Why the other options are wrong
- A. header() sends one header; it does not return a list.
- C. Header_sent is not the name of a list function.
- D. Header_send is not a PHP function.
Tip: Functions starting with headers_ report on headers; header() sets one.
Question 10Medium
Which one of the following PHP functions can be used to find files?
- glob()
- file()
- fold()
- get_file()
Answer: A. glob()
Explanation
Here is an example-
// get all php files AND txt files
$files = glob('*.{php,txt}', GLOB_BRACE);
print_r($files);
/* output looks like:
Array (
[0] => phptest.php
[1] => pi.php
[2] => post_output.php
.
)
Step-by-step solution
- glob() function is used to find files matching a pattern.
- It can search for multiple file types using wildcards like {php,txt}.
- file() is used to read entire contents of a file into an array.
- fold() is not a valid PHP function.
- get_file() is not a valid PHP function.
- Correct answer is glob().
Why the other options are wrong
- B. file() reads a file's lines into an array; it does not search for files.
- C. fold() is not a PHP function for finding files.
- D. get_file() is not a PHP function, so it cannot find files.
Tip: Use glob() to find files matching specific patterns in PHP.
Practise all 10 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →