Java OCA OCP Practice Question 211

Question

Diagram://ww w. j a  v  a  2  s  .c  o  m

                       Animal 
                         |
                       Mammal
                         |
 +----------------+------+--------+----------------+
 |                |               |                |
                 Cat           Raccoon           Fish 
Dog         (implements      (implements          
              Washer)          Washer) 




                                        

Consider the following code:

1. Cat myCat; 
2. Washer myObject; 
3. Fish pogo; 
4. 
5. myCat = new Cat(); 
6. myObject = myCat; 
7. pogo = (Fish)myObject; 

Which of the following statements is true? (Choose one.)

  • A. Line 6 will not compile; an explicit cast is required to convert a Cat to a Washer.
  • B. Line 7 will not compile, because you cannot cast an interface to a class.
  • C. The code will compile and run, but the cast in line 7 is not required and can be eliminated.
  • D. The code will compile but will throw an exception at line 7, because runtime conversion from an interface to a class is not permitted.
  • E. The code will compile but will throw an exception at line 7, because the runtime class of myObject cannot be converted to type Fish.


E.

Note

The cast in line 7 is required.

Answer D is a preposterous statement expressed in a tone of authority.




PreviousNext

Related