Java OCA OCP Practice Question 2340

Question

Which statement is true about the following code?

public class Main {

  static void print1(List<String> lst) {     // (1)
    for(String element : lst) {
      System.out.print(element + " ");
    }// w  ww. j  a  v  a  2s.c  o  m
  }

  static void print2(List<String> lst) {     // (2)
    for(Object element : lst) {
      System.out.print(element + " ");
    }
  }

  static void print3(List<?> lst) {          // (3)
    for(Object element : lst) {
      System.out.print(element + " ");
    }
  }

  static <T> void print4(List<T> lst) {      // (4)
    for(Object element : lst) {
      System.out.print(element + " ");
    }
  }

  static <T> void print5(List<T> lst) {     // (5)
    for(T element : lst) {
      System.out.print(element + " ");
    }
  }
}

Select the one correct answer.

  • (a) The formal type parameter specification for the methods in (1), (2), and (3) is missing.
  • (b) The generic methods in (4) and (5) should be declared in a generic class.
  • (c) The element type Object for the local variable element in the for(:) loop header of the method in (3) is inconsistent with the element type of the list.
  • (d) The element type Object for the local variable element in the for(:) loop header of the method in (4) is inconsistent with the element type of the list.
  • (e) The program will compile without warnings.
  • (f) None of the above.


(e)

Note

The methods in (1), (2), and (3) are not generic, but the methods in (4) and (5) are.

A generic method need not be declared in a generic class.

Regardless of what type an object has, it is still an Object.




PreviousNext

Related