Reading Time: 4 minutes

Java Regular Expressions Quiz

Java Regular Expressions Quiz Regex Questions

We have brought some Java Regular Expressions Quiz Regex Questions to test your Regex Knowledge. If you need to brush up your Java Regex skill, check out the below tutorials.

All About Java Regular Expressions Regex 2019

 

Java Regular Expression Questions

1. Which of the following statements about the regex API are true?

A. Instances of the Pattern class are used to match character sequences against a given pattern
B. The package java.util.regex includes an exception called PatternSyntaxException
C. Instances of Matcher class are used to represent regular expressions in the form of String type
D. None of the above

B

2. What is the significance of Matcher class for a regular expression in java?

A) interprets pattern in the string
B) Performs match in the string
C) interpreted both patterns and performs match operations in the string
D) None of the mentioned.

C

3. An object of which class is used to compile regular expression?

A) Pattern class
B) Matcher class
C) PatternSyntaxException
D) None of the mentioned

A

4. What is the significance of Matcher class for a regular expression in java?

A. interprets pattern in the string
B. Performs match in the string
C. interpreted both patterns and performs match operations in the string
D. None of the mentioned

C

5. What does public int end(int group) return?

A. offset from the last character of the subsequent group
B. offset from the first character of the subsequent group
C. offset from the last character matched
D. offset from the first character matched

A

6. groupCount() reports a total number of Capturing groups.

A) True
B) False

A

7. Which of the following matches nonword character using regular expression in java?

A) \w
B) \W
C) \s
D) \S

B

8. Which capturing group can represent the entire expression?

A) group *
B) group 0
C) group * or group 0
D) None of the mentioned

B

9. Which of the following matches the end of the string using regular expression in java?

A) \z
B) \\
C) \*
D) \Z

A

10. Which of the following is not a class of java.util.regex?

A) Pattern class
B) matcher class
C) PatternSyntaxException
D) Regex class

D

 

Java Regular Expressions Quiz Continues:

11. What does public int end(int group) return?

A) offset from the last character of the subsequent group
B) offset from the first character of the subsequent group
C) offset from the last character matched
D) offset from the first character matched

A

12. what does public String replaceAll(string replace) do?

A) Replace all characters that match a pattern with a replacement string
B) Replace the first subsequence that matches a pattern with a replacement string
C) Replace all other than the first subsequence of that matches a pattern with a replacement string
D) Replace every subsequence of the input sequence that matches a pattern with a replacement string

D

13. What does public int start() return?

A) returns start index of the input string
B) returns start index of the current match
C) returns start index of the previous match
D) None of the above

C

14. Which capturing group can represent the entire expression?

A. group *
B. group 0
C. group * or group 0
D. None of the mentioned

B

 

15. What is java regex for below string S?

S must be of length: 6
First character: 1, 2 or 3
Second character: 1, 2 or 0
Third character: x, s or 0
Fourth character: 3, 0, A or a
Fifth character: x, s or u
Sixth character: . or,

A) ^([1-3][0-2][xs0][30Aa][xsu][.,])$
B) ([1-3][0-2][xs0][30Aa][xsu][.,])
C) ([1-3][0-2][xs0][30Aa][xsu][.,]){6}
D) ^([1-3][0-2][x,s,0][3,0,A,a][x,s,u][\.\,])$

A

 

16. What is the Regex for below String?

The string should consist of only lowercase and uppercase letters (no numbers or symbols).
The string should end in s.

A) ^([(?i)a-z]*S)
B) ^((?i)[A-Z]*s)
C) ^([a-zA-Z]*s)$
D) ([a-z]*s)$

A

 

 

17. What is Java Regex for below String?

The string must start with Mr., Mrs., Ms., Dr. or Er.
The rest of the string must contain only one or more English alphabetic letters (upper and lowercase).

A) (Mr,Mrs,Ms,Dr,Er).([a-zA-Z]?)$
B) ^(Mr|Mrs|Ms|Dr|Er)\\.([a-zA-Z]+)$
C) ^(Mr|Mrs|Ms|Dr|Er)\\.([a-zA-Z])$
D) (Mr|Mrs|Ms|Dr|Er)\.([a-zA-Z]+)$

B

 

18. Is String.matches() is same as Pattern.matches()?

A) true
B) false

A

 

19. Pattern.matches() should only be used when entire input should completely match with Regex.

A) true
B) false

A

 

20. Is PatternSyntaxException a checked Exception?

A) true
B) false

B

 

 

We will soon add some more questions to the list of Java Regular Expressions Quiz Regex questions, If you see any error in these questions, please let us know here.

If you are stuck with any of regex, Check it here.

CheckOut our GitHub Page.