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? Answer: d 2. What is the significance of Matcher class for regular expression in java? Answer: c 3. Object of which class is used to compile regular expression? Answer: a 4. Which capturing group can represent the entire expression? Answer: b 5. groupCount reports a total number of Capturing groups. Answer: a 6. Which of the following matches nonword character using regular expression in java? Answer: b 7. Which of the following matches end of the string using regular expression in java? Answer: a 8. What does public int end(int group) return? Answer: a 9. what does public String replaceAll(string replace) do? Answer: d 10. What does public int start() return? Answer: c
a) Pattern class
b) matcher class
c) PatternSyntaxException
d) Regex class
Clarification: java.util.regex consists 3 classes. PatternSyntaxException indicates syntax error in regex.
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.
Clarification: macther() method is invoked using matcher object which interpretes pattern and performs match operations in the input string.
a) Pattern class
b) Matcher class
c) PatternSyntaxException
d) None of the mentioned
Clarification: object of Pattern class can represent compiled regular expression.
a) group *
b) group 0
c) group * or group 0
d) None of the mentioned
Clarification: Group 0 is a special group which represents the entire expression.
a) True
b) False
Clarification: groupCount reports total number of Capturing groups. this does not include special group, group 0.
a) w
b) W
c) s
d) S
Clarification: W matches nonword characters. [0-9], [A-Z] and _ (underscore) are word characters. All other than these characters are nonword characters.
a) z
b) \
c) *
d) Z
Clarification: z is used to match end of the entire string in regular expression in java.
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
Clarification: public int end(int group) returns offset from the last character of the subsequent group.
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
Clarification: replaceAll method replaces every subsequence of the sequence that matches pattern with a replacement string.
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
Clarification: public int start() returns index of the previous match in the input string.