• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11333
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!