Java SE Mock Test
Topic: Exception Handling
1. Exceptions defined by 'Error' and 'RuntimeException' classes and
their subclasses are known as:
Option 1: Unchecked exception
Option 2: Checked exception
Option 3: Can be checked or unchecked both
Option 4: none of the above
Correct Answer: Unchecked exception
2. All exceptions in java are derived from:
Option 1: Exceptions class
Option 2: Throwable class
Option 3: Error class
Option 4: both A and B
Correct Answer: Throwable class
3. What will be the output of the below program:
public class exception_example {
public static void main(String[] args) {
div(4, 0);
}
public static void div(int a, int b) {
try {
int x = (a / b);
[Link]("x=" + x);
} catch (Exception exp) {
[Link]("Exception caught in catch-1");
} catch (ArithmeticException e) {
[Link]("Exception caught in catch-2");
}
}
}
Option 1: Exception caught in catch-2
Option 2: Exception caught in catch-1
Option 3: Exception caught
Option 4: Compilation error
Correct Answer: Compilation error
4. Which of these keywords must be used to handle the exception
thrown by try block in some rational manner?
Option 1: try
Option 2: catch
Option 3: finally
Option 4: throw
Correct Answer: catch
5. Which of these class is related to all the exceptions that cannot be
caught?
Option 1: Runtime Exception
Option 2: Error
Option 3: both A and B
Option 4: none of the above
Correct Answer: Error
6. Which of these handles the exception when no catch is used?
Option 1: throw handler
Option 2: final
Option 3: Java run time system
Option 4: Default handler
Correct Answer: Default handler
7. Which of these operator is used to generate an instance of an
exception than can be thrown by using throw?
Option 1: calloc
Option 2: malloc
Option 3: new
Option 4: thrown
Correct Answer: new
8. Which exception will the following throw?
public class Test {
public static void main(String[] args) {
Object obj = new Integer(3);
String str = (String) obj;
[Link](str);
}
}
Option 1: ArrayIndexOutOfBoundsException
Option 2: IllegalArgumentException
Option 3: ClassCastException
Option 4: NumberFormatException
Correct Answer: ClassCastException
9. unchecked(runtime) exception in java is:
Option 1: ArrayIndexOutOfBoundsException
Option 2: ArithmeticException
Option 3: NullPointerException
Option 4: All of the above
Correct Answer: All of the above
10. What will happen when catch and finally block both return value?
Option 1: method will return value returned by finally block
Option 2: method will return value returned by catch block
Option 3: finally block won't execute
Option 4: None
Correct Answer: method will return value returned by finally block
11. 'throws' keyword is used to:
Option 1: Propagate the exception to the caller of the method
Option 2: Define a new exception type
Option 3: Throw an exception explicitly
Option 4: both A and B
Correct Answer: Propagate the exception to the caller of the method
12. Which option can we use in the try block to stop the execution of
the “finally” block?
Option 1: return
Option 2: [Link](0)
Option 3: break
Option 4: continue
Correct Answer: [Link](0)
13. Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
[Link]("A");
} finally {
[Link]("B");
}
[Link]("C");
}
}
Option 1: Compile-time error
Option 2: ABC
Option 3: AC
Option 4: BC
Correct Answer: ABC
14. When a finally block executed in Java?
Option 1: Try block is executed without any exception
Option 2: Exception has occurred
Option 3: Executed at last
Option 4: None of these
Correct Answer: Executed at last
15. Predict the output of following Java program?
class Main {
public static void main(String args[]) {
try {
throw 10;
} catch (int e) {
[Link]("Got the Exception " + e);
}
}
}
Option 1: Got the Exception 10
Option 2: Got the Exception 0
Option 3: Compiler Error
Option 4: None of the above
Correct Answer: Compiler Error
16. If exception condition is not handled explicitly in a java program,
then in case of occurrence of an exception, the uncaught exception
will be handled by:
Option 1: main thread's default exception handler.
Option 2: It will never be handled and the program ends abruptly.
Option 3: both A and B
Option 4: None of the above
Correct Answer: main thread's default exception handler.
17. What is the output of the below Java program with nested
exceptions?
public class ExceptionTest14 {
public static void main(String[] args) {
try {
int a = 10 / 1;
try {
int b = 20 / 1;
} catch (Exception e1) {
[Link]("b=20");
}
} catch (Exception e2) {
[Link]("a=10");
}
}
}
Option 1: a=10 b=20
Option 2: b=20 a=10
Option 3: Compiler error
Option 4: No output
Correct Answer: No output (Logical) / Compiler error (Mock Key)
18. The variables initialized inside a TRY-with-resource are treated like
____ variables in Java.
Option 1: static
Option 2: instance
Option 3: final
Option 4: None of the above
Correct Answer: final
19. What are the difference between Checked and Unchecked
exceptions in Java?
Option 1: Unchecked exceptions produce less number of compiler errors than the code with
Checked exceptions.
Option 2: Most of the unchecked exceptions have RuntimeException as the superclass. Checked
exceptions have different classes as superclasses.
Option 3: Unchecked exceptions cause more number of runtime exceptions at the time of
execution. As the Checked exceptions are handled well with TRY CATCH blocks, you will get less
number of exceptions or errors at the time of execution.
Option 4: All the above
Correct Answer: All the above
20. Which is the superclass of Error class in Java?
Option 1: RuntimeException
Option 2: Exception
Option 3: Throwable
Option 4: None of the above
Correct Answer: Throwable
21. Which is the exception handling block that closes the used
resources inside of it automatically in Java?
Option 1: TRY
Option 2: CATCH
Option 3: FINALLY
Option 4: TRY with resource
Correct Answer: TRY with resource
22. In which of the following package Exception class exist?
Option 1: [Link]
Option 2: [Link]
Option 3: [Link]
Option 4: [Link]
Correct Answer: [Link]
23. Which statement is Correct?
Option 1: A try statement must have at least one corresponding catch block.
Option 2: Multiple catch statements can catch the same class of exception more than once.
Option 3: Except in case of VM shutdown, if a try block starts to execute, a corresponding finally
block will always start to execute.
Option 4: An Error that might be thrown in a method must be declared as thrown by that method, or
be handled within that method.
Correct Answer: Except in case of VM shutdown, if a try block starts to execute, a
corresponding finally block will always start to execute.
24. User-defined exceptions can be created by creating a subclass of
______ class.
Option 1: Input
Option 2: Exception
Option 3: Declaration
Option 4: Method
Correct Answer: Exception
25. Which of these exceptions handles the divide by zero error?
Option 1: MathException
Option 2: ArithmeticException
Option 3: IllegarException
Option 4: IllegalAccessException
Correct Answer: ArithmeticException
26. What happen in case of multiple catch blocks?
Option 1: The superclass exception cannot caught first.
Option 2: The superclass exception must be caught first.
Option 3: Either super or subclass can be caught first.
Option 4: None of these
Correct Answer: The superclass exception cannot caught first.
27. Which of these are [Link] in exception handling in java:
Option 1: IOError
Option 2: AssertionError
Option 3: VirtualMachineError
Option 4: All the above
Correct Answer: All the above
28. Which of the following should be true of the object thrown by a
thrown statement?
Option 1: Should be assignable to Error type
Option 2: Should be assignable to String type
Option 3: Should be assignable to Exception type
Option 4: Should be assignable to Throwable type
Correct Answer: Should be assignable to Throwable type
29. Which of these methods return description of an exception?
Option 1: getException()
Option 2: getMessage()
Option 3: obtainDescription()
Option 4: obtainException()
Correct Answer: getMessage()
30. Which of the following statements is correct? A. The exception is
unrecoverable. B. The error is recoverable by debugging.
Option 1: only A
Option 2: only B
Option 3: both A and B
Option 4: none of these
Correct Answer: none of these
Java SE Mock Exam - Exception Handling