Java Exception Handling MCQ Questions
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
Also check :
Discussion