This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Strings and Regular Expressions”.
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)
View Answer
Explanation: None.
2. Which one of the following regular expression matches any string containing zero or one p?
a) p+
b) p*
c) P?
d) p#
View Answer
Explanation: None.
3. [:alpha:] can also be specified as ________
a) [A-Za-z0-9]
b) [A-za-z]
c) [A-z]
d) [a-z]
View Answer
Explanation:[:alpha:] is nothing but Lowercase and uppercase alphabetical characters.
4. How many functions does PHP offer for searching strings using POSIX style regular expression?
a) 7
b) 8
c) 9
d) 10
View Answer
Explanation: ereg(), ereg_replace(), eregi(), eregi_replace(), split(), spliti(), and sql_regcase() are the functions offered.
5. 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) Error
b) Username must be all lowercase!
c) Username is all lowercase!
d) No Output is returned
View Answer
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.
6. POSIX implementation was deprecated in which version of PHP?
a) PHP 4
b) PHP 5
c) PHP 5.2
d) PHP 5.3
View Answer
Explanation: None.
7. 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
View Answer
Explanation: None.
8. 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) Array ( [0] => some text that [1] => we might like to parse. )
c) Array ( [0] => this is [1] => some text that [2] => we might like to parse. )
d) [0] => this is [1] => some text that [2] => we might like to parse.
View Answer
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.
9. 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)
View Answer
Explanation: This matches f followed by two to four occurrences of o.
10. 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)
View Answer
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.
More MCQs on PHP Strings and Regular Expressions:
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.
- Check MCA Books
- Check PHP Books
- Practice MCA MCQs
- Practice Programming MCQs
- Apply for Programming Internship