Java OCA OCP Practice Question 2580

Question

What is the result of the following code?

public class Browsers { 
   static class Browser { 
      public void go() { 
         System.out.println("Inside Browser"); 
      } //from   w  w w .j  av  a  2 s  .  c o  m
   } 
   static class Firefox extends Browser { 
      public void go() { 
         System.out.println("Inside Firefox"); 
      } 
   } 
   static class IE extends Browser { 
      @Override public void go() { 
         System.out.println("Inside IE"); 
      } 
   } 
   public static void main(String[] args) { 
      Browser b = new Firefox(); 
      IE e = (IE) b; 
      e.go(); 
   } 
} 
  • A. Inside Browser
  • B. Inside Firefox
  • C. Inside IE
  • D. The code does not compile.
  • E. A runtime exception is thrown.


E.

Note

The main method tries to cast a Firefox instance to IE.

Since IE is not a subclass of Firefox, this throws a ClassCastException.




PreviousNext

Related