250+ TOP MCQs on Regular Expression and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “Regular Expression”.

1. Which of the following is not a class of java.util.regex?
a) Pattern class
b) matcher class
c) PatternSyntaxException
d) Regex class

Answer: d
Clarification: java.util.regex consists 3 classes. PatternSyntaxException indicates syntax error in regex.

2. What is the significance of Matcher class for regular expression in java?
a) interpretes pattern in the string
b) Performs match in the string
c) interpreted both pattern and performs match operations in the string
d) None of the mentioned.

Answer: c
Clarification: macther() method is invoked using matcher object which interpretes pattern and performs match operations in the input string.

3. Object of which class is used to compile regular expression?
a) Pattern class
b) Matcher class
c) PatternSyntaxException
d) None of the mentioned

Answer: a
Clarification: object of Pattern class can represent compiled regular expression.

4. Which capturing group can represent the entire expression?
a) group *
b) group 0
c) group * or group 0
d) None of the mentioned

Answer: b
Clarification: Group 0 is a special group which represents the entire expression.

5. groupCount reports a total number of Capturing groups.
a) True
b) False

Answer: a
Clarification: groupCount reports total number of Capturing groups. this does not include special group, group 0.

6. Which of the following matches nonword character using regular expression in java?
a) w
b) W
c) s
d) S

Answer: b
Clarification: W matches nonword characters. [0-9], [A-Z] and _ (underscore) are word characters. All other than these characters are nonword characters.

7. Which of the following matches end of the string using regular expression in java?
a) z
b) \
c) *
d) Z

Answer: a
Clarification: z is used to match end of the entire string in regular expression in java.

8. What does public int end(int group) return?
a) offset from last character of the subsequent group
b) offset from first character of the subsequent group
c) offset from last character matched
d) offset from first character matched

Answer: a
Clarification: public int end(int group) returns offset from the last character of the subsequent group.

9. what does public String replaceAll(string replace) do?
a) Replace all characters that matches pattern with a replacement string
b) Replace first subsequence that matches pattern with a replacement string
c) Replace all other than first subsequence of that matches pattern with a replacement string
d) Replace every subsequence of the input sequence that matches pattern with a replacement string

Answer: d
Clarification: replaceAll method replaces every subsequence of the sequence that matches pattern with a replacement string.

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

Answer: c
Clarification: public int start() returns index of the previous match in the input string.

Leave a Reply

Your email address will not be published. Required fields are marked *