Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 271–280 of 476
Topics in Java Programming
Q.271 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.272 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
Q.273 Medium Exception Handling
What will happen if you don't catch a checked exception?
A Compilation error
B Runtime error
C It will be automatically handled
D Program will run successfully
Correct Answer:  A. Compilation error
EXPLANATION

Checked exceptions must be either caught in a try-catch block or declared in the method signature with 'throws'. Failure to do so results in a compilation error.

Take Test
Q.274 Medium Exception Handling
Which exception hierarchy is correct in Java?
A Throwable → Error → Exception → RuntimeException
B Throwable → Exception → RuntimeException and Throwable → Error
C Exception → Throwable → Error → RuntimeException
D RuntimeException → Exception → Throwable → Error
Correct Answer:  B. Throwable → Exception → RuntimeException and Throwable → Error
EXPLANATION

Throwable is the root. It has two subclasses: Exception and Error. Exception has RuntimeException as a subclass. Checked exceptions extend Exception directly.

Take Test
Q.275 Medium Exception Handling
Analyze this code:
public void test() throws IOException {
// method body
}
What does 'throws' indicate?
A The method will definitely throw IOException
B The method might throw IOException and caller must handle it
C IOException is caught inside the method
D IOException is prevented from being thrown
Correct Answer:  B. The method might throw IOException and caller must handle it
EXPLANATION

The 'throws' keyword declares that a method might throw the specified checked exception. The responsibility of handling is passed to the caller of the method.

Take Test
Q.276 Medium Exception Handling
What is the output?
try {
int x = 5/0;
} catch(Exception e) {
System.out.println("Caught");
} catch(ArithmeticException ae) {
System.out.println("Arithmetic");
}
A Caught
B Arithmetic
C Compilation Error
D Caught Arithmetic
Correct Answer:  C. Compilation Error
EXPLANATION

This is a compilation error. Catch blocks must be ordered from most specific to most general. ArithmeticException is more specific than Exception, so it should come first.

Take Test
Q.277 Medium Exception Handling
Which statement about try-with-resources is TRUE?
A It requires manual closing of resources
B It was introduced in Java 6
C Resources are automatically closed in reverse order of opening
D It cannot be used with multiple resources
Correct Answer:  C. Resources are automatically closed in reverse order of opening
EXPLANATION

Try-with-resources (Java 7+) automatically closes resources implementing AutoCloseable. Resources are closed in reverse order. Multiple resources can be declared with semicolon separation.

Take Test
Q.278 Medium Exception Handling
Which of the following exceptions is a checked exception?
A ClassCastException
B FileNotFoundException
C StackOverflowError
D NullPointerException
Correct Answer:  B. FileNotFoundException
EXPLANATION

FileNotFoundException is a checked exception (extends IOException). Others are unchecked - ClassCastException and NullPointerException are RuntimeExceptions, StackOverflowError is an Error.

Take Test
Q.279 Medium Exception Handling
What is the output of multiple catch blocks?
try {
int[] arr = {1, 2, 3};
System.out.println(arr[5]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Index");
} catch(Exception e) {
System.out.println("Exception");
}
A Index
B Exception
C Both Index and Exception
D No output
Correct Answer:  A. Index
EXPLANATION

ArrayIndexOutOfBoundsException is thrown and caught by the first catch block. The second catch block is not executed. Only the most specific matching catch block executes.

Take Test
Q.280 Medium Exception Handling
What will be the output?
try {
throw new Exception("Test");
} catch(Exception e) {
System.out.println("Caught");
} finally {
System.out.println("Finally");
}
A Caught Finally
B Finally
C Caught
D Test Finally
Correct Answer:  A. Caught Finally
EXPLANATION

The exception is caught and prints "Caught". The finally block always executes after try-catch, printing "Finally". Output: Caught Finally

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