Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 531–540 of 958
Topics in Java Programming
Q.531 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.

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

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

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

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

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

Test
What will be the output?
public class Demo {
public static void main(String[] args) {
try {
System.out.println("A");
throw new Exception("Test");
System.out.println("B");
} catch (Exception e) {
System.out.println("C");
}
}
}
A A C B
B A C
C A B C
D A
Correct Answer:  B. A C
EXPLANATION

After 'A' is printed, exception is thrown. Statement 'B' is never executed. Catch block prints 'C'.

Test
Which exception is thrown when a numeric string cannot be converted to a number?
A NumberFormatException
B ParseException
C ConversionException
D StringFormatException
Correct Answer:  A. NumberFormatException
EXPLANATION

NumberFormatException is thrown by Integer.parseInt(), Double.parseDouble() when string format is invalid.

Test
In Java exception handling, what is the order of execution in try-finally-catch block?
A try → catch → finally
B try → finally → catch
C catch → try → finally
D finally → try → catch
Correct Answer:  A. try → catch → finally
EXPLANATION

The correct order is try block executes first, then catch block (if exception occurs), and finally block always executes at the end.

Test
What is the output of this code?
public class Test {
public static void main(String[] args) {
try {
int[] arr = {1, 2};
System.out.println(arr[5]);
} catch (Exception e) {
System.out.println("Caught");
}
}
}
A Caught
B ArrayIndexOutOfBoundsException
C Compilation error
D No output
Correct Answer:  A. Caught
EXPLANATION

ArrayIndexOutOfBoundsException is caught by the generic Exception catch block, printing 'Caught'.

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