Given: 11. static classA { 12. void process() throws Exception { throw new Exception(); } 13. } 14. static class B extends A { 15. void process() { System.out.println(�B �); } 16. } 17. public static void main(String[] args) { 18.A a=new B(); 19. a.process(); 20.} What is the result? A. B B. The code runs with no output. C. An exception is thrown at runtime. D. Compilation fails because of an error in line 15. E. Compilation fails because of an error in line 18. F. Compilation fails because of an error in line 19. Answer: F
Beat the world,if you can.......
Gaurav Bhatia
Ranch Hand
Joined: Jan 01, 2007
Posts: 49
posted
0
The call is using reference of class A. And as per the method definition i.e process method in class A, it throws an Exception. So, while making the call you need to enclose this in try catch blocks. HTH
Was this the whole source given? This code works only if present in a well formed class.
It had me confused a bit.
Java hobbyist.
Anand Shrivastava
Ranch Hand
Joined: Jul 22, 2007
Posts: 125
posted
0
Can we mark a class static like this. I read that only inner nested class could be marked static.
Anand Shrivastava
SCJA
Anit Nair
Greenhorn
Joined: May 09, 2008
Posts: 12
posted
0
At compile time there is no way for compiler to know what object the reference variable points to, in this case it may be object of A or object of B. Since you are using reference of class A and the method defined for class a throws exception, compiler will generate error if the exception is not handled or declared.
Sandeep Bhandari
Ranch Hand
Joined: Apr 16, 2004
Posts: 201
posted
0
surely the answer is compilation problem
since the code starts from line 11 and hence we can assume that this is part of a valid class.