Java OCA OCP Practice Question 3008

Question

Given:

public class Main {
   public static void main(String[] args) {
      for (int x = 1; x < args.length; x++)
         System.out.print(args[0].split(args[x]).length + " ");
   }
}

And the command-line invocation:

java Main "x1 23 y #" "\d" "\s" "\w" 

What is the result?

  • A. 4 4 6
  • B. 3 3 6
  • C. 5 4 6
  • D. 4 6 4
  • E. 3 6 3
  • F. 5 6 4
  • G. Compilation fails.
  • H. An exception is thrown at runtime.


A is correct.

Note

For the "regex-related" parts of the exam you need to remember the three metacharacters "\d" (which means digit), "\s" (which means a whitespace character), and "\w" (which means a word character-i.e., letters, digits, or the underscore character).

With that said, the calls to the split() method are tokenizing calls.

In this case, we're using metacharacters as our token delimiters.

In the first iteration of the for loop, digits are the delimiters.

In the second iteration, whitespace characters are the delimiters.

And in the final iteration, "word" characters are the delimiters.




PreviousNext

Related