Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 261–270 of 476
Topics in Java Programming
Q.261 Medium Exception Handling
Which statement is TRUE about exception propagation in Java?
A Exceptions propagate only upward in call stack
B Exceptions propagate downward in call stack
C Exceptions don't propagate
D Exceptions propagate both ways
Correct Answer:  A. Exceptions propagate only upward in call stack
EXPLANATION

When an exception is thrown and not caught, it propagates up the call stack until a matching catch block is found or the program terminates.

Take Test
Q.262 Medium Exception Handling
Which of the following cannot be used with try-with-resources?
A FileInputStream
B BufferedReader
C String
D Scanner
Correct Answer:  C. String
EXPLANATION

Try-with-resources requires resources to implement AutoCloseable interface. String doesn't implement it, so it cannot be used.

Take Test
Q.263 Medium Exception Handling
Consider this code:
public static void main(String[] args) {
try {
System.out.println("A");
return;
} finally {
System.out.println("B");
}
}
What is the output?
A A B
B A
C B A
D B
Correct Answer:  A. A B
EXPLANATION

Even with return in try block, finally block executes. So 'A' prints first, then 'B' before returning.

Take Test
Q.264 Medium Exception Handling
What is the difference between throw and throws?
A throw creates exception, throws declares it
B throws creates exception, throw declares it
C Both are same
D throw is for checked, throws for unchecked
Correct Answer:  A. throw creates exception, throws declares it
EXPLANATION

'throw' keyword actually throws an exception object. 'throws' keyword declares that a method throws a checked exception.

Take Test
Q.265 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.266 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.267 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.268 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.269 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.270 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
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