Java Programming MCQs - Regular Expressions
This section focuses on the "Regular Expressions" in Java programming. These Multiple Choice Questions (MCQ) should be practiced to improve the Java programming skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements and other competitive examinations.
1. Java provides the ______ package for pattern matching with regular expressions.
A. regex
B. java.regex
C. util.regex
D. java.util.regex
View Answer
Ans : D
Explanation: Java provides the java.util.regex package for pattern matching with regular expressions.
2. A __________ is a compiled representation of a regular expression.
A. Matcher Object
B. PatternSyntaxException
C. Pattern Object
D. None of the above
View Answer
Ans : C
Explanation: Pattern Class : A Pattern object is a compiled representation of a regular expression. The Pattern class provides no public constructors.
3. Capturing groups are a way to treat multiple characters as a single unit.
A. TRUE
B. FALSE
C. Can be true or false
D. Can not say
View Answer
Ans : A
Explanation: True, Capturing groups are a way to treat multiple characters as a single unit.
4. Which subexpression matches the beginning of the line.
A. $
B. ^
C. &
D. *
View Answer
Ans : B
Explanation: ^ : Matches the beginning of the line.
5. Subexpression $ is used for?
A. End of the entire string
B. Beginning of the entire string
C. Matches any single character not in brackets
D. Matches the end of the line
View Answer
Ans : D
Explanation: $ Matches the end of the line.
6. Which of the following matches the nonword characters?
A. w
B. W
C. C
D. c
View Answer
Ans : B
Explanation: W : Matches the nonword characters.
7. Subexpression S is used for?
A. Matches the word characters
B. Matches the whitespace
C. Matches the nonwhitespace
D. Matches the nondigits
View Answer
Ans : C
Explanation: S Matches the nonwhitespace.
8. Which method returns the start index of the previous match?
A. public int start(int group)
B. public int end()
C. public int end(int group)
D. public int start()
View Answer
Ans : D
Explanation: public int start() : Returns the start index of the previous match
9. What is the use of public boolean find()?
A. Attempts to find the next subsequence of the input sequence that matches the pattern.
B. Attempts to match the input sequence, starting at the beginning of the region, against the pattern.
C. Attempts to match the entire region against the pattern.
D. None of the above
View Answer
Ans : A
Explanation: public boolean find() : Attempts to find the next subsequence of the input sequence that matches the pattern.
10. E Ends quoting begun with Q.
A. Yes
B. No
C. Can be yes or no
D. Can not say
View Answer
Ans : A
Explanation: Yes, E Ends quoting begun with Q.
Discussion