Java OCA OCP Practice Question 18

Question

What is the output of the following program?

import java.util.HashSet;
import java.util.Iterator;

public class Main {

   public static void main(String args[]) {
      HashSet<String> set = new HashSet<>();
      String s1 = "abc";
      String s2 = "def";
      String s3 = "";
      set.add(s1);/*from ww w.  ja va2 s .  c  om*/
      set.add(s2);
      set.add(s1);
      set.add(s2);
      Iterator<String> i = set.iterator();
      while (i.hasNext()) {
         s3 +=  i.next();
      }
      System.out.println(s3);
   }
}
  • A. abcdefabcdef
  • B. defabcdefabc
  • C. fedcbafedcba
  • D. defabc


D.

Note

Sets may not have duplicate elements.




PreviousNext

Related