Java MCQs on keyword finally and built in exceptions of Java Programming Language.
1. Which of these clause will be executed even if no exceptions are found? Answer: b 2. A single try block must be followed by which of these? Answer: c 3. Which of these exceptions handles the divide by zero error? Answer: a 4. Which of these exceptions will occur if we try to access the index of an array beyond its length? Answer: d 5. What will be the output of the following Java code? Note : Execution command line : $ java exception_handling 6. What will be the output of the following Java code? a) A 7. What will be the output of the following Java code? a) A 8. What will be the output of the following Java code? Note: Execution command line: $ java exception_handling one two
a) throws
b) finally
c) throw
d) catch
Clarification: finally keyword is used to define a set of instructions that will be executed irrespective of the exception found or not.
a) finally
b) catch
c) finally & catch
d) none of the mentioned
Clarification: 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.
a) ArithmeticException
b) MathException
c) IllegalAccessException
d) IllegarException
Clarification: None.
a) ArithmeticException
b) ArrayException
c) ArrayIndexException
d) ArrayIndexOutOfBoundsException
Clarification: 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.
class exception_handling
{
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
Clarification: None.
Output:
$ javac exception_handling.java
$ java exception_handling
1
class exception_handling
{
public static void main(String args[])
{
try
{
throw new NullPointerException ("Hello");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
}
}
b) B
c) Compilation Error
d) Runtime Error
Clarification: Try block is throwing NullPointerException but the catch block is used to counter Arithmetic Exception. Hence NullPointerException occurs since no catch is there which can handle it, runtime error occurs.
Output:
$ javac exception_handling.java
$ java exception_handling
Exception in thread "main" java.lang.NullPointerException: Hello
class exception_handling
{
public static void main(String args[])
{
try
{
int a = 1;
int b = 10 / a;
try
{
if (a == 1)
a = a / a - a;
if (a == 2)
{
int c[] = {1};
c[8] = 9;
}
}
finally
{
System.out.print("A");
}
}
catch (Exception e)
{
System.out.println("B");
}
}
}
b) B
c) AB
d) BA
Clarification: The inner try block does not have a catch which can tackle ArrayIndexOutOfBoundException hence finally is executed which prints ‘A’ the outer try block does have catch for ArrayIndexOutOfBoundException exception but no such exception occurs in it hence its catch is never executed and only ‘A’ is printed.
Output:
$ javac exception_handling.java
$ java exception_handling
A
class exception_handling
{
public static void main(String args[])
{
try
{
int a = args.length;
int b = 10 / a;
System.out.print(a);
try
{
if (a == 1)
a = a / a - a;
if (a == 2)
{
int []c = {1};
c[8] = 9;
}
}
catch (ArrayIndexOutOfBoundException e)
{
System.out.println("TypeA");
}
catch (ArithmeticException e)
{
System.out.println("TypeB");
}
}
}
a) TypeA
b) TypeB
c) Compilation Error
d) Runtime Error
Clarification: try without catch or finally
Output:
$ javac exception_handling.java
$ java exception_handling
Main.java:9: error: 'try' without 'catch', 'finally' or resource declarations