This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Associate Certification (OCAJP 7) and the fly likes OCA7: 1Z0-803:  Mock Exam questions ME-Q31 and ME-Q39 from Mala Gupta Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7)
Bookmark "OCA7: 1Z0-803:  Mock Exam questions ME-Q31 and ME-Q39 from Mala Gupta" Watch "OCA7: 1Z0-803:  Mock Exam questions ME-Q31 and ME-Q39 from Mala Gupta" New topic

OCA7: 1Z0-803: Mock Exam questions ME-Q31 and ME-Q39 from Mala Gupta

Thomas Hauck
Ranch Hand

Joined: Jun 07, 2011
Posts: 57
ME-Q31. Ideally, which of the following should never be handled by an exception
handler? (Select 2 options.)
a) StackOverflowError
b) OutOfMemoryError
c) ArrayIndexOutOfBoundError
d) ClassLoadingException
e) CompilationError
f) OutOfStorageError

Answer: a, b
Explanation: Options (c), (d), (e), and (f) are incorrect because the Java API does not define these exception or error classes.
Options (a) and (b) are correct. You should never try to handle these errors in your code because StackOverflowError and OutOfMemoryError are serious errors.

----- -----
I understand why a) and b) are the correct answers because these are system errors beyond the control of the programmer.
----- -----
ME-Q31 contradicts ME-Q39 unless ME-Q39 is a hypothetical question.

ME-Q39. Given the following code, which of the following statements are true? (Select 3 options.)

a) The code will print caught.
b) The code will not print caught.
c) The code would print caught if StackOverflowError were a runtime exception.
d) The code would print caught if StackOverflowError were a checked exception.
e) The code will print caught if question() throws exception NullPointer-Exception.

Answer: a, c, d
Explanation: Option (a) is correct. The control will be transferred to the exception handler for StackOverflowError when it is encountered. Hence it will print caught.
Options (c) and (d) are correct. Exception handlers execute when the corresponding checked or runtime exceptions are thrown.
Option (e) is incorrect. An exception handler for class StackOverflow can’t handle exceptions of class NullPointerException because NullPointerException is not
a superclass of StackOverflowError.

Question: Why does the Java compiler allow one to catch an error like StackOverflowError when error is something that should not be anticipated by the programmer?

It is generally accepted in Java that you do not need to worry about errors. They are intended to indicate fatal problems or virtual machine errors.
[P106 Learning Java] - You are actively discouraged from trying to catch or recover from them because they are supposed to indicate a fatal program bug, not a routine condition.

The code "MyExam" compiles.
Question: Why would a programmer anticipate a StackOverflowError when it is generally accepted that errors are not to be handled by the programmer?
Roel De Nijs

Joined: Jul 19, 2004
Posts: 5139

That's an easy one: it is not because you should never try to catch (or handle) an OutOfMemoryError or StackOverflowError that you can't do it (e.g. just for fun ). The compiler doesn't care about which exceptions or errors you catch (or handle) as long as you catch (or handle) each checked exception appropriately. So for the exam you have to know 2 things about these errors:
  • It's considered bad practice if you try to catch (or handle) Errors (like OutOfMemoryError or StackOverflowError), so never do it.
  • But if you decide catching (or handling) an Error is the thing you want to do, then the same rules apply as with regular exceptions.

  • Note: Errors extend from the Error class, so these errors are not considered as checked exceptions by the compiler (so compilation won't fail if you catch such an Error).

    Hope it helps!

    SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
    I agree. Here's the link:
    subject: OCA7: 1Z0-803: Mock Exam questions ME-Q31 and ME-Q39 from Mala Gupta
    Similar Threads
    Dan's question
    Exceptions (long one) Your response requested.
    Exam Objectives Flow Control.
    Question related to exception
    is CloneNotSupportedException Checked Exception.