Java MCQs on Exceptions types in Java Programming Language.
1. Which of these is a super class of all exceptional type classes? Answer: c 2. Which of these class is related to all the exceptions that can be caught by using catch? Answer: b 3. Which of these class is related to all the exceptions that cannot be caught? Answer: a 4. Which of these handles the exception when no catch is used? Answer: a 5. What exception thrown by parseInt() method? Answer: d 6. What will be the output of the following Java code? a) Hello 7. What will be the output of the following Java code? a) -1
a) String
b) RuntimeExceptions
c) Throwable
d) Cacheable
Clarification: All the exception types are subclasses of the built in class Throwable.
a) Error
b) Exception
c) RuntimeExecption
d) All of the mentioned
Clarification: 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.
a) Error
b) Exception
c) RuntimeExecption
d) All of the mentioned
Clarification: 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.
a) Default handler
b) finally
c) throw handler
d) Java run time system
Clarification: None.
a) ArithmeticException
b) ClassNotFoundException
c) NullPointerException
d) NumberFormatException
Clarification: parseInt() method parses input into integer. The exception thrown by this method is NumberFormatException.
class exception_handling
{
public static void main(String args[])
{
try
{
System.out.print("Hello" + " " + 1 / 0);
}
finally
{
System.out.print("World");
}
}
}
b) World
c) Compilation Error
d) First Exception then World
Clarification: None.
Output:
$ javac exception_handling.java
$ java exception_handling
Exception in thread "main" java.lang.ArithmeticException: / by zero
World
class exception_handling
{
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");
}
}
}
b) 0
c) -10
d) -101
Clarification: 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