Java OCA OCP Practice Question 299

Question

Given:

 3. import java.util.regex.*;
 4. public class Main {
 5.   public static void main(String[] args) {
 6.     Pattern p = Pattern.compile(args[0]);
 7.     Matcher m = p.matcher(args[1]);
 8.     int count = 0;
 9.     while(m.find())
10.       count++;// w  w w . j  a v  a  2 s.c o m
11.     System.out.print(count);
12.   }
13. }

And given the command-line invocation:

java Main "\d+" ab2c4d67

What is the result?

  • A. 0
  • B. 3
  • C. 4
  • D. 8
  • E. 9
  • F. Compilation fails


B is correct.

Note

The "\d" metacharacter looks for digits.

The + quantifier says look for "one or more" occurrences.

The find() method will find three sets of one or more consecutive digits: 2, 4, and 67.




PreviousNext

Related