Java OCA OCP Practice Question 283

Question

Given:

import java.util.regex.*;
class Main {/*from  w ww.  j  a va2 s . c om*/
  public static void main(String [] args) {
    Pattern p = Pattern.compile(args[0]);
    Matcher m = p.matcher(args[1]);
    System.out.print("match positions: ");
    while(m.find()) {
      System.out.print(m.start() + " ");
    }
    System.out.println("");
  }
}

Which invocation(s) will produce the output: 0 2 4 8 ?

Choose all that apply.

  • A. java Main "\b" "^23 *$76 bc"
  • B. java Main "\B" "^23 *$76 bc"
  • C. java Main "\S" "^23 *$76 bc"
  • D. java Main "\W" "^23 *$76 bc"
  • E. None of the above
  • F. Compilation fails
  • G. An exception is thrown at runtime


B is correct.

Note

Remember that the boundary metacharacters (\b and \B), act as though the string being searched is bounded with invisible, non-word characters.

Then remember that \B reports on boundaries between word to word or non-word to non-word characters.




PreviousNext

Related