Java OCA OCP Practice Question 2715

Question

Given:

1. class Printable {  
2.   String doStuff() { return "howdy "; }  
3. }  /*from w  w  w.  j av  a  2 s.co m*/
4. class Main extends Printable {  
5.   String doStuff() { return "send money "; }  
6.   public static void main(String[] args) {  
7.      Main s1 = new Main();     
8.      Printable c3 = new Printable();        
9.      Printable c1 = s1;                  
10.     Main s2 = (Main) c1;    
11.     Main s3 = (Main) c3;     
12.     Main s4 = new Printable();     
13. } } 

Which are true? (Choose all that apply.)

  • A. Compilation succeeds.
  • B. The code runs without exception.
  • C. If the line(s) of code that do NOT compile (if any) are removed, the code runs without exception.
  • D. If the line(s) of code that do NOT compile (if any) are removed, the code throws an exception at runtime.


D is correct.

Note

Once line 12 is removed, the code will compile but throw a ClassCastException at line 11.




PreviousNext

Related