aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception Question" Watch "Exception Question" New topic
Author

Exception Question

Sim Kim
Ranch Hand

Joined: Aug 06, 2004
Posts: 268
Which statement is true?

A. catch(X x) can catch subclasses of X.
B. The Error class is a RuntimeException.
C. Any statement that can throw an Error must be enclosed in a try block.
D. Any statement that can throw an Exception must be enclosed in a try block.
E. Any statement that can throw a RuntimeException must be enclosed in a try
block.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

A,D


Groovy
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I see this type of question a lot and it really is ambiguous.

In answer B, "a RuntimeException" means "a subclass of RuntimeException".

In answer D, if "an Exception" means "only an instance of class Exception", D is true. If "an Exception" means "an instance of the class Exception or any subclass of Excepton", the D is not true.

Luckily, on the real exam we are told the number of answers expected.

If we take literally, "Which statement is true?", only one answer is expected and it is A.

[ January 12, 2005: Message edited by: Mike Gershman ]
[ January 12, 2005: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

"an Exception" means "an instance of the class Exception or any subclass of Excepton", the D is not true.


I dont understand why is it not true. Is it because RuntimeException is subclass of Exception, so it is possible that subclasses of RTException may be thrown.
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
As I see it, none of the last three (C-E) can be true because the statement that throw something can be inserted in a method that throws something.

Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Pradeep got my point but I think J Isberg really nailed the answer. The key word is "must". A throws clause is an alternative to a try block.

I hope this kind of question is not on the SCJP exam. It's more like a riddle than a test of knowledge. Somone who is well aware of throws clauses (like myself and the other above posters) were fooled by the wording into answering a different question than the one asked.

Some misleading items, like incorrect indentation and deceptive naming of class references, are realistic reflections of life as a programmer. Tricky wording of questions is just an easy way to get some spread in the scores.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Lavjeet Khanuja:
Which statement is true?


C. Any statement that can throw an Error must be enclosed in a try block.
D. Any statement that can throw an Exception must be enclosed in a try block.
E. Any statement that can throw a RuntimeException must be enclosed in a try
block.


I think all 3 are wrong ... because we can avoid try/catch by using throws ...
[ January 12, 2005: Message edited by: rathi ji ]
 
 
subject: Exception Question