Java OCA OCP Practice Question 1482

Question

Which variable declaration is the first line not to compile?

30:  class Shape {} 
31:  class Rectangle extends Shape{} 
32: /*from ww  w .ja  v  a  2  s .  com*/
33:  public void convert() { 
34:     Shape b =  new Shape(); 
35:     Rectangle h = new Rectangle(); 
36:     Shape bh = new Rectangle(); 
37:     Rectangle p = (Rectangle) b; 
38:     Rectangle q = (Rectangle) h; 
39:     Rectangle r = (Rectangle) bh; 
40:  } 
  • A. p
  • B. q
  • C. r
  • D. None of the above


D.

Note

Shape and Rectangle are defined correctly.

Any Rectangle or Shape reference can potentially be a Rectangle.

The compiler does not know which ones work and which don't.

This means all three casts compile.




PreviousNext

Related