Java OCA OCP Practice Question 2142

Question

What will the program print when compiled and run?

public class Main {
 public static void main(String[] args) {
   System.out.print(Pattern.compile("\\s+")
                      .matcher("|    To be    |\n|or \tnot \t\t\tto be|")
                      .replaceAll(" "));
 }
}

    

Select the one correct answer.

(a) | To be |/*from  w w w.  j a v a  2 s  .co m*/
    |or not to be|
(b) | To be |
    |or  not    to be|
(c) | To be |
    |or \tnot \t\t\tto be|
(d) | To be | |or not to be|
(e) | To be ||or not to be|
(f) | To be ||or \tnot \tto be|
(g) The program will not compile.
(h) The program will throw an exception when run.


(d)

Note

Each sequence of white space is replaced by a single space ( ' '), including the newline (\n) character.




PreviousNext

Related