Java Questions & Answers – Regular Expression

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

Answer: d
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

Answer: c
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

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

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

Answer: b
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

Answer: a
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

Answer: b
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

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

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

Answer: a
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

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

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

Answer: c
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.