Java OCA OCP Practice Question 2780

Question

Given:

51.  String s = "4.5x8.a.3";  
52.  String[] tokens = s.split("\\s");  
53.  for(String o: tokens)  
54.    System.out.print(o + " ");  
55.  
56.  System.out.print("  ");  
57.  tokens = s.split("\\..");  
58.  for(String o: tokens)  
59.    System.out.print(o + " "); 

What is the result?

  • A. 4 x8
  • B. 4 x8 .
  • C. 4 x8 3
  • D. 4 5x8 a 3 4 x8
  • E. 4.5x8.a.3 4 x8
  • F. 4.5x8.a.3 4 x8 .


E is correct.

Note

The first invocation of split() uses the "\s" metacharacter, which splits on whitespace.

Since there is no whitespace in String s, the entire string is placed into tokens[0].

The second invocation of split() combines "\\." (which means look for "."), with a standalone ".", which is the metacharacter for "find any character."

So the second split() reads "split on a dot followed by any character."

Note that the split() method does not change the String being split.




PreviousNext

Related