0% found this document useful (0 votes)
13 views9 pages

Java Exception Handling Quiz Questions

The document contains a series of multiple-choice questions related to exception handling in Java. Each question tests knowledge on specific scenarios involving try-catch blocks, exception types, and the behavior of the Java runtime. The questions cover various aspects of exceptions, including checked and unchecked exceptions, output of code snippets, and compilation validity.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views9 pages

Java Exception Handling Quiz Questions

The document contains a series of multiple-choice questions related to exception handling in Java. Each question tests knowledge on specific scenarios involving try-catch blocks, exception types, and the behavior of the Java runtime. The questions cover various aspects of exceptions, including checked and unchecked exceptions, output of code snippets, and compilation validity.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXCEPTION ONE MARK QUESTIONS

1. What will be the output?

try {
int a = 5 / 0;
} catch (ArithmeticException e) {
[Link]("Error");
}

a) 0
b)Compilation Error
c) Error
d)Runtime Error

2. What will be the output?

try {
int[] a = new int[5];
a[10] = 50;
} catch (Exception e) {
[Link]("Caught");
}

a) Compilation Error
b) ArrayIndexOutOfBoundsException
c) Runtime Error
d) Caught

Prepared by MUKESH
-2-
3. What will be the output?

try {
String s = null;
[Link]([Link]());
} catch (NullPointerException e) {
[Link]("Null error");
}
a) null
b) 0
c) Null error
d) Compilation Error

4. What is printed?
try {
[Link]("Try");
} finally {
[Link]("Finally");
}

a) Try Finally
b) Finally
c) Try
d) Compilation Error.

5. Will this compile?


throw new ArithmeticException("Divide by zero");

a) Yes
b) No
c) Only in try block
d) Only with throws clause

Prepared by MUKESH
-3-
6. Will this compile?
try {
int x = [Link]("abc");
} catch (NumberFormatException e) {
[Link]("Format issue");
}

a) Compilation Error
b) abc
c) Runtime Error
d) Format issue

7. Which block is always executed?


try {
int a = 10;
} finally {
[Link]("Done");
}

a) try
b) catch
c) finally
d) none

Prepared by MUKESH
-4-
8. Which block is always executed?
try {
int a = 5 / 0;
} catch (NullPointerException e) {
[Link]("Null");
} catch (ArithmeticException e) {
[Link]("Math");
}

a) Null
b) Math
c) Compilation Error
d) 0

9. Is this valid?
catch (Exception e, ArithmeticException ae) {
[Link]("Invalid");
}

a) Yes
b) Only with Java 8
c) No
d) Only with throws.

10. What exception is thrown here?


String str = "abc";
char ch = [Link](10);

a) NullPointerException
b) StringIndexOutOfBoundsException
c) RuntimeException
d) No Exception

Prepared by MUKESH
-5-
11. Which is a checked exception?

a) ArithmeticException
b) ArrayIndexOutOfBoundsException
c) IOException
d) NullPointerException

12. Can catch come without try?

a) Yes
b) No
c) Only if finally exists
d) With static methods

13. What will be the output?


try {
int a = 5 / 0;
} catch (Exception e) {
[Link]([Link]());
}

a) null
b) / by zero
c) Error
d) Compilation Error

Prepared by MUKESH
-6-
14. Is this valid?

try {
} finally {
}

a) Yes
b) No
c) Only with catch
d) Only with throws

15. Which exception is thrown by sleep()?

a) TimeoutException
b) IOException
c) ArithmeticException
d) InterruptedException

16. What will be the output?


try {
int a = 10 / 2;
[Link]("Success");
} catch (Exception e) {
[Link]("Failed");
}

a) Failed
b) Success
c) 5
d) Compilation Error

Prepared by MUKESH
-7-

17. Multiple catch block order?

a) Most specific to most general


b) Any order
c) General to specific
d) Only one allowed

18. Which is not throwable?

a) Exception
b) Error
c) Throwable
d) String

19. Can you throw a checked exception without catch/throws?

a) Yes
b) No
c) Only for IOException
d) Only for FileNotFound

Prepared by MUKESH
-8-
20. What will be the output?

public static int test() {


try {
return 1;
} finally {
return 2;
}
}

a) 1
b) 2
c) Compilation Error
d) Runtime Exception

21. Custom exception at least extend?

a) Throwable
b) Object
c) Exception
d) Runtime

22. What will be the output?


try {
[Link](0);
} finally {
[Link]("Finally");
}

a) Finally
b) Exit
c) Nothing
d) Error

Prepared by MUKESH
-9-
23. What is required with throws?

a) Method body
b) Catch block
c) Static method
d) Declared exception

24. What will be the output?

try {
throw new RuntimeException("Error");
} catch (Exception e) {
[Link]("Handled");
}

a) Error
b) RuntimeException
c) Handled
d) Nothing

25. Which is parent of all exceptions?

a) Throwable
b) Error
c) Exception
d) Object

Prepared by MUKESH

Common questions

Powered by AI

When an array in Java is accessed with an index that is out of its bounds, an ArrayIndexOutOfBoundsException is thrown. This can be caught and handled within a try-catch block, as shown in Source 1 where accessing an index of 10 in an array of size 5 is managed in a catch block, printing 'Caught' .

When handling multiple exceptions in a try-catch block in Java, they should be caught from the most specific to the most general. This avoids unreachable code and ensures that the specific exceptions are handled first .

A checked exception cannot be thrown without being caught or declared in the throws clause of a method. Java enforces exception handling during compilation for checked exceptions to ensure that specific recovery actions are taken, unlike unchecked exceptions .

Using a finally block to suppress exceptions could lead to loss of critical exception information, misleading program outcomes, or unexpected behaviors because exceptions raised in try and catch blocks can be overridden by the finally block. This can complicate debugging and cause resource leaks if resources are incorrectly handled .

If System.exit(0) is invoked within a try block, it will terminate the program immediately, and any code in the finally block will not execute—illustrated by the scenario where nothing is printed after calling System.exit(0).

Attempting integer division by zero will throw an ArithmeticException because division by zero is undefined. If not explicitly caught, it will terminate the program unless the try block manages it with a catch, as demonstrated where 'Error' is printed when the ArithmeticException is caught .

A 'throw' statement is used to explicitly throw an exception; however, it must be used in a context where the exception can be declared or handled. In checked exceptions, the context should also include the corresponding throws declaration in the method signature unless the exception is caught within the method—ensuring error transparency and error handling mechanisms are in place .

The 'finally' block in Java will always execute after the try block, whether an exception is thrown or not, making it ideal for cleanup operations. For example, System.out.println("Done") in a finally block will execute even if an exception is not thrown in the try block as described in Q7 .

When using 'return' statements in both a try and a finally block, the 'return' in the finally block will override the one in the try block. This is exemplified in a method returning 2, not 1, when defined in a finally block after a try indicating a return of 1 .

Catching multiple exceptions in a single catch block separated by a comma is invalid because Java does not allow multi-variable declaration in a single catch block. Instead, from Java 7 onwards, multiple exceptions can be caught using a single catch block by using the pipe (|) operator .

You might also like