Java OCA OCP Practice Question 2685

Question

Given:

2. import java.util.regex.*;  
3. public class Main {  
4.   public static void main(String[] args) {  
5.     Pattern p = Pattern.compile(args[0]);  
6.     Matcher m = p.matcher(args[1]);  
7.     while(m.find())  
8.       System.out.print(m.group() + " ");  
9.   } 
10.} 

And the three command-line invocations:

  • I. java Main "0([0-7])?" "1012 0208 430"
  • II. java Main "0([0-7])*" "1012 0208 430"
  • III. java Main "0([0-7])+" "1012 0208 430"

Which are true? (Choose all that apply.)

  • A. All three invocations will return valid octal numbers.
  • B. None of the invocations will return valid octal numbers.
  • C. Only invocations II and III will return valid octal numbers.
  • D. All three invocations will return the same set of valid octal numbers.
  • E. Of those invocations that return only valid octal numbers, each invocation will return a different set of valid octal numbers.


A and E are correct.

Note

The three invocations will return:

  • I. - 01 02 0 0
  • II. - 012 020 0
  • III. - 012 020

Of course, the key to this question is to remember how the three quantifiers (?, *, and +) work.




PreviousNext

Related