Java OCA OCP Practice Question 2945

Question

Given this code in a method:

5.     String s = "dogs. with words.";  
6.     // insert code here     
7.     for(String o: output)  
8.       System.out.print(o + " "); 

Which of the following, inserted independently at line 6, will produce output that contains the String "dogs"? (Choose all that apply.).

  • A. String[] output = s.split("s");
  • B. String[] output = s.split("d");
  • C. String[] output = s.split("\\d");
  • D. String[] output = s.split("\\s");
  • E. String[] output = s.split("\\w");
  • F. String[] output = s.split("\\.");


C, D, and F are correct.

Note

The "\\d" tells the split() method to split Strings whenever a digit is encountered.

The "\\s" tells the split() method to split Strings whenever a whitespace character is encountered.

Because a standalone "." (dot) is a predefined metacharacter (like \d or \s), the "\\." tells the split method to split Strings whenever the character "." is encountered.

A is incorrect because "s" characters are viewed as splitting characters and are removed from the output.

B is incorrect because "d" characters are viewed as splitting characters.

E is incorrect because the "\\w" tells the split() method to use letters, digits, and the underscore character as splitting characters.




PreviousNext

Related