Which statements, when inserted at line 1, will cause an exception at run time?
class MyClass {} class MyClass1 extends MyClass {} class MyClass2 extends MyClass {} public class Main{ public static void main (String args []){ MyClass b = new MyClass (); MyClass1 b1 = new MyClass1 (); MyClass2 b2 = new MyClass2 (); // insert statement here }/*from w ww .j ava2 s.c o m*/ }
Select 1 option
Correct Option is : C
For A.
There won't be a problem anytime because MyClass1 is a MyClass
For B.
It fails at Compile time as an object referenced by b may not be a MyClass2, so an explicit cast will be needed.
For C.
It will pass at compile time but fail at run time as the actual object referenced by b is not a MyClass1.
For D.
It will not compile because b 1 can never point to an object of class MyClass2.
For E.
This won't compile. Another cast is needed. i.e. b1 = (MyClass1)(MyClass)b1;