Home Subjects Java Programming Exception Handling

Java Programming
Exception Handling

Java OOP, collections, multithreading

30 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–30 of 30
Topics in Java Programming
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
Which exception is thrown when a string cannot be converted to a number?
A NumberFormatException
B ConversionException
C ParseException
D StringConversionException
Correct Answer:  A. NumberFormatException
EXPLANATION

NumberFormatException is thrown when attempting to convert a string to a numeric type but the string does not have an appropriate format. It's an unchecked exception.

Test
Which exception is thrown when trying to access a method of a null object?
A NullAccessException
B NullPointerException
C NullReferenceException
D EmptyObjectException
Correct Answer:  B. NullPointerException
EXPLANATION

NullPointerException is thrown when attempting to call a method or access a property on a null object reference in Java.

Test
Which of the following is NOT a subclass of Throwable in Java?
A Error
B Exception
C RuntimeException
D Runnable
Correct Answer:  D. Runnable
EXPLANATION

Runnable is a functional interface, not related to exception hierarchy. Error and Exception are direct subclasses of Throwable. RuntimeException is a subclass of Exception.

Test
Can you have a try block without a catch block in Java?
A No, catch block is mandatory
B Yes, if finally block is present
C Yes, if return statement is present
D No, you need at least one catch or finally
Correct Answer:  B. Yes, if finally block is present
EXPLANATION

A try block must be followed by either a catch block or a finally block (or both). A try block alone is not valid syntax.

Test
Which exception is thrown when a method receives an illegal or inappropriate argument?
A IllegalStateException
B IllegalArgumentException
C InvalidParameterException
D ParameterException
Correct Answer:  B. IllegalArgumentException
EXPLANATION

IllegalArgumentException is thrown to indicate that a method has been passed an illegal or inappropriate argument. It is an unchecked exception.

Test
Which of the following is a checked exception in Java?
A NullPointerException
B IOException
C ArrayIndexOutOfBoundsException
D ArithmeticException
Correct Answer:  B. IOException
EXPLANATION

IOException is a checked exception that must be caught or declared in the method signature. NullPointerException, ArrayIndexOutOfBoundsException, and ArithmeticException are unchecked exceptions.

Test
IGET
IGET AI
Online · Exam prep assistant
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