Java OCA OCP Practice Question 2885

Question

Given:

2. class Shape { }  
3. class Rectangle extends Shape { }  
4. public class Square extends Rectangle {  
5.   public static void main(String[] args) {  
6.     Shape p4 = new Square();  
7.     // insert code here  
8. } } 

And the following six declarations (which are to be inserted independently at line 7):

  • I. Shape p5 = p4;
  • II. Shape p6 = (Rectangle)p4;
  • III. Rectangle b2 = (Rectangle)p4;
  • IV. Rectangle b3 = (Square)p4;
  • V. Square s1 = (Rectangle)p4;
  • VI. Square s2 = (Square)p4;

Which are true? (Choose all that apply.)

  • A. All six declarations will compile.
  • B. Exactly one declaration will not compile.
  • C. More than one of the declarations will not compile.
  • D. Of those declarations that compile, none will throw an exception.
  • E. Of those declarations that compile, exactly one will throw an exception.
  • F. Of those declarations that compile, more than one will throw an exception.


B and D are correct.

Note

Declaration V will not compile because Square might have methods that Rectangle doesn't have.

The other five declarations run without an exception.




PreviousNext

Related