Font size
WorksheetsJava Exception Handling Quiz
Total questions: 115
Worksheet time: 1hrs 1mins
Which of the following is the parent class of all exceptions in Java?
Error
Throwable
Exception
RuntimeException
Which keyword is used to manually throw an exception in Java?
try
throw
throws
finally
Which block is always executed whether an exception is handled or not?
try
catch
throw
finally
Which of the following is an unchecked exception?
IOException
SQLException
ArrayIndexOutOfBoundsException
FileNotFoundException
What happens if a try block does not have a catch block but has a finally block?
The code will not compile
The code will run, and finally block will always execute
The program will crash
An exception will be thrown at compile time
Which of the following is true about multiple catch blocks in Java?
Only one catch block is executed for a try block
All catch blocks are executed for a try block
No catch block is executed
Multiple catch blocks can't be used
What is the correct syntax to catch multiple exceptions in a single catch block in Java (since Java 7)?
catch (Exception1 | Exception2 e)
catch (Exception1, Exception2 e)
catch (Exception1; Exception2 e)
catch (Exception1 || Exception2 e)
Which of the following keywords indicates a method can throw an exception?
try
throw
throws
finally
Can a try block be used without a catch block?
Yes, but only with a throw block
Yes, but only with a finally block
No, it must have a catch block
Yes, without any restriction
Which exception is thrown when a thread is waiting, sleeping, or occupied, and the thread is interrupted?
InterruptedException
IllegalThreadStateException
ThreadDeath
ThreadStateException
What is an exception in Java?
A method
A class
An error during program execution
A data structure
Which of the following is a checked exception?
NullPointerException
IOException
ArithmeticException
ArrayIndexOutOfBoundsException
What is the superclass of all exceptions in Java?
Throwable
Exception
RuntimeException
Error
Which of the following exceptions is NOT checked?
SQLException
ClassNotFoundException
IOException
NumberFormatException
What happens if a finally block is present after a try-catch block?
It executes only if an exception occurs
It executes whether or not an exception occurs
It never executes
It depends on the catch block execution
Which block must follow a try block?
finally
catch
Both
None of the above
Which of the following is true about the finally block?
It is optional
It must be followed by a catch block
It executes only if there is an exception
It must be present
If an exception occurs in the try block, which block will handle it?
finally
catch
throw
None of the above
Can a try block exist without a catch block?
Yes, but it must have a finally block
No, catch is mandatory
Yes, try can exist independently
It depends on the exception type
If no exception occurs, will the finally block execute?
No
Yes
It depends on the type of exception
None of the above
Which of the following is used to declare an exception?
throw
throws
final
finally
What is the purpose of the throw keyword?
To throw an exception explicitly
To declare exceptions
To handle exceptions
To suppress exceptions
Which of these keywords is used for explicitly throwing an exception?
try
catch
throw
throws
Where can throws be used?
Inside the method
At the beginning of the method
In a class declaration
In a variable declaration
Which of the following exceptions cannot be thrown using the throw keyword?
IOException
RuntimeException
NullPointerException
Error
How do you create a custom exception in Java?
By extending Throwable
By extending RuntimeException
By extending Exception
By extending Error
Which of these is not a method of the Throwable class?
getMessage()
printStackTrace()
getCause()
getException()
Which of the following is true for custom exceptions?
They must extend Throwable
They must extend RuntimeException
They must extend Exception
They can extend either Exception or RuntimeException
Which of the following exceptions is part of the java.lang package?
SQLException
ArrayIndexOutOfBoundsException
IOException
EOFException
Which class is used for all standard error messages in Java?
Error
Exception
RuntimeException
Throwable
What is the output when an exception occurs but is not caught?
The program executes successfully
A runtime exception is printed in the console
The program continues from the next line
It depends on the exception
Which of the following is NOT a type of exception in Java?
ClassCastException
ArithmeticException
ErrorException
IOException
Which exception is thrown when an object is not found?
IOException
NoSuchElementException
ClassNotFoundException
FileNotFoundException
What is the result of calling printStackTrace() on an exception object?
The exception is rethrown
It prints the call stack to the standard error
It suppresses the exception
It returns a String representation of the stack
Which of the following exceptions is not thrown during the compilation phase?
NullPointerException
ClassNotFoundException
IOException
FileNotFoundException
What is multi-catch in exception handling?
Handling multiple exceptions in one catch block
Throwing multiple exceptions
Declaring multiple exceptions in a throws clause
A new feature introduced in Java 8
Which exception occurs when trying to access an invalid index in an array?
NullPointerException
ArrayIndexOutOfBoundsException
IndexOutOfBoundsException
IOException
Which statement is true about catching multiple exceptions in a single catch block?
The exceptions must be unrelated
The exceptions must inherit from the same class
The exceptions must be runtime exceptions
The exceptions must belong to the same package
Can an exception be rethrown in a catch block?
No
Yes, using the throw keyword
Yes, using the throws keyword
Only in the finally block
What is the order of catch blocks when handling exceptions in inheritance hierarchy?
Most specific exception to most general exception
Most general exception to most specific exception
Order does not matter
Only one catch block is allowed
What is the advantage of Exception Handling
To avoid abnormal termination of a program
To find out errors
To Debug program
None of these
What is the parent class of All Exceptions in JAVA
Throw
Exception
Error
Bug
Which of the following key word is optional in Exception handling program
try
catch
finally
throw
What is the purpose of 'throw' keyword
To Raise an exception explicityly
To Raise an exception implicityly
To create user defined exceptions
To create custom exceptions
Exception classes belongs to following package
import java.io.*
import java.lang.*
import java.util.*
import java.lang.Exception.*
How to create our own exception
using 'throw' keyword
using 'throws' keyword
using 'finally' keyword
using 'throwable' keyword
Choose an exception if attempt to divide number by zero
int number = 89 / 0;
System.out.println("The answer is " + number);
ArithmeticException
NullPointerException
NumberFormatException
ArrayIndexOutOfBoundException
What will it print?
1
2
3
30
1
2
30
1
2
3
11
1
2
11
What will this program print?
Nothing, there is an error
There was a ValueError, please try again
num1num2
num2num1
public class Foo
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}
Finally
Compilation fails.
The code runs with no output
An exception is thrown at runtime.
class Main {
public static void main(String args[]) {
try {
throw 10;
}
catch(int e) {
System.out.println("Got the Exception " + e);
}
}
}
Got the Exception 10
Got the Exception 0
Compiler Error
none
Choose the CORRECT mechanism of exception handling.
try block --> catch block --> finally block
try block --> catch block --> throws exception
try block --> catch block --> finally --> throws exception
try block --> throws exception--> catch block --> finally block
What is the output?
The code will lead to a compilation error as declaration of the display method has been provided in two interface.
The code will lead to a compilation error due to public modifier while declaring the display method.
The code will compile and execute successfully showing the output Welcome to Examveda.
The code will lead to a compilation error as the display method is not declared as abstract.
None of these
Exception is a class/interface/abstract class/other?
Class
Interface
Abstract class
Other
1. class main_class
2. {
3. public static void main(String args[])
4. {
5. int y = 10;
6. if (y == 10)
7. {
8. int y = 11;
9. System.out.println(y);
10. }
11. }
12. }
Compilation error
10
11
Run time error
What is the advantage of Exception Handling
To avoid abnormal termination of a program
To find out errors
To Debug program
None of these
What is the parent class of All Exceptions in JAVA
Throw
Exception
Error
Bug
Which of the following key word is optional in Exception handling program
try
catch
finally
throw
try block may or may not contains catch blocks
True
False
What is the purpose of 'throw' keyword
To Raise an exception explicityly
To Raise an exception implicityly
To create user defined exceptions
To create custom exceptions
What is an exception
Error occurred during the execution of a program
Bug occurred during the compile time of a program
Simply an Error
It is related to input values
How to create our own exception
using 'throw' keyword
using 'throws' keyword
using 'finally' keyword
using 'throwable' keyword
Exception classes belongs to following package
import java.io.*
import java.lang.*
import java.util.*
import java.lang.Exception.*
Which of these keywords is not a part of exception handling?
try
finally
thrown
catch
java.lang.NullPointerException is a
Error
runtime exception
Compile time exception
None
Which of these class is highest in hierarchy in java
java.lang.Exception
java.lang.Error
java.lang.Throwable
java.lang.Object
Choose the CORRECT exception
String department="JTMK";
Integer no=Integer.parseInt(department);
ArithmeticException
NullPointerException
NumberFormatException
ArrayIndexOutOfBoundsException
Choose an exception if attempt to divide number by zero
int number = 89 / 0;
System.out.println("The answer is " + number);
ArithmeticException
NullPointerException
NumberFormatException
ArrayIndexOutOfBoundException
What will it print?
1
2
3
30
1
2
30
1
2
3
11
1
2
11
Which line or lines should we get inside a try block to avoid errors?
3
4
6
2
What will this print?
There was an error
1
9
0
What will this program print?
Nothing, there is an error
There was a ValueError, please try again
num1num2
num2num1
public class Foo
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}
Finally
Compilation fails.
The code runs with no output
An exception is thrown at runtime.
What will be the output of the program?
try
{
int x = 0;
int y = 5 / x;
}
catch (Exception e)
{
System.out.println("Exception");
}
catch (ArithmeticException ae)
{
System.out.println(" Arithmetic Exception");
}
System.out.println("finished");
finished
runtime error.
Compilation fails
Exception.
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod()
{
throw new Error(); /* Line 22 */
}
}
ABCD
Compilation fails.
C is printed before exiting with an error message.
BC is printed before exiting with an error message
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (RuntimeException ex) /* Line 10 */
{
System.out.print("B");
}
catch (Exception ex1)
{
System.out.print("C");
}
finally
{
System.out.print("D");
}
System.out.print("E");
}
public static void badMethod()
{
throw new RuntimeException();
}
}
BD
BDE
BD
DE
FileNotFoundException
Is a subclass/extends IOException
Is a Compile time exception
Found in java.io package
All
class Main {
public static void main(String args[]) {
try {
throw 10;
}
catch(int e) {
System.out.println("Got the Exception " + e);
}
}
}
Got the Exception 10
Got the Exception 0
Compiler Error
none
class exception_handling
{ public static void main(String args[])
{ try
{ int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
} catch(ArithmeticException e)
{ System.out.print("B");
}
finally
{ System.out.print("C");
}
}
}
A
B
AC
BC
class IT
{
static public void main(String...args)
{
int k=Integer.parseInt(args[0]);
System.out.println(k);
}
}
if args[0]="gec" then what type of exception is thrown by the above program.
NumberFormatException
NullPointerException
ArrayIndexOutOfBoundsException
InvalidStringException
class IT
{
static public void main(String...args)
{
String s=null;
System.out.println("length is:"+s.length());
}
}
It throws NullPointerException
it throws IOException
It throws StringExeption
output is 0
Which of the following should be true of the object thrown by a thrown statement?
a) Should be assignable to String type
b) Should be assignable to Exception type
c) Should be assignable to Throwable type
d) Should be assignable to Error type
Which of the following keywords is used for throwing exception manually?
finally
try
throw
catch
A
B
Compilation Error
Runtime Error
Which of these keywords are used for the block to be examined for exceptions?
try
catch
throw
finally
Hello
World
HelloWOrld
Compilation Error
Hello
World
HelloWOrld
Compilation Error
Hello
World
HelloWOrld
WorldWorld
Which of these classes is super class of Exception class?
Throwable
System
RunTime
Class
5) What is the need for Autoboxing and Unboxing in Java?
A) Reduces code to be written by developers to convert from Wrapper to Primitive vice versa
B) Compile time is reduced due to fixed methods followed to automatically convert Objects and Primitives
C) Auto-unboxing improves memory efficiency by converting Wrapper to Primitive and process when object version is not needed
D) All the above
Answer [=]
public class ExceptionTest9
{
public static void main(String[] args)
{
System.out.println("I am going to forest.");
throw new ClassNotFoundException();
}
}
I am going to forest.
No output
Compiler error
None of the above
Which is the superclass of Error class in Java?
RuntimeException
Exception
Throwable
Object
public class ExceptionTest14
{
public static void main(String[] args)
{
try
{
int a=10/1;
try
{int b=20/1;}
catch(Exception e1)
{ System.out.println("b=20"); }
}
catch(Exception e2)
{System.out.println("a=10");}
}
}
a=10 b=20
b=20 a=10
Compiler error
No output
Choose the right statement which does autoboxing in Java?
Integer temperature1 = 100;
int temperature2 = 101;
Integer temperature1 = 100; int temperature2 = 101;
All the above
Choose the correct way of autoboxing Primitives to Wrapper objects below?
Float f1 = 1.0f;
Boolean bull = 0;
int temperature2 = 101;
All the above
public class Except
{
public static void main(String[] args)
{
try
{
throw new Error();
}
catch (Error e)
{
try
{
throw new RuntimeException();
}
catch (Throwable t)
{
}
}
System.out.println("phew");
}
}
Noting
phew
Compiler Error
Program Halt
1. Which of these keywords must be used to monitor for exceptions?
a) try
b) finally
c) throw
d) catch
2. Class at the top of exception class hierarchy?
ArithmeticException
Throwable
Object
Exception
3. In which of the following package, Exception class exist?
java.util
java.io
java.lang
java.file
4. Exception generated by try block is caught in ______ block?
catch
throw
throws
finally
5. Which exception is thrown when divide by zero occurs?
NumberFormatException
ArithmeticException
NullPointerException
None of these
What is an exception in Java?
An exception in Java is a feature that allows the program to run faster.
An exception in Java is an event that occurs during the execution of a program that disrupts the normal flow of instructions.
An exception in Java is a type of loop used for iteration.
An exception in Java is a method used to print output to the console.
What is the purpose of exception handling in Java?
The purpose of exception handling in Java is to ignore runtime errors and continue with the program execution.
The purpose of exception handling in Java is to display error messages to the user.
The purpose of exception handling in Java is to increase the performance of the program.
The purpose of exception handling in Java is to handle and manage runtime errors or exceptional conditions that may occur during the execution of a program.
What is the difference between checked and unchecked exceptions in Java?
Checked exceptions are not checked at compile-time, while unchecked exceptions are checked at compile-time.
Checked exceptions are checked at compile-time, while unchecked exceptions are not checked at compile-time.
Checked exceptions are checked at runtime, while unchecked exceptions are checked at compile-time.
Checked exceptions are checked at compile-time, while unchecked exceptions are checked at runtime.
What are the keywords used for exception handling in Java?
try, throw, finally
try, throw, catch
throw, catch, finally
try, catch, finally
What is the use of try-catch block in Java?
Memory management
Exception handling
Loop control
Input/output operations
What is the use of finally block in Java exception handling?
The finally block is used to define a block of code that will be executed regardless of whether an exception is thrown or not.
The finally block is used to handle exceptions in Java.
The finally block is used to terminate the program in case of an exception.
The finally block is used to catch and handle exceptions in Java.
What is the difference between throw and throws in Java?
The 'throw' keyword is used to explicitly throw an exception, while 'throws' is used in the method signature to declare that the method may throw one or more exceptions.
The 'throw' keyword is used to catch exceptions, while 'throws' is used to throw exceptions.
The 'throw' keyword is used to declare exceptions, while 'throws' is used to handle exceptions.
The 'throw' keyword is used to handle exceptions, while 'throws' is used to declare exceptions.
What is the difference between final, finally, and finalize in Java?
'finalize' is a block used in exception handling to specify cleanup operations
'finally' is a keyword used to specify code that will be executed only if an exception is thrown
'final' is used to indicate that a variable can be changed
The difference between final, finally, and finalize in Java is that 'final' is a keyword used for variables, methods, and classes to indicate that they cannot be changed, overridden, or subclassed respectively. 'finally' is a block used in exception handling to specify code that will be executed regardless of whether an exception is thrown or not. 'finalize' is a method called by the garbage collector before an object is destroyed to perform cleanup operations.
What is the purpose of the throws keyword in Java?
The throws keyword in Java is used to handle exceptions within a try-catch block.
The throws keyword in Java is used to specify the return type of a method.
The throws keyword in Java is used to define a custom exception class.
The purpose of the throws keyword in Java is to declare that a method may throw one or more specific exceptions.
What is the difference between Error and Exception in Java?
Errors are minor issues that can be recovered from, while exceptions are serious issues that cannot be recovered from.
Errors and exceptions are the same thing.
Errors occur during compile-time, while exceptions occur during runtime.
Errors are serious issues that cannot be recovered from, while exceptions can be handled and recovered from.
What is the purpose of the finally block in Java exception handling?
The finally block is used to define a block of code that will be executed regardless of whether an exception is thrown or not.
The finally block is used to handle exceptions in Java.
The finally block is used to terminate the program in case of an exception.
The finally block is used to catch and handle exceptions in Java.
What is the use of try-catch block in Java?
Memory management
Exception handling
Loop control
Input/output operations
What is the purpose of the finally block in Java exception handling?
The finally block is used to define a block of code that will be executed regardless of whether an exception is thrown or not.
The finally block is used to handle exceptions in Java.
The finally block is used to terminate the program in case of an exception.
The finally block is used to catch and handle exceptions in Java.
What is the difference between checked and unchecked exceptions in Java?
Checked exceptions are not checked at compile-time, while unchecked exceptions are checked at compile-time.
Checked exceptions are checked at compile-time, while unchecked exceptions are not checked at compile-time.
Checked exceptions are checked at runtime, while unchecked exceptions are checked at compile-time.
Checked exceptions are checked at compile-time, while unchecked exceptions are checked at runtime.
