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
Answer: b
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
Answer: 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.
3. Which of these exceptions handles the divide by zero error?
a) ArithmeticException
b) MathException
c) IllegalAccessException
d) IllegarException
View Answer
Answer: a
Explanation: None.
Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
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
Answer: 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.
5. What will be the output of the following Java code?
Take Java Programming Mock Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- class exception_handling
- {
- public static void main(String args[])
- {
- try
- {
- int a = args.length;
- int b = 10 / a;
- System.out.print(a);
- }
- {0
- {
- {2
- }
- {4
- {5
Note : Execution command line : $ java exception_handling
a) 0
b) 1
c) Compilation Error
d) Runtime Error
View Answer
Answer: b
Explanation: None.
Output:
advertisement
{6
6. What will be the output of the following Java code?
advertisement
- class exception_handling
- {
- public static void main(String args[])
- {
- try
- {
- public static void main(String args[]) 3
- }
- public static void main(String args[]) 5
- {
- public static void main(String args[]) 7
- }
- {4
- {5
a) A
b) B
c) Compilation Error
d) Runtime Error
View Answer
Answer: d
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:
{1
7. What will be the output of the following Java code?
- {2
- {
- {4
- {
- {6
- {7
- {8
- {9
- try 0
- try 1
- try 2
- try 3
- try 4
- try 5
- try 6
- try 7
- try 8
- try 9
- {0
- try 1
- {2
- try 9
- {4
- {5
- {7
- {7
- {4
- }
- {4
a) A
b) B
c) AB
d) BA
View Answer
Answer: a
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:
int a = args.length;1
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);
- {6
- {7
- int b = 10 / a;3
- int b = 10 / a;4
- int b = 10 / a;5
- int b = 10 / a;6
- int b = 10 / a;7
- int b = 10 / a;8
- int b = 10 / a;9
- {4
- System.out.print(a);1
- {7
- System.out.print(a);3
- {4
- {0
- {
- System.out.print(a);7
- }
- {4
- {5
Note: Execution command line: $ java exception_handling one two
a) TypeA
b) TypeB
c) Compilation Error
d) Runtime Error
View Answer
Answer: c
Explanation: try without catch or finally
Output:
}1
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.