Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 541–550 of 958
Topics in Java Programming
Analyze the nested try-catch:
try {
try {
int x = 5/0;
} catch(NullPointerException e) {
System.out.println("Inner");
}
} catch(ArithmeticException e) {
System.out.println("Outer");
}
A Inner
B Outer
C Inner Outer
D Neither prints
Correct Answer:  B. Outer
EXPLANATION

ArithmeticException is thrown by 5/0. Inner catch only catches NullPointerException, so it propagates to outer catch which handles ArithmeticException. Output: Outer

Test
Q.542 Medium Exception Handling
What will happen if you don't catch a checked exception?
A Compilation error
B Runtime error
C It will be automatically handled
D Program will run successfully
Correct Answer:  A. Compilation error
EXPLANATION

Checked exceptions must be either caught in a try-catch block or declared in the method signature with 'throws'. Failure to do so results in a compilation error.

Test
Q.543 Medium Exception Handling
Which exception hierarchy is correct in Java?
A Throwable → Error → Exception → RuntimeException
B Throwable → Exception → RuntimeException and Throwable → Error
C Exception → Throwable → Error → RuntimeException
D RuntimeException → Exception → Throwable → Error
Correct Answer:  B. Throwable → Exception → RuntimeException and Throwable → Error
EXPLANATION

Throwable is the root. It has two subclasses: Exception and Error. Exception has RuntimeException as a subclass. Checked exceptions extend Exception directly.

Test
Q.544 Medium Exception Handling
Analyze this code:
public void test() throws IOException {
// method body
}
What does 'throws' indicate?
A The method will definitely throw IOException
B The method might throw IOException and caller must handle it
C IOException is caught inside the method
D IOException is prevented from being thrown
Correct Answer:  B. The method might throw IOException and caller must handle it
EXPLANATION

The 'throws' keyword declares that a method might throw the specified checked exception. The responsibility of handling is passed to the caller of the method.

Test
Which exception is thrown when a string cannot be converted to a number?
A NumberFormatException
B ConversionException
C ParseException
D StringConversionException
Correct Answer:  A. NumberFormatException
EXPLANATION

NumberFormatException is thrown when attempting to convert a string to a numeric type but the string does not have an appropriate format. It's an unchecked exception.

Test
Q.546 Medium Exception Handling
What is the output?
try {
int x = 5/0;
} catch(Exception e) {
System.out.println("Caught");
} catch(ArithmeticException ae) {
System.out.println("Arithmetic");
}
A Caught
B Arithmetic
C Compilation Error
D Caught Arithmetic
Correct Answer:  C. Compilation Error
EXPLANATION

This is a compilation error. Catch blocks must be ordered from most specific to most general. ArithmeticException is more specific than Exception, so it should come first.

Test
Q.547 Medium Exception Handling
Which statement about try-with-resources is TRUE?
A It requires manual closing of resources
B It was introduced in Java 6
C Resources are automatically closed in reverse order of opening
D It cannot be used with multiple resources
Correct Answer:  C. Resources are automatically closed in reverse order of opening
EXPLANATION

Try-with-resources (Java 7+) automatically closes resources implementing AutoCloseable. Resources are closed in reverse order. Multiple resources can be declared with semicolon separation.

Test
What happens if an exception is thrown in the finally block?
A It suppresses the original exception
B It is ignored
C It replaces the original exception
D It causes a compilation error
Correct Answer:  C. It replaces the original exception
EXPLANATION

If an exception occurs in the finally block, it will replace any exception thrown in the try or catch block, and the new exception will be propagated.

Test
Q.549 Medium Exception Handling
Which of the following exceptions is a checked exception?
A ClassCastException
B FileNotFoundException
C StackOverflowError
D NullPointerException
Correct Answer:  B. FileNotFoundException
EXPLANATION

FileNotFoundException is a checked exception (extends IOException). Others are unchecked - ClassCastException and NullPointerException are RuntimeExceptions, StackOverflowError is an Error.

Test
Analyze the code:
try {
return 5;
} finally {
return 10;
}
What will be returned?
A 5
B 10
C Error
D Null
Correct Answer:  B. 10
EXPLANATION

The finally block always executes, and if it contains a return statement, it overrides the return value from try or catch block. So 10 is returned.

Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips