Finally & Built in Exceptions

advertisement
Finally & Built in Exceptions
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
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 packages contain all the Java’s built in exceptions?
a) java.io
b) java.util
c) java.lang
d) java.net
View Answer
Answer: c
Explanation: None.
4. Which of these exceptions handles the divide by zero error?
a) ArithmeticException
b) MathException
c) IllegalAccessException
d) IllegarException
View Answer
Answer: a
Explanation: None.
5. 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.
6. What is the output of this program?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
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
Note : Execution command line : $ java exception_handling
View Answer
Answer: b
Explanation: None.
Output:
$ javac exception_handling.java
$ java exception_handling
1
7. What is the output of this program?
1.
2.
3.
4.
5.
6.
7.
8.
9.
class exception_handling {
public static void main(String args[]) {
try {
throw new NullPointerException ("Hello");
System.out.print("A");
}
catch(ArithmeticException e) {
System.out.print("B");
}
10.
11.
}
}
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:
$ javac exception_handling.java
$ java exception_handling
Exception in thread “main” java.lang.NullPointerException: Hello
8. What is the output of this program?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
class exception_handling {
static void throwexception() throws ArithmeticException {
System.out.print("0");
throw new ArithmeticException ("Exception");
}
public static void main(String args[]) {
try {
throwexception();
}
catch (ArithmeticException e) {
System.out.println("A");
}
}
}
a) A
b) 0
c) 0A
d) Exception
View Answer
Answer: c
Explanation: None.
Output:
$ javac exception_handling.java
$ java exception_handling
0A
9. What is the output of this program?
1. class exception_handling
2.
{
3.
public static void main(String args[])
4.
{
5.
try
6.
{
7.
int a = 1;
8.
int b = 10 / a;
9.
try
10.
{
11.
if (a == 1)
12.
a = a / a - a;
13.
if (a == 2)
14.
{
15.
int c[] = {1};
16.
c[8] = 9;
17.
}
18.
}
19.
finally
20.
{
21.
System.out.print("A");
22.
}
23.
}
24.
catch (Exception e)
25.
{
26.
System.out.println("B");
27.
}
28.
}
29.
}
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:
$ javac exception_handling.java
$ java exception_handling
A
10. What is the output of this program?
1.
2.
3.
4.
class exception_handling {
public static void main(String args[]) {
try {
int a = args.length;
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
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
Note: Execution command line: $ java exception_handling one two
View Answer
Answer: c
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
Download