MCQ

PHP Questions and Answers – Regular Expressions

This collection of PHP Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Regular Expressions”.
 

1. Both POSIX regex and PERL regex are example of ___________

A Delimitering

B Tokenizing

C Regular expressions

D None of them

C
Both POSIX regex and PERL regex are example of Regular expressions.

 

 

2. Which of the following function is not listed in PHP6?

A ereg()

B eregi()

C ereg_replace()

D All of them

D
All of them are not listed in PHP6.

 

 

3. Which of the following regular expression matches any string containing 0 or 1 P?

A p*

B p#

C p+

D P?

D
P? matches any string containing 0 or 1 P. Check this regex demo.

 

 

4. [:hello:] can also be specified as ____________

A [a-z]

B [A-z]

C [A-za-z]

D [A-Za-z0-9] .

C
[:hello:] can also be specified as [A-za-z]. Check this regex demo.

 

 

5. How many functions does PHP provide for searching strings using POSIX style regular expression?

A 5

B 6

C 7

D 8

C
split(), ereg_replace(), ereg(), eregi(), eregi_replace(), sql_regcase(), and spliti() are the functions provided.

 

 

6. What is the result of the following PHP code?
<?php
    $username = "Alex";
    if (preg_match("([^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

B
The built-in function preg_match() returns whether a match was found in a string. Check this regex demo.

 

 

7. POSIX stands for _________________?

A Portative Operating System Interface for Linux

B Portable Operating System Interface for Unix

C Portable Operating System Interface for Linux

D Portative Operating System Interface for Unix

B
POSIX stands for Portable Operating System Interface for Unix.

 

 

8. POSIX style regular expressions was deprecated in which version of PHP?

A PHP 4

B PHP 5

C PHP 5.2

D PHP 5.3

D
POSIX style regular expressions was deprecated in PHP 5.3.

 

 

9. What is the result of the following PHP code?
<?php
    $names = array("alex", "jean", "emily", "jane");
    $name = preg_grep("/^e/", $names);
    print_r($name);
?>

A Array ( [0] => alex [1] => jean [2] => emily [3] => jane )

B Array ( [0] => alex )

C Array ( [2] => emily )

D Array ( [3] => jane )

C
The built-in function preg_grep() returns an array containing only items from the input that match the given pattern.

 

 

10. What is the result of the following PHP code?
<?php
    $mail = "[email protected]";
    $mail = str_replace("a","@",$mail);
    echo "Contact me at $mail.";
?>

A Contact me at @dmin@[email protected].

B Contact me at [email protected].

C Contact me at adminaexample.com.

D Contact me at $mail.

A
The built-in function The str_replace() replaces some characters with other characters in a string.

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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