Regex in Java MCQs Exercise I

Ques 1 Regex


Which class is used to work with regular expressions in Java?

A Regex
B Pattern
C Matcher
D RegexPattern

Ques 2 Regex


What will be the output of the following code snippet?

Pattern pattern = Pattern.compile("sc");
Matcher matcher = pattern.matcher("scode");
System.out.println(matcher.find());

A false
B true
C Compilation error
D Runtime error

Ques 3 Regex


Which method is used to replace occurrences of a regex pattern with a specified string in Java?

A replace()
B replaceAll()
C replaceFirst()
D substitute()

Ques 4 Regex


What will be the output of the following Java code snippet?

Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("1234");
while (matcher.find()) {
    System.out.print(matcher.group() + " ");
}

A 1 2 3 4
B 1234
C Compilation error
D Runtime error

Ques 5 Regex


What is the metacharacter used to match zero or one occurrence of the preceding character in a regex pattern in Java?

A +
B *
C |
D ?