WorksheetsJava Exceptions
Total questions: 10
Worksheet time: 10mins
Consider the code in the image. What will happen when the program is executed?
Program will crash.
Program will catch an exception in main.
Program will catch an exception in divide.
Program will run normally.
Consider the code in the image. What happens if a user inputs 2?
An InputMismatchException is caught in main().
An IllegalArgumentException is caught in main().
The program crashes.
The program calculates and displays asin(2).
Consider the code in the image. What happens if a user inputs the String "sqrt(2)"?
An InputMismatchException is caught in main().
An IllegalArgumentException is caught in main().
The program crashes.
The program calculates and displays asin(2).
Consider the code in the image. What happens when this program is executed?
An Exception is caught in method1.
An Exception is caught in method2.
An Exception is caught in method3.
The program crashes.
Which of these best explains how try-catch blocks differ from throw statements?
try-catch blocks are handled locally, but throw statements can propagate through the call stack
try-catch block only catch exceptions, and throw statements only throw exceptions
try-catch blocks only try to execute code, but throw statements always work
try-catch blocks are written inside a function, but throw statements are written in the function header
Which of these is a checked exception?
OutOfMemoryError
StackOverflowError
ArithmeticException
IOException
Which of these is caused by integer division by zero?
ArithmeticException
ArrayIndexOutOfBoundsException
MathException
DivisionByZeroException
If String s = "abc", what is the value of s.charAt(2)?
a
b
c
This command will throw a StringIndexOutOfBoundsException.
If String s = "abc", what is the value of s.charAt(3)?
a
b
c
This command will throw a StringIndexOutOfBoundsException.
If sc = new Scanner(System.in) and x = sc.nextInt(), which of these is thrown if the user inputs the String "x"?
NullPointerException
StringIndexOutOfBoundsException
InputMismatchException
IllegalArgumentException
