Exception Handling MCQ Questions
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