MCQs > IT & Programming > Regular Expressions With PHP Test MCQs > Basic Regular Expressions with PHP Test MCQs

Basic Regular Expressions with PHP Test MCQ

1. 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?


Answer

Correct Answer: A compilation error will be displayed.<br>

Note: This Question is unanswered, help us to find answer for this one

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


Answer

Correct Answer: newline character

Note: This Question is unanswered, help us to find answer for this one

3. 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?


Answer

Correct Answer: \$40 for a g3\/400

Note: This Question is unanswered, help us to find answer for this one

4. 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?


Answer

Correct Answer: Your password is strong.

Note: This Question is unanswered, help us to find answer for this one

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

<?php
echo preg_replace("/([Cc]opyright) 200(3|4|5|6)/", "$1 2007", "Copyright 2005");
?>

What will be the output of the above code snippet?


Answer

Correct Answer: Copyright 2007

Note: This Question is unanswered, help us to find answer for this one

6. Which of the following statements are true about the \b PHP Regular Expression modifier?


Answer

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

Note: This Question is unanswered, help us to find answer for this one

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


Answer

Correct Answer: \D

Note: This Question is unanswered, help us to find answer for this one

8. 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?


Answer

Correct Answer: bcde

Note: This Question is unanswered, help us to find answer for this one

9. 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?


Answer

Correct Answer: world

Note: This Question is unanswered, help us to find answer for this one

10. 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?


Answer

Correct Answer: %%

Note: This Question is unanswered, help us to find answer for this one

11. Which of the following examples use the PHP positive lookahead assertion in a correct format?


Answer

Correct Answer: preg_match("/eg+(?=:)/", $string, $match)

Note: This Question is unanswered, help us to find answer for this one

12. 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?


Answer

Correct Answer: 1

Note: This Question is unanswered, help us to find answer for this one

13. 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?


Answer

Correct Answer: hahee

Note: This Question is unanswered, help us to find answer for this one

14. 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?


Answer

Correct Answer: Array ( [0] =&gt; The )

Note: This Question is unanswered, help us to find answer for this one

15. 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?


Answer

Correct Answer: Array ( [0] =&gt; php [1] =&gt; regular [2] =&gt; expressions )

Note: This Question is unanswered, help us to find answer for this one

16. 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?


Answer

Correct Answer: Compilation error will be displayed.

Note: This Question is unanswered, help us to find answer for this one

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


Answer

Correct Answer: $string = 'abcd';<br>echo preg_match("/abc/", $string);

Note: This Question is unanswered, help us to find answer for this one

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


Answer

Correct Answer: \z

Note: This Question is unanswered, help us to find answer for this one

19. 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?


Answer

Correct Answer: 1

Note: This Question is unanswered, help us to find answer for this one

20. 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?


Answer

Correct Answer: 1

Note: This Question is unanswered, help us to find answer for this one

21. 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?


Answer

Correct Answer: 10101000

Note: This Question is unanswered, help us to find answer for this one

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?


Answer

Correct Answer: []

Note: This Question is unanswered, help us to find answer for this one

23. 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?


Answer

Correct Answer: Found a match

Note: This Question is unanswered, help us to find answer for this one

24. 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?


Answer

Correct Answer: x

Note: This Question is unanswered, help us to find answer for this one

25. 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?


Answer

Correct Answer: Array ( [0] => Andrew [1] => John [4] => Bill )

Note: This Question is unanswered, help us to find answer for this one

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


Answer

Correct Answer: ~

Note: This Question is unanswered, help us to find answer for this one

27. 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?


Answer

Correct Answer:

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


Note: This Question is unanswered, help us to find answer for this one

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


Answer

Correct Answer: \

Note: This Question is unanswered, help us to find answer for this one

29. Which of the following is the correct syntax for creating a Regular Expression in PHP?


Answer

Correct Answer: preg_match("/^ABC/i", $string)

Note: This Question is unanswered, help us to find answer for this one

30. 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?


Answer

Correct Answer: 0

Note: This Question is unanswered, help us to find answer for this one

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


Answer

Correct Answer: ^

Note: This Question is unanswered, help us to find answer for this one

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


Answer

Correct Answer: \U

Note: This Question is unanswered, help us to find answer for this one

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


Answer

Correct Answer: A newline character

Note: This Question is unanswered, help us to find answer for this one

34. 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?


Answer

Correct Answer: 1

Note: This Question is unanswered, help us to find answer for this one

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


Answer

Correct Answer: ?

Note: This Question is unanswered, help us to find answer for this one

36. 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?


Answer

Correct Answer: 11001000

Note: This Question is unanswered, help us to find answer for this one

37. 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?


Answer

Correct Answer: This is the The PHP Way brought to you by PHPro.orG

Note: This Question is unanswered, help us to find answer for this one

38. 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?


Answer

Correct Answer: .3$

Note: This Question is unanswered, help us to find answer for this one

39. What is the [\b] PHP Regular Expression modifier used for?


Answer

Correct Answer: It is used to match a single backspace character.

Note: This Question is unanswered, help us to find answer for this one

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


Answer

Correct Answer: at

Note: This Question is unanswered, help us to find answer for this one

41. 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?


Answer

Correct Answer: Backtrack limit was exhausted!

Note: This Question is unanswered, help us to find answer for this one

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


Answer

Correct Answer: +

Note: This Question is unanswered, help us to find answer for this one

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


Answer

Correct Answer: \s

Note: This Question is unanswered, help us to find answer for this one