MCQs on Regular Expressions Basics | Perl

In Chapter 6: Regular Expressions Basics, we explore Perl’s powerful tools for pattern matching, substitution, and transliteration. These skills are essential for efficient text processing and data manipulation. Here are 30 multiple-choice questions (MCQs) divided into topics to test your knowledge on Regular Expressions in Perl.


Introduction to Regex in Perl

  1. Which operator is used to perform a regex match in Perl?
    • A) :=
    • B) !~
    • C) =~
    • D) ==
  2. What is the purpose of regular expressions in Perl?
    • A) To perform arithmetic calculations
    • B) To handle input/output operations
    • C) To match patterns in strings
    • D) To define subroutines
  3. What is the correct syntax to search for the pattern “abc” in a string using Perl?
    • A) /abc/
    • B) abc/
    • C) “abc”/
    • D) /”abc”/
  4. In Perl, what does the =~ operator indicate?
    • A) Assignment of a pattern to a string
    • B) Matching a regular expression
    • C) Replacing a substring
    • D) Comparing two strings
  5. Which of the following is the syntax to check if a string matches a regular expression in Perl?
    • A) $string =~ /pattern/;
    • B) $string =~ /pattern/;
    • C) /string/ =~ pattern;
    • D) pattern =~ $string;
  6. What does the !~ operator do in Perl regular expressions?
    • A) It performs a match
    • B) It checks if the string matches the pattern
    • C) It checks if the string does not match the pattern
    • D) It replaces a match
  7. How do you escape a special character in a regular expression in Perl?
    • A) By enclosing it in parentheses
    • B) By placing a backslash before the character
    • C) By using quotes around the character
    • D) By doubling the character
  8. Which of the following is used to match the beginning of a string in a regular expression?
    • A) ^
    • B) $
    • C) *
    • D) +
  9. What does the $ symbol represent in a regular expression in Perl?
    • A) The end of the string
    • B) A special character
    • C) The start of a string
    • D) A space character
  10. Which metacharacter is used to match any single character except a newline?
    • A) .
    • B) *
    • C) +
    • D) ?

Simple Match (=, !)

  1. What will the following code return in Perl?
    if (“hello” =~ /ell/) { print “Match\n”; }
    A) “Match”
    B) “hello”
    C) Error
    D) Nothing
  2. What is the output of the following Perl statement?
    if (“hello” !~ /world/) { print “No match\n”; }
    A) No match
    B) world
    C) Error
    D) Match
  3. Which of the following regex patterns will match the word “perl” in the string “I love perl”?
    A) /perl/
    B) perl/
    C) “perl”
    D) /”perl”/
  4. What will be the result of the following Perl expression?
    if (“apple” =~ /p/) { print “Found\n”; }
    A) Found
    B) apple
    C) Error
    D) No match
  5. What does the following code check for?
    if ($string =~ /abc/) { … }
    A) If the string contains “abc”
    B) If the string starts with “abc”
    C) If the string ends with “abc”
    D) If the string is exactly “abc”
  6. How can you check if a string does not contain the pattern “123” in Perl?
    A) $string =~ /123/;
    B) $string !~ /123/;
    C) $string eq /123/;
    D) $string == /123/;
  1. In Perl, what does the expression $str =~ /pattern/ return if the match is successful?
    • A) The string
    • B) True
    • C) False
    • D) The pattern
  2. Which of the following code snippets checks if the string $text starts with “abc”?
    • A) if ($text =~ /^abc/)
    • B) if ($text =~ /abc$/)
    • C) if ($text =~ abc^)
    • D) if ($text =~ ^abc)
  3. How do you match a pattern only if it does not occur in a string in Perl?
    • A) Use the !~ operator
    • B) Use the =~ operator
    • C) Use if
    • D) Use match
  4. What is the purpose of using the =~ operator in regular expressions?
    • A) To replace text
    • B) To compare strings
    • C) To check for a match between a string and a pattern
    • D) To delete parts of a string

Substitution and Transliteration

  1. Which operator is used for substitution in Perl regular expressions?
    • A) s///
    • B) //
    • C) m///
    • D) tr///
  2. What is the correct syntax for replacing all occurrences of “apple” with “orange” in a string?
    • A) $string =~ s/apple/orange/g;
    • B) $string =~ s/apple -> orange/g;
    • C) $string = s/apple/orange/g;
    • D) $string = s/apple -> orange/g;
  3. How can you substitute only the first occurrence of “apple” with “orange” in Perl?
    • A) $string =~ s/apple/orange/;
    • B) $string =~ s/apple/orange/g;
    • C) $string = s/apple/orange/;
    • D) $string = s/apple/orange/g;
  4. What does the g modifier do in a substitution?
    • A) It replaces only the first occurrence
    • B) It replaces all occurrences
    • C) It escapes special characters
    • D) It matches the pattern globally
  5. Which function is used for transliteration in Perl?
    • A) tr///
    • B) s///
    • C) m///
    • D) g///
  6. What is the output of the following Perl code?
    my $str = “abc”;
    $str =~ tr/a/b/;
    print $str;
    A) bbc
    B) abc
    C) ccc
    D) aac
  7. How can you use transliteration to change every “a” to “b” in a string?
    A) $string =~ tr/a/b/;
    B) $string = tr/a/b/;
    C) $string =~ s/a/b/;
    D) $string =~ m/a/b/;
  8. What will the following code do?
    my $text = “apple”;
    $text =~ s/a/A/g;
    print $text;
    A) Apple
    B) aApple
    C) ApAp
    D) apple
  1. How do you apply a regular expression with substitution to replace “cat” with “dog”?
    • A) $str =~ s/cat/dog/;
    • B) $str =~ tr/cat/dog/;
    • C) $str = s/cat/dog/;
    • D) $str = s/cat/dog/g;
  2. Which of the following is a valid regular expression for matching “hello” in a string?
    • A) /hello/
    • B) hello/
    • C) “hello”/
    • D) hello@

Answer Key

QnoAnswer
1C) =~
2C) To match patterns in strings
3A) /abc/
4B) Matching a regular expression
5A) $string =~ /pattern/;
6C) It checks if the string does not match the pattern
7B) By placing a backslash before the character
8A) ^
9A) The end of the string
10A) .
11A) “Match”
12A) No match
13A) /perl/
14A) Found
15A) If the string contains “abc”
16B) $string !~ /123/;
17B) True
18A) if ($text =~ /^abc/)
19A) Use the !~ operator
20C) To check for a match between a string and a pattern
21A) s///
22A) $string =~ s/apple/orange/g;
23A) $string =~ s/apple/orange/;
24B) It replaces all occurrences
25A) tr///
26A) bbc
27A) $string =~ tr/a/b/;
28A) Apple
29A) $str =~ s/cat/dog/;
30A) /hello/

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top