Java OCA OCP Practice Question 3133

Question

What will be printed when the program is compiled and run?

public class Main {
  public static void main(String[] args) {
    List<Integer> intList = new ArrayList<Integer>();
    Set<Double> doubleSet = new HashSet<Double>();
    List<?>          list = intList;
    Set<?>            set = doubleSet;

    scuddle(intList);/*from w w  w .j a v  a  2 s  .  c om*/
    scuddle(doubleSet);
    scuddle(list);
    scuddle(set);
  }

  private static void scuddle(Collection<?> col) {
    if (col instanceof List<?>) {
      System.out.println("I am a list.");
    } else if (col instanceof Set<?>) {
      System.out.println("I am a set.");
    }
  }
}

Select the one correct answer.

(a) The method scuddle() will not compile.
(b) The method main() will not compile./* w  w  w .j av  a2  s  .c  o m*/
(c) The  program  will  compile,  but  issue  an  unchecked  warning  in  method
    scuddle(). It will run and terminate normally with the following output:

    I am a list.
    I am a set.
    I am a list.
    I am a set.

(d) The  program  will  compile,  but  issue  an  unchecked  warning  in  the  method
    main(). When run, it will throw an exception.
(e) The program will compile without any unchecked warnings. It will run and
    terminate normally, with the following output:

    I am a list.
    I am a set.
    I am a list.
    I am a set.

(f) The program will compile without any unchecked warnings. It will run and
    terminate normally, with the following output:

    I am a list.
    I am a set.
(g) None of the above.


(e)

Note

Instance tests in the scuddle() method use the reified type List<?>.

All assignments in the main() method are type-safe.




PreviousNext

Related