aspose file tools*
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
Author

please explane me why is it so

varinder mahajan
Ranch Hand

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


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


~Gaurav<br />SCJP5
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
You need to QuoteYourSources when posting questions like this.


Ping & DNS - my free Android networking tools app
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
SCJA
Anit Nair
Greenhorn

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: http://aspose.com/file-tools
 
subject: please explane me why is it so