PHP Multiple Choice Questions – Strings and Regular Expressions

PHP Multiple Choice Questions & Answers (MCQs) focuses on “Strings and Regular Expressions”.

Q 1. PHP has long supported two regular expression implementations known as _______ and _______

i) Perl
ii) PEAR
iii) Pearl
iv) POSIX
A. i) and ii)
B. ii) and iv)
C. i) and iv)
D. ii) and iii)

Show Answer Answer:-C. i) and iv)


Q 2. What will be the output of the following PHP code?

<?php
$text = “this is\tsome text that\nwe might like to parse.”;
print_r(split(“[\n\t]”,$text));
?>
A. this is some text that we might like to parse.
B. [0] => this is [1] => some text that [2] => we might like to parse.
C. Array ( [0] => this is [1] => some text that [2] => we might like to parse. )
D. Array ( [0] => some text that [1] => we might like to parse. )

Show Answer Answer:-B. [0] => this is [1] => some text that [2] => we might like to parse.
Explanation The split() function divides a string into various elements, with the boundaries of each element based on the occurrence of a defined pattern within the string.


Q 3. Which of the following would be a potential match for the Perl-based regular expression /fo{2,4}/?

i) fol
ii) fool
iii) fooool
iv) fooooool
A. Only i)
B. ii) and iii)
C. i), iii) and iv)
D. i) and iv)

Show Answer Answer:-B. ii) and iii)
Explanation This matches f followed by two to four occurrences of o.


Q 4. Which among the following is/are not a metacharacter?

i) \a
ii) \A
iii) \b
iv) \B
A. Only i)
B. i) and iii)
C. ii), iii) and iv)
D. ii) and iv)

Show Answer Answer:-A. Only i)
Explanation /A, /b and /B are metacharacters. \A: Matches only at the beginning of the string. \b: Matches a word boundary. \B: Matches anything but a word boundary.


Q 5 . Which one of the following regular expression matches any string containing zero or one p?
A. P?
B. p*
C. p+
D. p#

Show Answer Answer:-A. P?


Q 6. [:alpha:] can also be specified as ________
A. [A-Za-z0-9]
B. [a-z]
C. [A-z]
D. [A-za-z]

Show Answer Answer:-D. [A-za-z]
Explanation [:alpha:] is nothing but Lowercase and uppercase alphabetical characters.


Q 7. How many functions does PHP offer for searching strings using POSIX style regular expression?
A. 7
B. 8
C. 9
D. 10

Show Answer Answer:-A. 7
Explanation ereg(), ereg_replace(), eregi(), eregi_replace(), split(), spliti(), and sql_regcase() are the functions offered.

Q 8. What will be the output of the following PHP code?

<?php
$username = “jasoN”;
if (ereg(“([^a-z])”,$username))
echo “Username must be all lowercase!”;
else
echo “Username is all lowercase!”;
?>
A. Username must be all lowercase!
B. Username is all lowercase!
C. No Output is returned
D. Error

Show Answer Answer:-A. Username must be all lowercase!
Explanation Because the provided username is not all lowercase, ereg() will not return FALSE (instead returning the length of the matched string, which PHP will treat as TRUE), causing the message to output.


Q 9. POSIX implementation was deprecated in which version of PHP?
A. PHP 4
B. PHP 5
C. PHP 5.2
D. PHP 5.3

Show Answer Answer:-D. PHP 5.3


Q 10. POSIX stands for ____________
A. Portable Operating System Interface for Unix
B. Portable Operating System Interface for Linux
C. Portative Operating System Interface for Unix
D. Portative Operating System Interface for Linux

Show Answer Answer:-A. Portable Operating System Interface for Unix

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page