Question 41
MediumWhat is the difference between checked and unchecked exceptions in Java?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
What is the difference between checked and unchecked exceptions in Java?
Unchecked exceptions can't be caught
Checked exceptions must be caught or declared with throws; unchecked (RuntimeException) need not be
Checked exceptions are faster
There is no difference
Sign up free to see the answer and a step-by-step explanation.
What is the difference between throw and throws in Java?
throws raises the exception immediately
throw is only for errors
They are the same
throw raises an exception; throws in a method signature declares exceptions it may raise
Sign up free to see the answer and a step-by-step explanation.
Which exception does Java throw for int[] a = new int[3]; a[3] = 1;?
ArithmeticException
NullPointerException
ClassCastException
ArrayIndexOutOfBoundsException
Sign up free to see the answer and a step-by-step explanation.
Which exception does Java throw for 10 / 0 with integers?
NumberFormatException
ArithmeticException
No exception; the result is infinity
IllegalStateException
Sign up free to see the answer and a step-by-step explanation.
Which exception does Integer.parseInt("12a") throw?
ClassCastException
ArithmeticException
NumberFormatException
IOException
Sign up free to see the answer and a step-by-step explanation.
Why is catch (Exception e) {} with an empty body a bad idea?
It doesn't compile
It makes the program faster
It silently hides errors, making bugs hard to find
It catches nothing
Sign up free to see the answer and a step-by-step explanation.
When does the finally block NOT run in Java?
When the try block returns
When an exception is thrown
When the JVM exits, for example through System.exit()
When the catch block runs
Sign up free to see the answer and a step-by-step explanation.
What happens to an exception thrown in a method that doesn't catch it?
The method restarts
It is converted into a warning
It is ignored
It propagates up the call stack to the caller
Sign up free to see the answer and a step-by-step explanation.
Which is the right place to release a file handle so it happens whether or not an exception occurs?
Only the catch block
Only the try block
A finally block, or try-with-resources / with
After the method returns
Sign up free to see the answer and a step-by-step explanation.
What is a custom exception, such as InsufficientFundsException, mainly for?
Making programs faster
Giving callers a clear, specific error type they can handle separately
Avoiding try blocks
Replacing return values entirely
Sign up free to see the answer and a step-by-step explanation.