Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 501–510 of 958
Topics in Java Programming
What will be the output?
public class Test {
public static void main(String[] args) {
try {
throw new Exception("Test");
} catch (Exception e) {
System.out.println("1");
} catch (Exception e) {
System.out.println("2");
}
}
}
A 1
B 1 2
C Compilation error
D 2
Correct Answer:  C. Compilation error
EXPLANATION

Multiple catch blocks with the same exception type cause compilation error. Java doesn't allow duplicate catch blocks for identical exception types.

Test
What is the output of the following code?
public class ExceptionTest {
public static void main(String[] args) {
try {
int x = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Caught");
} finally {
System.out.println("Finally");
}
}
}
A Caught Finally
B Finally
C Caught
D Program terminates with exception
Correct Answer:  A. Caught Finally
EXPLANATION

ArithmeticException is caught by the catch block printing 'Caught', then finally block executes printing 'Finally'.

Test
Which of the following statements about try-catch-finally blocks is correct?
A finally block is executed even if return statement is present in try block
B finally block is skipped if catch block throws an exception
C finally block cannot contain return statements
D finally block is optional only when catch block is present
Correct Answer:  A. finally block is executed even if return statement is present in try block
EXPLANATION

The finally block always executes regardless of whether a return, break, continue, or exception occurs in try or catch blocks, unless System.exit() is called.

Test
What is suppressed exception in Java (introduced in Java 7)?
A An exception that occurs but is hidden from the programmer
B Exceptions thrown during resource closure in try-with-resources are added to suppressed list of primary exception
C An exception that doesn't need to be caught
D An exception that only occurs in finally blocks
Correct Answer:  B. Exceptions thrown during resource closure in try-with-resources are added to suppressed list of primary exception
EXPLANATION

In try-with-resources, if an exception occurs in try and another in resource close(), the second is added as suppressed exception to the first using addSuppressed(), preserving all exception information.

Test
Consider a method signature: public void readFile() throws IOException. Which statement is correct?
A This method will definitely throw IOException
B This method may throw IOException that caller must handle
C IOException will never occur in this method
D The caller can ignore IOException
Correct Answer:  B. This method may throw IOException that caller must handle
EXPLANATION

throws declaration indicates the method may throw IOException. The caller is responsible for handling it with try-catch or declaring throws. The method doesn't always throw it.

Test
What is the execution order in try-catch-finally for nested exception handling?
A Inner try → Inner catch → Inner finally → Outer try → Outer catch → Outer finally
B Outer try → Outer catch → Inner try → Inner catch → Inner finally → Outer finally
C All finally blocks execute before any catch blocks
D Execution order depends on compiler optimization
Correct Answer:  B. Outer try → Outer catch → Inner try → Inner catch → Inner finally → Outer finally
EXPLANATION

Nested exception handling follows inner-to-outer order. Inner try-catch-finally completes fully, then control passes to outer blocks. Finally blocks execute in their respective scope order.

Test
Which exception is thrown when attempting to access an index outside the bounds of an array?
A IndexOutOfRangeException
B ArrayIndexOutOfBoundsException
C ArrayIndexException
D OutOfBoundsException
Correct Answer:  B. ArrayIndexOutOfBoundsException
EXPLANATION

ArrayIndexOutOfBoundsException is thrown when attempting to access array elements with invalid indices. It's an unchecked exception extending RuntimeException.

Test
What happens when an unchecked exception is not caught?
A Compilation fails
B The exception propagates up the call stack until caught or program terminates
C The exception is automatically converted to checked exception
D The finally block is skipped
Correct Answer:  B. The exception propagates up the call stack until caught or program terminates
EXPLANATION

Unchecked exceptions (RuntimeException subclasses) don't require explicit handling. If not caught, they propagate up the call stack, eventually terminating the thread if not caught anywhere.

Test
Q.509 Medium Exception Handling
Which method is used to get the exception that caused the current exception in Java?
A getException()
B getCause()
C getRootCause()
D getOriginalException()
Correct Answer:  B. getCause()
EXPLANATION

The getCause() method returns the Throwable that caused the current exception, enabling exception chaining analysis and debugging.

Test
Q.510 Medium Exception Handling
What is the output of exception chaining in Java?
A Multiple exceptions are processed simultaneously
B One exception wraps another exception preserving the original cause
C All exceptions are converted to RuntimeException
D The first exception is ignored
Correct Answer:  B. One exception wraps another exception preserving the original cause
EXPLANATION

Exception chaining wraps a caught exception within another exception using initCause() or constructor, preserving the original exception for debugging and stack trace analysis.

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