Regular Expressions with PHP Test MCQs

Regular Expressions with PHP Test MCQs

The following Regular Expressions with PHP MCQs have been compiled by our experts through research, in order to test your knowledge of the subject of Regular Expressions with PHP. We encourage you to answer these 40 multiple-choice questions to assess your proficiency.
Please continue by scrolling down.

1: Which of the following flags is used to include a newline character \n while using the dot meta character of PHP Regular Expressions?


A.   \z

B.   \m

C.   \s

D.   \a

2: Which of the following PHP Regular Expression modifiers finds one or more occurrences of a specific character in a string?


A.   ?

B.   *

C.   +

D.   #

3: Analyze the following code snippet that uses the preg_last_error() PHP Regular Expression method:

<?php
preg_match('/(?:\D+|<\d+>)*[!?]/', 'foobar foobar foobar');
if (preg_last_error() == PREG_BACKTRACK_LIMIT_ERROR) {
    echo 'Backtrack limit was exhausted!';
}
?>

What will be the output of the above code?


A.   Backtrack limit was exhausted!

B.   0

C.   1

D.   null

4: Suppose you use [hc]+at for pattern matching as a Regular Expression. Which of the following patterns does not match the above expression?


A.   at

B.   ccchat

C.   hhat

5: What is the [\b] PHP Regular Expression modifier used for?


A.   It is used as a word boundary specifying exactly what must be matched.

B.   It is used to match only a single character in a string.

C.   It is used to match a single backspace character.

D.   It is used to match all alpha numeric characters.

6: Suppose you pass "^.{3}$" as a pattern to be matched to a specified string using PHP Regular Expressions. Which of the following strings match the above pattern?


A.   .3$

B.   3$.$

C.   3_$.

D.   $3_3$

7: Analyze the following code snippet that uses the preg_replace() PHP Regular Expression method:

<?php
$string = 'This is the {_FOO_} brought to you by {_BAR_}';
$template_vars=array("FOO" => "The PHP Way", "BAR" => "PHPro.orG");
$string = preg_replace("/{_(.*?)_}/ime", "\$template_vars['$1']",$string);
echo $string;
?>

What will be the output of the above code?


A.   0

B.   This is the The FOO Way brought to you by BAR

C.   1

D.   This is the The PHP Way brought to you by PHPro.orG

8: Analyze the following code:

<?php
$string = "1, 100 or 1000?";
preg_match_all("/10*/",$string,$matches);
foreach($matches[0] as $value)
        {
        echo $value;
        }
?>

What will be the output of the above code snippet?


A.   1010

B.   110

C.   1,10

D.   1,100,1000

E.   11001000

9: Which of the following PHP Regular Expression modifiers finds zero or one occurrences of a specific character in a string?


A.   ?

B.   *

C.   +

D.   #

10: Analyze the following code:

<?php
$str="This lathe turns wood.";
echo(preg_match("/\Bthe\b/", $str));
?>

What will be the output of the above code snippet?


A.   0

B.   the

C.   null

D.   1

11: Which of the following characters is found by a Regular Expression when the \s PHP modifier is set?


A.   A newline character

B.   A null character

C.   A whitespace character

D.   A special character

12: Which of the following PHP Regular Expression modifiers is used to make as few matches as possible in a given pattern?


A.   \U

B.   \S

C.   \B

D.   \W

13: Which of the following PHP Regular Expression modifier is used to find a pattern at the beginning of a string?


A.   $

B.   ^

C.   ?=

D.   \b

14: Suppose you perform pattern matching in a string using the \n PHP Regular Expression modifier. Which of the following values is returned if no newline character occurs in the string?


A.   0

B.   null

C.   false

D.   None of the above

15: Which of the following is the correct syntax for creating a Regular Expression in PHP?


A.   preg_match("/^ABC/i", $string)

B.   preg_match("\^ABC\i", $string)

C.   preg_match("^ABC", $string)

D.   preg_match("/^ABC/i/", $string)

16: Which of the following characters is used to escape special characters in a given string during pattern matching using Regular Expressions?


A.   /

B.   \

C.   $

D.   #

17: Analyze the following code snippet that uses the preg_split() PHP Regular Expression method:

<?php
$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);
?>

What will be the output of the above code?


A.  

Array ( [1] => s [2] => t [3] => r [4] => i [5] => n [6] => g )

B.  

Array ( [0] => s )

C.  

Array ( [0] => s [1] => t [2] => r [3] => i [4] => n [5] => g )

D.   None of the above

18: Which of the following characters can be used as a delimiter in a PHP Regular Expression?


A.   ~

B.   *

C.   <

D.   @

19: Analyze the following code snippet that uses the preg_grep() PHP Regular Expression method:

<?php
$names = array('Andrew','John','Peter','Nastin','Bill');
$output = preg_grep('/^[a-m]/i', $names);
print_r( $output );
?>

What will be the output of the above code?


A.   Array ( [0] => Andrew [1] => John [4] => Bill )

B.  

Array ( [1] => Andrew [2] => John [3] => Bill )

C.  

Array ( [0] => Andrew [1] => John [3] => Bill )

D.  

Array ( [0] => Andrew )

20: Which of the following PHP Regular Expression modifiers is used to put the Regex on multiple lines while at the same time allowing comments within the Regular Expression itself?


A.   x

B.   u

C.   e

D.   s

21: Analyze the following code that uses the ?<= PHP Regular Expression modifier:

<?php
$string = 'I live in the whitehouse';
if(preg_match("/(?<!blue)house/i", $string))
        {
        echo 'Found a match';
        }
else
        {
        echo 'No match found';
        }
?>

What will be the output of the above code snippet?


A.   Found a match

B.   No match found

C.   Compilation error

22: Analyze the following code:

<?php
$string = 'This is a [templateVar]';
preg_match_all("/[\[\]]/", $string, $matches);
foreach($matches[0] as $value)
        {
        echo $value;
        }
?>

What will be the output of the above code snippet?


A.   [\[\

B.   [\]

C.   []]

D.   []

23: Analyze the following code that uses the {x} PHP Regular Expression modifier:

<?php
$string = "10 or 100 or 1000?";
preg_match_all("/\d{2}/", $string,$matches);
foreach($matches[0] as $value)
        {
        echo $value;
        }
?>

What will be the output of the above code snippet?


A.   10

B.   101010

C.   1000

D.   10101000

24: Analyze the following code snippet that uses the preg_match() PHP Regular Expression method:

<?php
$string = 'ball';
echo preg_match("/b[aoiu]l/", $string, $matches);
?>

What will be the output of the above code?


A.   1

B.   An array of all matching strings found

C.   The last match found

D.   Only the first matching element

25: Analyze the following code that uses a PHP Regular Expression Character Set:

<?php
$string = "Is this all there is?";
echo(preg_match("/[^A-J]/",$string,$matches));
?>

What will be the output of the above code snippet?


A.   1

B.   I

C.   Is

D.   s

E.   true

26: Which of the following PHP Regular Expression modifiers is used to find a pattern at the end of a string?


A.   $

B.   \z

C.   ^

D.   \s

27: Which of the following is the correct syntax for creating a Regular Expression object in PHP?


A.   $string = 'abcd';<br>echo preg_match("abc", $string);

B.   $string = "abcd";<br>echo preg_match(abc/$string);

C.   $string = 'abcd';<br>echo preg_match("/abc/", $string);

D.   $string = 'abcd';<br>echo preg_match(/abc/$string);

28: Analyze the following code that uses a PHP Regular Expression Character Set:

<?php
$string = "Welcome to the new era";
preg_match_all("/[h-b]/",$string,$matches);
foreach($matches[0] as $value)
        {
        echo $value;
        }
?>

What will be the output of the above code snippet?


A.   null

B.   1

C.   Compilation error will be displayed.

D.   h

29: Analyze the following code snippet that uses the preg_split() PHP Regular Expression method:

<?php
$keywords = preg_split("/[\s,]+/", "php, regular expressions");
print_r( $keywords );
?>

What will be the output of the above code?


A.   Array ( [0] => php [1] => regular [2] => expressions )

B.   Array ( [1] => php [2] => regular [3] => expressions )

C.   Array ( [0]- php [1] - regular [2]- expressions )

D.   Array ( [0] => php/ [1] => regular/ [2] => expressions )

30: Analyze the following code:

<?php
$string = 'The Lord is the God';
if(preg_match("/the+(?!is)/i", $string,$match))
    {
   print_r($match);
    }
else
    {
    echo 'No match found';
    }
?>

What will be the output of the above code snippet?


A.   the

B.   Array ( [0] => The )

C.   The,the

D.   The

31: Analyze the following code that uses a PHP Regular Expression Character Set:

<?php
$string = "Is this all there is?";
preg_match_all("/[a-h]/",$string,$matches);
foreach($matches[0] as $value)
        {
        echo $value;
        }
?>

What will be the output of the above code snippet?


A.   h

B.   hahee

C.   h,a,h,e,e

D.   hae

32: Analyze the following code:

<?php
$string = 'six at noon taxes';
echo preg_match("/s.x/", $string, $matches);
?>

What will be the output of the above code snippet?


A.   six

B.   1

C.   six at noon taxes

D.   null

33: Which of the following examples use the PHP positive lookahead assertion in a correct format?


A.   preg_match("/eg?=:/", $string);

B.   preg_match("/eg+(?=:)/", $string, $match)

C.   preg_match("/eg+?=:/", $string, $match)

D.   preg_match("/eg+(?=:)/", $string)

34: Analyze the following code:

<?php
$string = '%Give_result%';
preg_match_all("/[\W]/", $string, $matches);
foreach($matches[0] as $value)
        {
        echo $value;
        }
?>

What will be the output of the above code snippet?


A.   G,i,v,e,r,e,s,u,l,t

B.   %_%

C.   %%

D.   G

35: Suppose you pass "world{2,3}" as a pattern to be matched to a specified string as shown in the code snippet below:

$pattern = "world{2,3}";

Which of the following strings does not match the above pattern?


A.   worldd

B.   world

C.   worlddd

D.   None of the above

36: Suppose you pass "a(bc)*" as a pattern to be matched to a specified string using PHP Regular Expressions. Which of the following strings does not match the above pattern?


A.   aa

B.   aade

C.   abc

D.   bcde

37: Which of the following PHP Regular Expression modifiers finds any non digit character in a given string?


A.   \W

B.   \S

C.   \B

D.   \D

38: Which of the following statements are true about the \b PHP Regular Expression modifier?


A.   It is used to get a match only at the beginning of a word in a string.

B.   It is used to get a match anywhere in a string.

C.   If a match is not found using the \b modifier, it returns null.

D.   It is used to get a match at the beginning or end of a word in a string.

A.   copyright 2005

B.   Copyright 2007

C.   Copyright 2005

D.   copyright 2007

40: Analyze the following code snippet used to check the password strength:

<?php
$password = "Fyfjk34sdfjfsjq7";
if (preg_match("/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/", $password))
     {
    echo "Your passwords is strong.";
     }
else
     {
    echo "Your password is weak.";
     }
?>

What will be the output of the above code snippet?


A.   Your password is strong.

B.   Your password is weak.

C.   It will display an error as the output.

D.   null

41: Analyze the following code snippet that uses the preg_quote() PHP Regular Expression method:

<?php
$keywords = '$40 for a g3/400';
$keywords = preg_quote($keywords, '/');
echo $keywords;
?>

What will be the output of the above code?


A.   $40 for a g3/400

B.   No output

C.   \$40 for a g3\/400

D.   \$40<br>

42: Which of the following characters is not matched by a Regular Expression when a .(dot) character is set?


A.   Any special character

B.   null character

C.   white space character

D.   newline character

43: Analyze the following code:

<?php
$string = 'The GOD is the Lord.';
$string = preg_replace("/The/g", 'Ze', $string);
echo $string;
?>

What will be the output of the above code snippet?


A.   Ze GOD is the Lord.

B.   The GOD is the Lord.

C.   Ze GOD is Ze Lord.

D.   A compilation error will be displayed.<br>