Java OCA OCP Practice Question 3276

Question

Consider the following program and choose the appropriate option:

import java.util.*;

public class Test {
     public static void main(String []args) {
             Set<Integer> set = new LinkedHashSet<Integer>(); //#1
             LinkedHashSet<Integer> set2 = new HashSet<Integer>(); //#2
             SortedSet<Integer> set3 = new TreeSet<Integer>(); //#3
             SortedSet<Integer> set4 = new NavigableSet<Integer>(); //#4
     }/*  www. jav a2  s. c  o  m*/
}
  • a) Statements #1 and #2 will compile successfully.
  • b) Statements #1 and #3 will compile successfully.
  • c) Statements #1, #2, and #3 will compile successfully.
  • d) Statements #2 and #4 will compile successfully.


b)

Note

LinkedHashSet inherits from Set so statement #1 will compile.

TreeSet inherits from SortedSet so statement #3 will also compile successfully.

LinkedHashSet is inherited from HashSet so statement #2 will not compile.

Statement #4 tries to create an object of type NavigableSet which is an interface, so it will also not compile.




PreviousNext

Related