Java Programming Multiple Choice Questions - Exception Handling
This section focuses on the "Exception Handling" In Java programming. These Multiple Choice Questions (MCQ) should be practiced to improve the Java programming skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements and other competitive examinations.
1. Which statement is true?
A. catch(X x) can catch subclasses of X where X is a subclass of Exception.
B. Any statement that can throw an Exception must be enclosed in a try block.
C. The Error class is a RuntimeException.
D. Any statement that can throw an Error must be enclosed in a try block.
View Answer
Ans : A
Explanation: Option A is correct. If the class specified in the catch clause does have subclasses, any exception object that subclasses the specified class will be caught as well.
Option B is wrong. The error class is a subclass of Throwable and not Runtime Exception.
Option C is wrong. You do not catch this class of error.
Option D is wrong. An exception can be thrown to the next method higher up the call stack.
2. Which statement is true?
A. An Error that might be thrown in a method must be declared as thrown by that method, or be handled within that method.
B. Multiple catch statements can catch the same class of exception more than once.
C. A try statement must have at least one corresponding catch block.
D. Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start to execute.
View Answer
Ans : D
Explanation: A is wrong. A try statement can exist without catch, but it must have a finally statement.
B is wrong. A try statement executes a block. If a value is thrown and the try statement has one or more catch clauses that can catch it, then control will be transferred to the first such catch clause. If that catch block completes normally, then the try statement completes normally.
C is wrong. Exceptions of type Error and RuntimeException do not have to be caught, only checked exceptions (java.lan
3. When does Exceptions in Java arises in code sequence?
A. Run Time
B. Can Occur Any Time
C. Compilation Time
D. None of the mentioned
View Answer
Ans : A
Explanation: Exceptions in Java are run-time errors.
4. Which of these keywords is not a part of exception handling?
A. finally
B. thrown
C. catch
D. try
View Answer
Ans : B
Explanation: Exceptional handling is managed via 5 keywords try, catch, throws, throw and finally.
5. Which of these keywords must be used to monitor for exceptions?
A. finally
B. throw
C. catch
D. try
View Answer
Ans : D
Explanation: Try keywords must be used to monitor for exceptions
6. Which of these keywords must be used to handle the exception thrown by try block in some rational manner?
A. finally
B. throw
C. catch
D. try
View Answer
Ans : C
Explanation: If an exception occurs within the try block, it is thrown and cached by catch block for processing.
7. Which of these keywords is used to manually throw an exception?
A. finally
B. throw
C. catch
D. try
View Answer
Ans : B
Explanation: Throw keywords is used to manually throw an exception.
8. Which of these is a super class of all errors and exceptions in the Java language?
A. Catchable
B. Throwable
C. RunTimeExceptions
D. None of the above
View Answer
Ans : B
Explanation: Throwable is a super class of all errors and exceptions in the Java language
9. In which of the following package Exception class exist?
A. java.file
B. java.lang
C. java.io
D. java.util
View Answer
Ans : B
Explanation: No explanation.
10. Which exception is thrown when divide by zero statement executes?
A. NumberFormatException
B. NullPointerException
C. ArithmeticException
D. None of these
View Answer
Ans : C
Explanation: ArithmeticException is thrown when divide by zero statement executes.
11. What is the output of this program?
class Main
{
public static void main(String args[])
{
try
{
System.out.print("Hello" + " " + 1 / 0);
}
catch(ArithmeticException e)
{
System.out.print("World");
}
}
}
A. Hello
B. World
C. HelloWorld
D. Hello World
View Answer
Ans : B
Explanation: System.out.print() function first converts the whole parameters into a string and then prints, before ""Hello"" goes to output stream 1 / 0 error is encountered which is cached by catch block printing just ""World"".
12. What is the output of this program?
class Main
{
public static void main(String args[])
{
try
{
int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
}
}
A. A
B. B
C. Compilation Error
D. Runtime Error
View Answer
Ans : B
Explanation: No explanation
13. What is the output of this program?
class Main
{
public static void main(String args[])
{
try
{
int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
}
}
A. A
B. B
C. AC
D. BC
View Answer
Ans : D
Explanation: finally keyword is used to execute the code before try and catch block end.
14. What is the output of this program?
class Main
{
public static void main(String args[])
{
try
{
int i, sum;
sum = 10;
for (i = -1; i < 3 ;++i)
sum = (sum / i);
}
catch(ArithmeticException e)
{
System.out.print("0");
}
System.out.print(sum);
}
}
A. 0
B. 5
C. Compilation Error
D. Runtime Error
View Answer
Ans : C
Explanation: Value of variable sum is printed outside of try block, sum is declared only in try block, outside try block it is undefined.
15. Predict the output of following Java program?
class Main
{
public static void main(String args[]) {
try {
throw 10;
}
catch(int e) {
System.out.println("Got the Exception " + e);
}
}
}
A. Got the Exception 10
B. Got the Exception 0
C. Compiler Error
D. None of the above
View Answer
Ans : C
Explanation: In Java only throwable objects (Throwable objects are instances of any subclass of the Throwable class) can be thrown as exception. So basic data type can no be thrown at all.
16. What will be output for the following code?
class Test extends Exception { }
class Main {
public static void main(String args[]) {
try {
throw new Test();
}
catch(Test t) {
System.out.println("Got the Test Exception");
}
finally {
System.out.println("Inside finally block ");
}
}
}
A. Got the Test Exception
Inside finally block
B. Got the Test Exception
C. Compiler Error
D. Inside finally block
View Answer
Ans : A
Explanation: In Java, the finally is always executed after the try-catch block. This block can be used to do the common cleanup work. There is no such block in C++.
17. Output of following Java program?
class Main {
public static void main(String args[])
{
int x = 0;
int y = 10;
int z = y/x;
}
}
A. Compiler Error
B. Compiles and runs fine
C. Compiles fine but throws ArithmeticException exception
D. None of the above
View Answer
Ans : C
Explanation: ArithmeticException is an unchecked exception, i.e., not checked by the compiler. So the program compiles fine.
18. What will be output for the following code?
class Base extends Exception {}
class Derived extends Base {}
public class Main {
public static void main(String args[])
{
try
{
throw new Derived();
}
catch(Base b)
{
System.out.println("Caught base class exception");
}
catch(Derived d)
{
System.out.println("Caught derived class exception");
}
}
}
A. Caught base class exception
B. Caught derived class exception
C. Compiler Error because derived is not throwable
D. Compiler Error because base class exception is caught before derived class
View Answer
Ans : D
Explanation: No explanation.
19. What will be output for the following code?
class Test
{
public static void main (String[] args)
{
try
{
int a = 0;
System.out.println("a = " + a);
int b = 20 / a;
System.out.println("b = " + b);
}
catch(ArithmeticException e)
{
System.out.println("Divide by zero error");
}
finally
{
System.out.println("inside the finally block");
}
}
}
A. Compile error
B. Divide by zero error
C. Divide by zero error
inside the finally block
D. inside the finally block
View Answer
Ans : C
Explanation: On division of 20 by 0, divide by zero exception occurs and control goes inside the catch block. Also, the finally block is always executed whether an exception occurs or not.
20. What is the output of this program?
class Main
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}
A. Finally
B. Compilation fails.
C. The code runs with no output.
D. An exception is thrown at runtime.
View Answer
Ans : A
Explanation: If you put a finally block after a try and its associated catch blocks, then once execution enters the try block, the code in that finally block will definitely be executed
21. Which of these is a super class of all exceptional type classes?
A. String
B. RuntimeExceptions
C. Throwable
D. Cacheable
View Answer
Ans : C
Explanation: All the exception types are subclasses of the built in class Throwable.
22. Which of these class is related to all the exceptions that can be caught by using catch?
A. Error
B. Exception
C. RuntimeExecption
D. All of the mentioned
View Answer
Ans : B
Explanation: Error class is related to java run time error that can't be caught usually, RuntimeExecption is subclass of Exception class which contains all the exceptions that can be caught.
23. Which of these class is related to all the exceptions that cannot be caught?
A. Error
B. Exception
C. RuntimeExecption
D. All of the mentioned
View Answer
Ans : A
Explanation: Error class is related to java run time error that can't be caught usually, RuntimeExecption is subclass of Exception class which contains all the exceptions that can be caught.
24. Which of these handles the exception when no catch is used?
A. Default handler
B. finally
C. throw handler
D. Java run time system
View Answer
Ans : A
Explanation: None.
25. What exception thrown by parseInt() method?
A. ArithmeticException
B. ClassNotFoundException
C. NullPointerException
D. NumberFormatException
View Answer
Ans : D
Explanation: parseInt() method parses input into integer. The exception thrown by this method is NumberFormatException.
26. What is the output of this program?
class Main
{
public static void main(String args[])
{
try
{
System.out.print("Hello" + " " + 1 / 0);
}
finally
{
System.out.print("World");
}
}
}
A. Hello
B. World
C. Compilation Error
D. First Exception then World
View Answer
Ans : D
Explanation: None.
Output:
$ javac exception_handling.java
$ java exception_handling
Exception in thread ""main"" java.lang.ArithmeticException: / by zero
World
27. What is the output of this program?
class Main
{
public static void main(String args[])
{
try
{
int i, sum;
sum = 10;
for (i = -1; i < 3 ;++i)
{
sum = (sum / i);
System.out.print(i);
}
}
catch(ArithmeticException e)
{
System.out.print("0");
}
}
}
A. -1
B. 0
C. -10
D. -101
View Answer
Ans : C
Explanation: For the 1st iteration -1 is displayed. The 2nd exception is caught in catch block and 0 is displayed.
Output:
$ javac exception_handling.java
$ java exception_handling
-10
28. Which of these operator is used to generate an instance of an exception than can be thrown by using throw?
A. new
B. malloc
C. alloc
D. thrown
View Answer
Ans : A
Explanation: new is used to create an instance of an exception. All of java's built in run-time exceptions have two constructors: one with no parameters and one that takes a string parameter.
29. Which of these keywords is used to by the calling function to guard against the exception that is thrown by called function?
A. try
B. throw
C. throws
D. catch
View Answer
Ans : C
Explanation: If a method is capable of causing an exception that it does not handle. It must specify this behaviour the behaviour so that callers of the method can guard themselves against that exception. This is done by using throws clause in methods declaration.
30. What is the output of this program?
class Main
{
public static void main(String args[])
{
try
{ System.out.print("A");
throw new NullPointerException ("Hello");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
}
}
A. A
B. B
C. Hello
D. NullPointerException
View Answer
Ans : D
Explanation: None.
Output:
$ javac exception_handling.java
$ java exception_handling
Exception in thread ""main"" java.lang.NullPointerException: Hello
at exception_handling.main
31. What is the output of this program?
class Main
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}
A. Finally
B. Compilation fails
C. The code runs with no output
D. An exception is thrown at runtime
View Answer
Ans : A
Explanation: Because finally will execute always.
32. A single try block must be followed by which of these?
A. finally
B. catch
C. finally & catch
D. none of the mentioned
View Answer
Ans : C
Explanation: try block can be followed by any of finally or catch block, try block checks for exceptions and work is performed by finally and catch block as per the exception.
B is wrong. A try statement executes a block. If a value is thrown and the try statement has one or more catch clauses that can catch it, then control will be transferred to the first such catch clause. If that catch block completes normally, then the try statement completes normally.
C is wrong. Exceptions of type Error and Runt
33. Which of these exceptions handles the divide by zero error?
A. ArithmeticException
B. MathException
C. IllegalAccessException
D. IllegarException
View Answer
Ans : A
Explanation: None.
34. Which of these exceptions will occur if we try to access the index of an array beyond its length?
A. ArithmeticException
B. ArrayException
C. ArrayIndexException
D. ArrayIndexOutOfBoundsException
View Answer
Ans : D
Explanation: ArrayIndexOutOfBoundsException is a built in exception that is caused when we try to access an index location which is beyond the length of an array.
35. What is the output of this program?
class Main
{
public static void main(String args[])
{
try
{
int a = args.length;
int b = 10 / a;
System.out.print(a);
}
catch (ArithmeticException e)
{
System.out.println("1");
}
}
}
A. 0
B. 1
C. Compilation Error
D. Runtime Error
View Answer
Ans : B
Explanation: None.
Output:
advertisement
$ javac exception_handling.java
$ java exception_handling
1
Also check :
Discussion