Java OCA OCP Practice Question 2702

Question

Which of these statements can fill in the blank so that the Main class compiles successfully?

(Choose all that apply.)

3:    public class Main { 
4:       public static <U extends Exception> void printException(U u) { 
5:          System.out.println(u.getMessage()); 
6:       } 
7:       public static void main(String[] args) { 
8:          __________________________________ 
9:       } 
10:   } 
  • A. Main.printException(new FileNotFoundException("A"));
  • B. Main.printException(new Exception("B"));
  • C. Main.<Throwable>printException(new Exception("C"));
  • D. Main.<NullPointerException>printException(new NullPointerException("D"));
  • E. Main.printException(new Throwable("E"));


A, B, D.

Note

The generic type must be Exception or a subclass of Exception since this is an upper bound.

C and E are wrong because Throwable is a superclass of Exception.

D uses an odd syntax by explicitly listing the type, but you should be able to recognize it as acceptable.




PreviousNext

Related