Regex in Java MCQs Exercise II

Ques 1 Regex


What method is used to compile a regular expression in Java?

A compile()
B pattern()
C regex()
D matcher()

Ques 2 Regex


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

Pattern pattern = Pattern.compile("\\d{3}");
Matcher matcher = pattern.matcher("1234");
System.out.println(matcher.matches());

A true
B false
C Compilation error
D Runtime error

Ques 3 Regex


What is the metacharacter used to match any character in a regex pattern in Java?

A .
B *
C +
D ?