Java Programming — Exception Handling
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 71–80 of 100 questions in Exception Handling
Q.71 Medium Exception Handling
Analyze the code:
try {
// code
} catch (IOException | SQLException e) {
// handle
}
What is this syntax called?
A Multi-catch or union catch
B Chained exception handling
C Nested exception handling
D Parallel exception handling
Correct Answer:  A. Multi-catch or union catch
EXPLANATION

This is called multi-catch or union catch syntax (Java 7+) allowing multiple exception types to be caught in one block using pipe operator.

Take Test
Q.72 Medium Exception Handling
What will happen if a finally block contains a return statement?
A It will override any return from try or catch block
B It will cause a compilation error
C It will be ignored
D It will execute but not return
Correct Answer:  A. It will override any return from try or catch block
EXPLANATION

Return in finally block overrides returns in try or catch blocks. The finally return value is what gets returned.

Take Test
Q.73 Medium Exception Handling
What is the output?
public class Test {
public static void main(String[] args) {
int x = 0;
try {
x = 5;
System.out.println(x / 0);
x = 10;
} catch (ArithmeticException e) {
x = 15;
} finally {
System.out.println(x);
}
}
}
A 5
B 10
C 15
D Compilation error
Correct Answer:  C. 15
EXPLANATION

x becomes 5, then ArithmeticException occurs. x becomes 15 in catch block. Finally block prints 15.

Take Test
Q.74 Medium Exception Handling
Which of these is NOT a correct way to handle checked exceptions?
A Using try-catch block
B Using throws keyword in method signature
C Using try-with-resources
D Ignoring it without any handling
Correct Answer:  D. Ignoring it without any handling
EXPLANATION

Checked exceptions must be either caught or declared using throws. Ignoring them causes compilation error.

Take Test
Q.75 Medium Exception Handling
Consider the code:
try (FileReader fr = new FileReader("file.txt")) {
// use fr
} catch (IOException e) {
// handle
}
What will happen to 'fr' after the try block?
A It remains open and must be manually closed
B It is automatically closed
C It causes a compilation error
D It is closed only if exception occurs
Correct Answer:  B. It is automatically closed
EXPLANATION

Try-with-resources automatically closes resources implementing AutoCloseable interface, regardless of normal or exceptional termination.

Take Test
Q.76 Medium Exception Handling
What happens if you throw an exception inside the finally block?
A The original exception is lost
B Both exceptions are thrown
C The finally block exception is ignored
D Compilation error occurs
Correct Answer:  A. The original exception is lost
EXPLANATION

If an exception is thrown in finally block, it replaces the original exception that was being propagated. The original exception is lost.

Take Test
Q.77 Medium Exception Handling
Analyze the code:
public void process() throws IOException {
try {
readFile();
} catch (FileNotFoundException e) {
// handle
}
}
What is the issue here?
A No issue, it's correct
B FileNotFoundException should not be caught
C IOException should also be caught as it's broader
D throws IOException is redundant
Correct Answer:  C. IOException should also be caught as it's broader
EXPLANATION

Since FileNotFoundException is a subclass of IOException, and readFile() can throw IOException, it should be caught in a separate catch block or the method signature should only throw FileNotFoundException.

Take Test
Q.78 Medium Exception Handling
Which statement about multiple catch blocks is TRUE?
A You can have multiple catch blocks, and all will execute
B You can have multiple catch blocks, but only one will execute
C Multiple catch blocks are not allowed in Java
D Multiple catch blocks execute in reverse order
Correct Answer:  B. You can have multiple catch blocks, but only one will execute
EXPLANATION

Only one catch block executes for a thrown exception. The first matching catch block is executed.

Take Test
What will be the output?
public class Demo {
public static void main(String[] args) {
try {
System.out.println("A");
throw new Exception("Test");
System.out.println("B");
} catch (Exception e) {
System.out.println("C");
}
}
}
A A C B
B A C
C A B C
D A
Correct Answer:  B. A C
EXPLANATION

After 'A' is printed, exception is thrown. Statement 'B' is never executed. Catch block prints 'C'.

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

NumberFormatException is thrown by Integer.parseInt(), Double.parseDouble() when string format is invalid.

Take 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