• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Ques form a Mock Exam

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
the following question is in Mock exam @ http://valiveru.tripod.com/java/jvaltest.html
Question 2.
Given that method aMethod() throws BaseException, SubException and RuntimeException of the following exception hierarchy
java.lang.Exception
|
+ - - BaseException
|
+ - - SubException
|
+ - - java.lang.RuntimeException

Which of the following are legal

A.public class MyClass {
public void myMethod(){
aMethod();
}
}
B.public class MyClass{
public void myMethod() throws BaseException,RuntimeException{
aMethod();
}
}
C.public class MyClass{
public void myMethod() throws BaseException{
aMethod();
}
}
D.public class MyClass{
public void myMethod() throws Exception{
aMethod();
}
}
E.public class MyClass{
public void myMethod() throws RuntimeException {
aMethod();
}
}
Could anyone please suggest the right answers with explanations.
Thanks
Manish
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Answer C looks correct for me
As the all exceptions like BaseException ... are extended from Exception, throwing 'Exception' will be enough to mention all of them.
Sorry for my bad english
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jafarali, I think you meant to say D ??
Ajith
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
So ultimately which is the correct answer ....is it "D" ?

Aruna
 
Manish Poddar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer given at the abovesaid site is C and E !!!
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How can e be correct as it throws runtime exception & the other exceptions are not caught or thrown.
Please make it clear.
Thanks
mita
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends
My answer would be C and D. That means either of them will suffice. Agreed that only D also suffices coz it specifies mother of all exceptions.
Just read the Question. "Which of the following ARE legal?". So I would add C to the answer also.
E is definitely not correct. Bcoz E catches only RuntimeException which is, as all of us know, redundant and more importantly it ignores the BaseException and Exception which are bound to be caught/declared in this case.
Any affirmation?
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ranjan Sarangi:
Hello Friends
My answer would be C and D.


I agree with u .
But did any of u check out option B.
It throws BaseException which covers SubException as well !
By the way the exception hierarchy is :
java.lang.Exception
   |
   + - - BaseException
     |
     + - - SubException
  |
  + -java.lang.RuntimeException
[This message has been edited by Deepak M (edited July 19, 2000).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak,
BaseException certainly catches the SubException , but what about RunTimeException... It is not the subclass of BaseException, So that makes B an illegal option.
Corrections??
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If Exception Hierarcay is what Deepak given then B ,C & D are correct. You need not to mention RuntimeException, because it's implesit that all method will throw RuntimeException. Need not to mension again.
Am I correct?.
Regards,
Holla.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Holla,


If Exception Hierarcay is what Deepak given then B ,C & D are correct. You need not to mention RuntimeException, because it's implesit that all method will throw RuntimeException. Need not to mension again.
Am I correct?.
Regards,
Holla.


If you say 'RuntimeException' need not to mension then,the answer 'C,D' enough right?. In 'C' it covers 'BaseException'.
My answer is 'C','D'
Any comments?

Thanks
Nirmala

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which one is the correct exception hierarchy.
I think if hierarchy mentioned by Manish is correct then only ans D should be write.
Ans A is incorrect
Ans B & C are incorrect as they are not taking care of SubException.( In Ans B declaring Runtime Exception is not necessary, as RuntimeExceptions are not checked exception, Please refer to RHE page 145-146)
Ans D is correct as it takes care of both Base and Sub Exception.
Ans E is incorrect as it is not handling Base and Sub Exception.
If exception hierarchy is as per Deepak then
ans B C D should be write.
Correct me if I am wrong.
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic