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

Exceptions One Mark 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)
2 views9 pages

Exceptions One Mark 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
You are on page 1/ 9

EXCEPTION ONE MARK QUESTIONS

1. What will be the output?

try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("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) {
System.out.println("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;
System.out.println(s.length());
} catch (NullPointerException e) {
System.out.println("Null error");
}
a) null
b) 0
c) Null error
d) Compilation Error

4. What is printed?
try {
System.out.println("Try");
} finally {
System.out.println("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 = Integer.parseInt("abc");
} catch (NumberFormatException e) {
System.out.println("Format issue");
}

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

7. Which block is always executed?


try {
int a = 10;
} finally {
System.out.println("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) {
System.out.println("Null");
} catch (ArithmeticException e) {
System.out.println("Math");
}

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

9. Is this valid?
catch (Exception e, ArithmeticException ae) {
System.out.println("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 = str.charAt(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) {
System.out.println(e.getMessage());
}

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;
System.out.println("Success");
} catch (Exception e) {
System.out.println("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 {
System.exit(0);
} finally {
System.out.println("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) {
System.out.println("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

You might also like