Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 251–260 of 476
Topics in Java Programming
Q.251 Medium Exception Handling
Consider the following code. What will be printed?
public class FinallyTest {
public static int getValue() {
try {
return 10;
} finally {
System.out.println("Finally");
}
}
public static void main(String[] args) {
System.out.println(getValue());
}
}
A Finally 10
B 10 Finally
C Finally
D 10
Correct Answer:  A. Finally 10
EXPLANATION

finally block executes before the return statement completes. System.out.println in finally executes first, then the return value 10 is printed.

Take Test
Q.252 Medium Exception Handling
Which exception is thrown when a thread tries to acquire a monitor that is already locked?
A InterruptedException
B IllegalMonitorStateException
C ThreadStateException
D Threads never throw exceptions during synchronization
Correct Answer:  B. IllegalMonitorStateException
EXPLANATION

IllegalMonitorStateException is thrown when an invalid monitor state operation occurs, such as calling notify() on an object not owned by the current thread.

Take Test
Q.253 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.

Take Test
Q.254 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.

Take Test
Q.255 Medium Exception Handling
What will be printed?
public class Test {
public static void main(String[] args) {
try {
System.out.println("In try");
return;
} catch (Exception e) {
System.out.println("In catch");
} finally {
System.out.println("In finally");
}
}
}
A In try\nIn catch\nIn finally
B In try\nIn finally
C In try only
D Compilation error
Correct Answer:  B. In try\nIn finally
EXPLANATION

Try block executes printing 'In try', then return is encountered. Finally block still executes printing 'In finally' before method returns. Catch is skipped as no exception occurs.

Take Test
Q.256 Medium Exception Handling
What happens if you use 'return' statement inside a finally block?
A The return statement is ignored
B The return statement in finally overrides any return in try/catch
C A compilation error occurs
D The finally block is skipped
Correct Answer:  B. The return statement in finally overrides any return in try/catch
EXPLANATION

A return statement in finally will override and suppress any return value or exception from try/catch blocks. This is considered a bad practice.

Take Test
Q.257 Medium Exception Handling
What is the output of this code?
public class ExceptionDemo {
public static void main(String[] args) {
try {
int arr[] = new int[2];
arr[5] = 10;
} catch (Exception e) {
System.out.println("Exception caught");
} finally {
System.out.println("Finally block");
}
}
}
A Exception caught
B Exception caught\nFinally block
C Finally block
D No output
Correct Answer:  B. Exception caught\nFinally block
EXPLANATION

ArrayIndexOutOfBoundsException is caught by Exception catch block, printing 'Exception caught'. Finally block always executes regardless, printing 'Finally block'.

Take Test
Q.258 Medium Exception Handling
Consider this code snippet. What will happen?
try {
int x = 5/0;
} catch (NullPointerException e) {
System.out.println("Caught NPE");
}
A Prints 'Caught NPE'
B ArithmeticException will be thrown and not caught
C Program will terminate silently
D A compilation error will occur
Correct Answer:  B. ArithmeticException will be thrown and not caught
EXPLANATION

5/0 throws ArithmeticException, not NullPointerException. The catch block only handles NPE, so ArithmeticException propagates to the caller.

Take Test
Q.259 Medium Exception Handling
Which statement about try-with-resources (introduced in Java 7) is correct?
A It can only handle one resource at a time
B It automatically calls close() method on AutoCloseable resources
C It eliminates the need for finally blocks completely
D It requires explicit resource declaration in finally block
Correct Answer:  B. It automatically calls close() method on AutoCloseable resources
EXPLANATION

Try-with-resources automatically invokes close() on any resource implementing AutoCloseable interface. Multiple resources can be declared with semicolon separation.

Take Test
Q.260 Medium Exception Handling
What will be the behavior if an exception is thrown in the finally block?
A It will be silently ignored
B It will replace any exception thrown in try/catch blocks
C It will be added to the exception suppression list
D The program will definitely crash
Correct Answer:  B. It will replace any exception thrown in try/catch blocks
EXPLANATION

If an exception occurs in finally, it will propagate and override any previous exception from try/catch blocks. However, in try-with-resources, exceptions are suppressed.

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