PHP Questions and Answers Part-16

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)

Answer: c
Explanation: i) and iv)

2. Which one of the following regular expression matches any string containing zero or one p?
a) p+
b) p*
c) P?
d) p#

Answer: c
Explanation: P?

3. [:alpha:] can also be specified as ________
a) [A-Za-z0-9]
b) [A-za-z]
c) [A-z]
d) [a-z]

Answer: b
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

Answer: a
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

Answer: b
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

Answer: d
Explanation: PHP 5.3

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

Answer: a
Explanation: Portable Operating System Interface for Unix

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.

Answer: d
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)

Answer: b
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)

Answer: a
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.