This section of our 1000+ Java MCQs focuses 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?
a) throws
b) finally
c) throw
d) catch
View Answer
Explanation: finally keyword is used to define a set of instructions that will be executed irrespective of the exception found or not.
2. 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
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.
3. Which of these exceptions handles the divide by zero error?
a) ArithmeticException
b) MathException
c) IllegalAccessException
d) IllegarException
View Answer
Explanation: None.
4. 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
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.
5. What will be the output of the following Java code?
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");
}
}
}
Note : Execution command line : $ java exception_handling
a) 0
b) 1
c) Compilation Error
d) Runtime Error
View Answer
Explanation: None.
Output:
$ javac exception_handling.java $ java exception_handling 1
6. What will be the output of the following Java code?
class exception_handling
{
public static void main(String args[])
{
try
{
throw new NullPointerException ("Hello");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
}
}
a) A
b) B
c) Compilation Error
d) Runtime Error
View Answer
Explanation: 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
7. What will be the output of the following Java code?
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");
}
}
}
a) A
b) B
c) AB
d) BA
View Answer
Explanation: 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
8. What will be the output of the following Java code?
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");
}
}
}
Note: Execution command line: $ java exception_handling one two
a) TypeA
b) TypeB
c) Compilation Error
d) Runtime Error
View Answer
Explanation: try without catch or finally
Output:
$ javac exception_handling.java $ java exception_handling Main.java:9: error: 'try' without 'catch', 'finally' or resource declarations
Sanfoundry Global Education & Learning Series – Java Programming Language.
- Apply for Computer Science Internship
- Practice BCA MCQs
- Check Programming Books
- Practice Programming MCQs
- Apply for Java Internship