PYTHON Regular Expressions MCQs

  1. Which module in Python is used for regular expressions?
    a) regex
    b) re
    c) regexp
    d) reg
    View Answer

    Correct answer: B — re

  2. What will be printed?
    1 python
    2 import re
    3 print(re.match("Edu","Eduinq").group())
    a) Edu
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — Edu

  3. Which of the following matches any single character?
    a) .
    b) *
    c) +
    d) ?
    View Answer

    Correct answer: A — .

  4. What is the output of print(re.search("inq","Eduinq").group())?
    a) inq
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — inq

  5. Which of the following matches zero or more occurrences?
    a) *
    b) +
    c) ?
    d) {}
    View Answer

    Correct answer: A — *

  6. What will be printed?
    1 python
    2 import re
    3 print(re.findall("n","Eduinq"))
    a) ['n']
    b) ['n','n']
    c) Error
    d) None
    View Answer

    Correct answer: A — ['n']

  7. Which of the following matches one or more occurrences?
    a) +
    b) *
    c) ?
    d) {}
    View Answer

    Correct answer: A — +

  8. What is the output of print(re.findall("i.","Eduinq"))?
    a) ['in']
    b) ['iq']
    c) ['i']
    d) None
    View Answer

    Correct answer: A — ['in']

  9. Which of the following matches zero or one occurrence?
    a) ?
    b) *
    c) +
    d) {}
    View Answer

    Correct answer: A — ?

  10. What will be printed?
    1 python
    2 import re
    3 print(re.match("E.?u","Eduinq").group())
    a) Edu
    b) Eu
    c) Error
    d) None
    View Answer

    Correct answer: A — Edu

  11. Which of the following matches beginning of string?
    a) ^
    b) $
    c) \b
    d) \A
    View Answer

    Correct answer: A — ^

  12. What is the output of print(re.match("^E","Eduinq").group())?
    a) E
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — E

  13. Which of the following matches end of string?
    a) $
    b) ^
    c) \b
    d) \Z
    View Answer

    Correct answer: A — $

  14. What will be printed?
    1 python
    2 import re
    3 print(re.search("q$","Eduinq").group())
    a) q
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — q

  15. Which of the following matches any digit?
    a) \d
    b) \D
    c) \w
    d) \s
    View Answer

    Correct answer: A — \d

  16. What is the output of print(re.findall("\d","Eduinq123"))?
    a) ['1','2','3']
    b) ['123']
    c) Error
    d) None
    View Answer

    Correct answer: A — ['1','2','3']

  17. Which of the following matches non digit?
    a) \D
    b) \d
    c) \w
    d) \s
    View Answer

    Correct answer: A — \D

  18. What will be printed?
    1 python
    2 import re
    3 print(re.findall("\D","Eduinq123"))
    a) ['E','d','u','i','n','q']
    b) ['123']
    c) Error
    d) None
    View Answer

    Correct answer: A — ['E','d','u','i','n','q']

  19. Which of the following matches any word character?
    a) \w
    b) \W
    c) \d
    d) \s
    View Answer

    Correct answer: A — \w

  20. What is the output of print(re.findall("\w","Eduinq!"))?
    a) ['E','d','u','i','n','q']
    b) ['!']
    c) Error
    d) None
    View Answer

    Correct answer: A — ['E','d','u','i','n','q']

  21. Which of the following matches non word characters?
    a) \W
    b) \w
    c) \d
    d) \s
    View Answer

    Correct answer: A — \W

  22. What will be printed?
    1 python
    2 import re
    3 print(re.findall("\W","Eduinq!"))
    a) ['!']
    b) ['E','d','u','i','n','q']
    c) Error
    d) None
    View Answer

    Correct answer: A — ['!']

  23. Which of the following matches whitespace?
    a) \s
    b) \S
    c) \w
    d) \d
    View Answer

    Correct answer: A — \s

  24. What is the output of print(re.findall("\s","Eduinq Python"))?
    a) [' ']
    b) ['Eduinq','Python']
    c) Error
    d) None
    View Answer

    Correct answer: A — [' ']

  25. Which of the following matches non whitespace?
    a) \S
    b) \s
    c) \w
    d) \d
    View Answer

    Correct answer: A — \S

  26. What will be printed?
    1 python
    2 import re
    3 print(re.findall("\S","Eduinq Python"))
    a) ['E','d','u','i','n','q','P','y','t','h','o','n']
    b) [' ']
    c) Error
    d) None
    View Answer

    Correct answer: A — ['E','d','u','i','n','q','P','y','t','h','o','n']

  27. Which of the following matches word boundary?
    a) \b
    b) \B
    c) ^
    d) $
    View Answer

    Correct answer: A — \b

  28. What is the output of print(re.search(r"\bEdu","Eduinq Python").group())?
    a) Edu
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — Edu

  29. Which of the following matches non word boundary?
    a) \B
    b) \b
    c) ^
    d) $
    View Answer

    Correct answer: A — \B

  30. What will be printed?
    1 python
    2 import re
    3 print(re.search(r"\Binq","Eduinq").group())
    a) inq
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — inq

  31. Which of the following is used for grouping in regex?
    a) ()
    b) []
    c) {}
    d) <>
    View Answer

    Correct answer: A — ()

  32. What is the output of print(re.search("(Edu)(inq)","Eduinq").groups())?
    a) ('Edu','inq')
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — ('Edu','inq')

  33. Which of the following is used for alternation in regex?
    a) |
    b) &
    c) ^
    d) $
    View Answer

    Correct answer: A — |

  34. What will be printed?
    1 python
    2 import re
    3 print(re.search("Edu|Python","Eduinq").group())
    a) Edu
    b) Python
    c) Error
    d) None
    View Answer

    Correct answer: A — Edu

  35. Which of the following is used for character classes?
    a) []
    b) ()
    c) {}
    d) <>
    View Answer

    Correct answer: A — []

  36. What is the output of print(re.findall("[aeiou]","Eduinq"))?
    a) ['E','u','i']
    b) ['a','e','i','o','u']
    c) Error
    d) None
    View Answer

    Correct answer: A — ['E','u','i']

  37. Which of the following is used for negated character classes?
    a) [^ ]
    b) []
    c) {}
    d) <>
    View Answer

    Correct answer: A — [^ ]

  38. What will be printed?
    1 python
    2 import re
    3 print(re.findall("[^aeiou]","Eduinq"))
    a) ['d','n','q']
    b) ['E','u','i']
    c) Error
    d) None
    View Answer

    Correct answer: A — ['d','n','q']

  39. Which of the following is used for quantifiers?
    a) {m,n}
    b) []
    c) ()
    d) <>
    View Answer

    Correct answer: A — {m,n}

  40. What is the output of print(re.findall("E.{2}","Eduinq"))?
    a) ['Edu']
    b) ['Ed']
    c) Error
    d) None
    View Answer

    Correct answer: A — ['Edu']

  41. Which of the following is used for raw strings in regex?
    a) r""
    b) ""
    c) ''
    d) None
    View Answer

    Correct answer: A — r""

  42. What will be printed?
    1 python
    2 import re
    3 print(re.search(r"\d+","Eduinq123").group())
    a) 123
    b) ['1','2','3']
    c) Error
    d) None
    View Answer

    Correct answer: A — 123

  43. Which of the following is used for compiling regex?
    a) re.compile()
    b) re.regex()
    c) re.pattern()
    d) None
    View Answer

    Correct answer: A — re.compile()

  44. What is the output of below code?
    1 python
    2 import re
    3 pattern = re.compile("Edu")
    4 print(pattern.match("Eduinq").group())
    a) Edu
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — Edu

  45. Which of the following is used to substitute in regex?
    a) re.sub()
    b) re.replace()
    c) re.change()
    d) None
    View Answer

    Correct answer: A — re.sub()

  46. What will be printed?
    1 python
    2 import re
    3 print(re.sub("Edu","Python","Eduinq"))
    a) Pythoninq
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — Pythoninq

  47. Which of the following is used to split string by regex?
    a) re.split()
    b) re.divide()
    c) re.cut()
    d) None
    View Answer

    Correct answer: A — re.split()

  48. What is the output of print(re.split("\s","Eduinq Python"))?
    a) ['Eduinq','Python']
    b) ['Eduinq Python']
    c) Error
    d) None
    View Answer

    Correct answer: A — ['Eduinq','Python']

  49. Which of the following is used to escape special characters?
    a) \
    b) /
    c) []
    d) {}
    View Answer

    Correct answer: A — \

  50. What will be printed?
    1 python
    2 import re
    3 print(re.search("\.","Eduinq.com").group())
    a) .
    b) Eduinq.com
    c) Error
    d) None
    View Answer

    Correct answer: A — .

Quick Links to Explore