Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 511–520 of 958
Topics in Java Programming
Which of the following cannot throw a checked exception without being declared in throws clause?
A Constructor
B Instance initializer block
C Static initializer block
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

Checked exceptions in constructors, instance initializers, and static initializers must be handled with try-catch or declared in throws (constructors only). This is enforced by Java compiler.

Test
Q.512 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.

Test
Which of the following is NOT a characteristic of Exception class in Java?
A It extends Throwable
B It is always checked exception
C Its subclasses may be checked or unchecked
D It can be instantiated directly
Correct Answer:  B. It is always checked exception
EXPLANATION

Exception class has both checked subclasses (like IOException) and unchecked subclasses (like RuntimeException). Not all exceptions extending Exception are checked.

Test
Q.514 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.

Test
Which exception is thrown when a method argument is null but shouldn't be?
A NullArgumentException
B NullPointerException
C InvalidArgumentException
D ArgumentNullException
Correct Answer:  B. NullPointerException
EXPLANATION

NullPointerException is thrown when attempting to use an object reference that hasn't been instantiated. Java doesn't have NullArgumentException.

Test
Q.516 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'.

Test
Q.517 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.

Test
What is the difference between 'throw' and 'throws' keywords in Java?
A throw is used to declare exceptions, throws is used to throw them
B throw creates an exception object and throws it, throws declares that a method may throw exceptions
C They are synonyms and can be used interchangeably
D throws is used only with try-catch blocks
Correct Answer:  B. throw creates an exception object and throws it, throws declares that a method may throw exceptions
EXPLANATION

throw explicitly throws an exception object; throws declares in method signature that exceptions may be thrown. Example: throw new IOException() vs public void method() throws IOException.

Test
Q.519 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.

Test
Q.520 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.

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