Java OCA OCP Practice Question 3128

Question

Consider the following program:

import java.util.*;

public class Main {
       public static void main(String []args) {
              String text = "<head>first program </head> <body>hello world</body>";
              Set<String> words = new TreeSet<>();
              try ( Scanner tokenizingScanner = new Scanner(text) ) {
                      tokenizingScanner.useDelimiter("\\W");
                      while(tokenizingScanner.hasNext()) {
                              String word = tokenizingScanner.next();
                              if(!word.trim().equals("")) {
                                      words.add(word);
                              }/* w  w w  .j a  v  a  2 s .c  o m*/
                      }
              }
              for(String word : words) {
                      System.out.print(word + " ");
              }
       }
}

Which one of the following options correctly provides the output of this program?

  • a) hello body program head first world
  • b) body first head hello program world
  • c) head first program head body hello world body
  • d) head first program body hello world
  • e) < </ >


b)

Note

TreeSet<String> orders the strings in default alphabetical ascending order and removes duplicates.

The delimiter \W is non-word, so the characters such as < act as separators.




PreviousNext

Related