Sie sind auf Seite 1von 5

ava Questions & Answers Exceptions Types

This section of our 1000+ Java MCQs focuses on Exceptions types in Java Programming
Language.

1. Which of these is a super class of all exceptional type classes?


a) String
b) RuntimeExceptions
c) Throwable
d) Cachable
View Answer

Answer: c
Explanation: All the exception types are subclasses of the built in class Throwable.

2. Which of these class is related to all the exceptions that can be caught by using catch?
a) Error
b) Exception
c) RuntimeExecption
d) All of the mentioned
View Answer

Answer: b
Explanation: Error class is related to java run time error that cant be caught usually,
RuntimeExecption is subclass of Exception class which contains all the exceptions that can be
caught.

3. Which of these class is related to all the exceptions that cannot be caught?
a) Error
b) Exception
c) RuntimeExecption
d) All of the mentioned
View Answer

Answer: a
Explanation: Error class is related to java run time error that cant be caught usually,
RuntimeExecption is subclass of Exception class which contains all the exceptions that can be
caught.

4. Which of these handles the exception when no catch is used?


a) Default handler
b) finally
c) throw handler
d) Java run time system
View Answer

Answer: a
Explanation: None.

5. Which of these keywords is used to manually throw an exception?


a) try
b) finally
c) throw
d) catch
View Answer

Answer: c
Explanation: None.

6. What is the output of this program?

1. class exception_handling {
2. public static void main(String args[]) {
3. try {
4. System.out.print("Hello" + " " + 1 / 0);
5. }
6. finally {
7. System.out.print("World");
8. }
9. }
10. }
advertisements
a) Hello
b) World
c) Compilation Error
d) First Exception then World
View Answer
Answer: d
Explanation: None.
Output:
$ javac exception_handling.java
$ java exception_handling
Exception in thread main java.lang.ArithmeticException: / by zero
World

7. What is the output of this program?

1. class exception_handling {
2. public static void main(String args[]) {
3. try {
4. int a, b;
5. b = 0;
6. a = 5 / b;
7. System.out.print("A");
8. }
9. catch(ArithmeticException e) {
10. System.out.print("B");
11. }
12. }
13. }

a) A
b) B
c) Compilation Error
d) Runtime Error
View Answer

Answer: b
Explanation: None.
Output:
$ javac exception_handling.java
$ java exception_handling
B

8. What is the output of this program?

1. class exception_handling {
2. public static void main(String args[]) {
3. try {
4. int a[] = {1, 2,3 , 4, 5};
5. for (int i = 0; i < 7; ++i)
6. System.out.print(a[i]);
7. }
8. catch(ArrayIndexOutOfBoundsException e) {
9. System.out.print("0");
10. }
11. }
12. }

a) 12345
b) 123450
c) 1234500
d) Compilation Error
View Answer

Answer: b
Explanation: When array index goes out of bound then ArrayIndexOutOfBoundsException
exceptio is thrown by the system.
Output:
$ javac exception_handling.java
$ java exception_handling
123450
advertisements
9. What is the output of this program?
1. class exception_handling {
2. public static void main(String args[]) {
3. try {
4. int i, sum;
5. sum = 10;
6. for (i = -1; i < 3 ;++i)
7. sum = (sum / i);
8. }
9. catch(ArithmeticException e) {
10. System.out.print("0");
11. }
12. System.out.print(sum);
13. }
14. }

a) 0
b) 05
c) Compilation Error
d) Runtime Error
View Answer

Answer: c
Explanation: Since sum is declared inside try block and we are trying to access it outside the try
block it results in error.
Output:
$ javac exception_handling.java
Exception in thread main java.lang.Error: Unresolved compilation problem:
sum cannot be resolved to a variable

10. What is the output of this program?

1. class exception_handling {
2. public static void main(String args[]) {
3. try {
4. int a[] = {1, 2,3 , 4, 5};
5. for (int i = 0; i < 5; ++i)
6. System.out.print(a[i]);
7. int x = 1/0;
8. }
9. catch(ArrayIndexOutOfBoundsException e) {
10. System.out.print("A");
11. }
12. catch(ArithmeticException e) {
13. System.out.print("B");
14. }
15. }
16. }
a) 12345
b) 12345A
c) 12345B
d) Comiplation Error
View Answer

Answer: c
Explanation: There can be more than one catch for a single try block. Here Arithmetic
exception(/ by 0) occurs instead of Array index out of bound exception, so 2nd catch block is
executed.
Output:
$ javac exception_handling.java
$ java exception_handling
1234

Das könnte Ihnen auch gefallen