File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please explane me why is it so Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "please explane me why is it so" Watch "please explane me why is it so" New topic

please explane me why is it so

varinder mahajan
Ranch Hand

Joined: Jun 11, 2008
Posts: 47
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();
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

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.

~Gaurav<br />SCJP5
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
You need to QuoteYourSources when posting questions like this.
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
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
Can we mark a class static like this. I read that only inner nested class could be marked static.

Anand Shrivastava
Anit Nair

Joined: May 09, 2008
Posts: 12
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
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.

SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
I agree. Here's the link:
subject: please explane me why is it so
jQuery in Action, 3rd edition