This set of Java Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer
Explanation: 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.
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: Group 0 is a special group which represents the entire expression.
5. groupCount reports a total number of Capturing groups.
a) True
b) False
View Answer
Explanation: 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
View Answer
Explanation: \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
View Answer
Explanation: \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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: public int start() returns index of the previous match in the input string.
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Java Internship
- Check Java Books
- Practice Programming MCQs
- Check Programming Books
- Practice BCA MCQs