wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Exceptions

Total questions: 10

Worksheet time: 10mins

Name
Class
Date
1.

Consider the code in the image. What will happen when the program is executed?

a)

Program will crash.

b)

Program will catch an exception in main.

c)

Program will catch an exception in divide.

d)

Program will run normally.

2.

Consider the code in the image. What happens if a user inputs 2?

a)

An InputMismatchException is caught in main().

b)

An IllegalArgumentException is caught in main().

c)

The program crashes.

d)

The program calculates and displays asin(2).

3.

Consider the code in the image. What happens if a user inputs the String "sqrt(2)"?

a)

An InputMismatchException is caught in main().

b)

An IllegalArgumentException is caught in main().

c)

The program crashes.

d)

The program calculates and displays asin(2).

4.

Consider the code in the image. What happens when this program is executed?

a)

An Exception is caught in method1.

b)

An Exception is caught in method2.

c)

An Exception is caught in method3.

d)

The program crashes.

5.

Which of these best explains how try-catch blocks differ from throw statements?

a)

try-catch blocks are handled locally, but throw statements can propagate through the call stack

b)

try-catch block only catch exceptions, and throw statements only throw exceptions

c)

try-catch blocks only try to execute code, but throw statements always work

d)

try-catch blocks are written inside a function, but throw statements are written in the function header

6.

Which of these is a checked exception?

a)

OutOfMemoryError

b)

StackOverflowError

c)

ArithmeticException

d)

IOException

7.

Which of these is caused by integer division by zero?

a)

ArithmeticException

b)

ArrayIndexOutOfBoundsException

c)

MathException

d)

DivisionByZeroException

8.

If String s = "abc", what is the value of s.charAt(2)?

a)

a

b)

b

c)

c

d)

This command will throw a StringIndexOutOfBoundsException.

9.

If String s = "abc", what is the value of s.charAt(3)?

a)

a

b)

b

c)

c

d)

This command will throw a StringIndexOutOfBoundsException.

10.

If sc = new Scanner(System.in) and x = sc.nextInt(), which of these is thrown if the user inputs the String "x"?

a)

NullPointerException

b)

StringIndexOutOfBoundsException

c)

InputMismatchException

d)

IllegalArgumentException