wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Exception Handling Quiz

Total questions: 115

Worksheet time: 1hrs 1mins

Name
Class
Date
1.

Which of the following is the parent class of all exceptions in Java?

a)

Error

b)

Throwable

c)

Exception

d)

RuntimeException

2.

Which keyword is used to manually throw an exception in Java?

a)

try

b)

throw

c)

throws

d)

finally

3.

Which block is always executed whether an exception is handled or not?

a)

try

b)

catch

c)

throw

d)

finally

4.

Which of the following is an unchecked exception?

a)

IOException

b)

SQLException

c)

ArrayIndexOutOfBoundsException

d)

FileNotFoundException

5.

What happens if a try block does not have a catch block but has a finally block?

a)

The code will not compile

b)

The code will run, and finally block will always execute

c)

The program will crash

d)

An exception will be thrown at compile time

6.

Which of the following is true about multiple catch blocks in Java?

a)

Only one catch block is executed for a try block

b)

All catch blocks are executed for a try block

c)

No catch block is executed

d)

Multiple catch blocks can't be used

7.

What is the correct syntax to catch multiple exceptions in a single catch block in Java (since Java 7)?

a)

catch (Exception1 | Exception2 e)

b)

catch (Exception1, Exception2 e)

c)

catch (Exception1; Exception2 e)

d)

catch (Exception1 || Exception2 e)

8.

Which of the following keywords indicates a method can throw an exception?

a)

try

b)

throw

c)

throws

d)

finally

9.

Can a try block be used without a catch block?

a)

Yes, but only with a throw block

b)

Yes, but only with a finally block

c)

No, it must have a catch block

d)

Yes, without any restriction

10.

Which exception is thrown when a thread is waiting, sleeping, or occupied, and the thread is interrupted?

a)

InterruptedException

b)

IllegalThreadStateException

c)

ThreadDeath

d)

ThreadStateException

11.

What is an exception in Java?

a)

A method

b)

A class

c)

An error during program execution

d)

A data structure

12.

Which of the following is a checked exception?

a)

NullPointerException

b)

IOException

c)

ArithmeticException

d)

ArrayIndexOutOfBoundsException

13.

What is the superclass of all exceptions in Java?

a)

Throwable

b)

Exception

c)

RuntimeException

d)

Error

14.

Which of the following exceptions is NOT checked?

a)

SQLException

b)

ClassNotFoundException

c)

IOException

d)

NumberFormatException

15.

What happens if a finally block is present after a try-catch block?

a)

It executes only if an exception occurs

b)

It executes whether or not an exception occurs

c)

It never executes

d)

It depends on the catch block execution

16.

Which block must follow a try block?

a)

finally

b)

catch

c)

Both

d)

None of the above

17.

Which of the following is true about the finally block?

a)

It is optional

b)

It must be followed by a catch block

c)

It executes only if there is an exception

d)

It must be present

18.

If an exception occurs in the try block, which block will handle it?

a)

finally

b)

catch

c)

throw

d)

None of the above

19.

Can a try block exist without a catch block?

a)

Yes, but it must have a finally block

b)

No, catch is mandatory

c)

Yes, try can exist independently

d)

It depends on the exception type

20.

If no exception occurs, will the finally block execute?

a)

No

b)

Yes

c)

It depends on the type of exception

d)

None of the above

21.

Which of the following is used to declare an exception?

a)

throw

b)

throws

c)

final

d)

finally

22.

What is the purpose of the throw keyword?

a)

To throw an exception explicitly

b)

To declare exceptions

c)

To handle exceptions

d)

To suppress exceptions

23.

Which of these keywords is used for explicitly throwing an exception?

a)

try

b)

catch

c)

throw

d)

throws

24.

Where can throws be used?

a)

Inside the method

b)

At the beginning of the method

c)

In a class declaration

d)

In a variable declaration

25.

Which of the following exceptions cannot be thrown using the throw keyword?

a)

IOException

b)

RuntimeException

c)

NullPointerException

d)

Error

26.

How do you create a custom exception in Java?

a)

By extending Throwable

b)

By extending RuntimeException

c)

By extending Exception

d)

By extending Error

27.

Which of these is not a method of the Throwable class?

a)

getMessage()

b)

printStackTrace()

c)

getCause()

d)

getException()

28.

Which of the following is true for custom exceptions?

a)

They must extend Throwable

b)

They must extend RuntimeException

c)

They must extend Exception

d)

They can extend either Exception or RuntimeException

29.

Which of the following exceptions is part of the java.lang package?

a)

SQLException

b)

ArrayIndexOutOfBoundsException

c)

IOException

d)

EOFException

30.

Which class is used for all standard error messages in Java?

a)

Error

b)

Exception

c)

RuntimeException

d)

Throwable

31.

What is the output when an exception occurs but is not caught?

a)

The program executes successfully

b)

A runtime exception is printed in the console

c)

The program continues from the next line

d)

It depends on the exception

32.

Which of the following is NOT a type of exception in Java?

a)

ClassCastException

b)

ArithmeticException

c)

ErrorException

d)

IOException

33.

Which exception is thrown when an object is not found?

a)

IOException

b)

NoSuchElementException

c)

ClassNotFoundException

d)

FileNotFoundException

34.

What is the result of calling printStackTrace() on an exception object?

a)

The exception is rethrown

b)

It prints the call stack to the standard error

c)

It suppresses the exception

d)

It returns a String representation of the stack

35.

Which of the following exceptions is not thrown during the compilation phase?

a)

NullPointerException

b)

ClassNotFoundException

c)

IOException

d)

FileNotFoundException

36.

What is multi-catch in exception handling?

a)

Handling multiple exceptions in one catch block

b)

Throwing multiple exceptions

c)

Declaring multiple exceptions in a throws clause

d)

A new feature introduced in Java 8

37.

Which exception occurs when trying to access an invalid index in an array?

a)

NullPointerException

b)

ArrayIndexOutOfBoundsException

c)

IndexOutOfBoundsException

d)

IOException

38.

Which statement is true about catching multiple exceptions in a single catch block?

a)

The exceptions must be unrelated

b)

The exceptions must inherit from the same class

c)

The exceptions must be runtime exceptions

d)

The exceptions must belong to the same package

39.

Can an exception be rethrown in a catch block?

a)

No

b)

Yes, using the throw keyword

c)

Yes, using the throws keyword

d)

Only in the finally block

40.

What is the order of catch blocks when handling exceptions in inheritance hierarchy?

a)

Most specific exception to most general exception

b)

Most general exception to most specific exception

c)

Order does not matter

d)

Only one catch block is allowed

41.

What is the advantage of Exception Handling

a)

To avoid abnormal termination of a program

b)

To find out errors

c)

To Debug program

d)

None of these

42.

What is the parent class of All Exceptions in JAVA

a)

Throw

b)

Exception

c)

Error

d)

Bug

43.

Which of the following key word is optional in Exception handling program

a)

try

b)

catch

c)

finally

d)

throw

44.

What is the purpose of 'throw' keyword

a)

To Raise an exception explicityly

b)

To Raise an exception implicityly

c)

To create user defined exceptions

d)

To create custom exceptions

45.

Exception classes belongs to following package

a)

import java.io.*

b)

import java.lang.*

c)

import java.util.*

d)

import java.lang.Exception.*

46.

How to create our own exception

a)

using 'throw' keyword

b)

using 'throws' keyword

c)

using 'finally' keyword

d)

using 'throwable' keyword

47.

Choose an exception if attempt to divide number by zero


int number = 89 / 0;

System.out.println("The answer is " + number);

a)

ArithmeticException

b)

NullPointerException

c)

NumberFormatException

d)

ArrayIndexOutOfBoundException

48.

What will it print?

a)

1

2

3

30

b)

1

2

30

c)

1

2

3

11

d)

1

2

11

49.

What will this program print?

a)

Nothing, there is an error

b)

There was a ValueError, please try again

c)

num1num2num1^{num2}

d)

num2num1num2^{num1}

50.

public class Foo

{

public static void main(String[] args)

{

try

{

return;

}

finally

{

System.out.println( "Finally" );

}

}

}

a)

Finally

b)

Compilation fails.

c)

The code runs with no output

d)

An exception is thrown at runtime.

51.

class Main {

public static void main(String args[]) {

try {

throw 10;

}

catch(int e) {

System.out.println("Got the Exception " + e);

}

}

}

a)

Got the Exception 10

b)

Got the Exception 0

c)

Compiler Error

d)

none

52.

Choose the CORRECT mechanism of exception handling.

a)

try block --> catch block --> finally block

b)

try block --> catch block --> throws exception

c)

try block --> catch block --> finally --> throws exception

d)

try block --> throws exception--> catch block --> finally block

53.

What is the output?

a)

The code will lead to a compilation error as declaration of the display method has been provided in two interface.

b)

The code will lead to a compilation error due to public modifier while declaring the display method.

c)

The code will compile and execute successfully showing the output Welcome to Examveda.

d)

The code will lead to a compilation error as the display method is not declared as abstract.

e)

None of these

54.

Exception is a class/interface/abstract class/other?

a)

Class

b)

Interface

c)

Abstract class

d)

Other

55.

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. }

a)

Compilation error

b)

10

c)

11

d)

Run time error

56.

What is the advantage of Exception Handling

a)

To avoid abnormal termination of a program

b)

To find out errors

c)

To Debug program

d)

None of these

57.

What is the parent class of All Exceptions in JAVA

a)

Throw

b)

Exception

c)

Error

d)

Bug

58.

Which of the following key word is optional in Exception handling program

a)

try

b)

catch

c)

finally

d)

throw

59.

try block may or may not contains catch blocks

a)

True

b)

False

60.

What is the purpose of 'throw' keyword

a)

To Raise an exception explicityly

b)

To Raise an exception implicityly

c)

To create user defined exceptions

d)

To create custom exceptions

61.

What is an exception

a)

Error occurred during the execution of a program

b)

Bug occurred during the compile time of a program

c)

Simply an Error

d)

It is related to input values

62.

How to create our own exception

a)

using 'throw' keyword

b)

using 'throws' keyword

c)

using 'finally' keyword

d)

using 'throwable' keyword

63.

Exception classes belongs to following package

a)

import java.io.*

b)

import java.lang.*

c)

import java.util.*

d)

import java.lang.Exception.*

64.

Which of these keywords is not a part of exception handling?

a)

try

b)

finally

c)

thrown

d)

catch

65.

java.lang.NullPointerException is a

a)

Error

b)

runtime exception

c)

Compile time exception

d)

None

66.

Which of these class is highest in hierarchy in java

a)

java.lang.Exception

b)

java.lang.Error

c)

java.lang.Throwable

d)

java.lang.Object

67.

Choose the CORRECT exception


String department="JTMK";

Integer no=Integer.parseInt(department);

a)

ArithmeticException

b)

NullPointerException

c)

NumberFormatException

d)

ArrayIndexOutOfBoundsException

68.

Choose an exception if attempt to divide number by zero


int number = 89 / 0;

System.out.println("The answer is " + number);

a)

ArithmeticException

b)

NullPointerException

c)

NumberFormatException

d)

ArrayIndexOutOfBoundException

69.

What will it print?

a)

1

2

3

30

b)

1

2

30

c)

1

2

3

11

d)

1

2

11

70.

Which line or lines should we get inside a try block to avoid errors?

a)

3

b)

4

c)

6

d)

2

71.

What will this print?

a)

There was an error

b)

1

c)

9

d)

0

72.

What will this program print?

a)

Nothing, there is an error

b)

There was a ValueError, please try again

c)

num1num2num1^{num2}

d)

num2num1num2^{num1}

73.

public class Foo

{

public static void main(String[] args)

{

try

{

return;

}

finally

{

System.out.println( "Finally" );

}

}

}

a)

Finally

b)

Compilation fails.

c)

The code runs with no output

d)

An exception is thrown at runtime.

74.

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");

a)

finished

b)

runtime error.

c)

Compilation fails

d)

Exception.

75.

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 */

}

}

a)

ABCD

b)

Compilation fails.

c)

C is printed before exiting with an error message.

d)

BC is printed before exiting with an error message

76.

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();

}

}

a)

BD

b)

BDE

c)

BD

d)

DE

77.

FileNotFoundException

a)

Is a subclass/extends IOException

b)

Is a Compile time exception

c)

Found in java.io package

d)

All

78.

class Main {

public static void main(String args[]) {

try {

throw 10;

}

catch(int e) {

System.out.println("Got the Exception " + e);

}

}

}

a)

Got the Exception 10

b)

Got the Exception 0

c)

Compiler Error

d)

none

79.

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)

A

b)

B

c)

AC

d)

BC

80.

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.

a)

NumberFormatException

b)

NullPointerException

c)

ArrayIndexOutOfBoundsException

d)

InvalidStringException

81.

class IT

{

static public void main(String...args)

{

String s=null;

System.out.println("length is:"+s.length());

}

}

a)

It throws NullPointerException

b)

it throws IOException

c)

It throws StringExeption

d)

output is 0

82.

Which of the following should be true of the object thrown by a thrown statement?

a)

a) Should be assignable to String type

b)

b) Should be assignable to Exception type

c)

c) Should be assignable to Throwable type

d)

d) Should be assignable to Error type

83.

Which of the following keywords is used for throwing exception manually?

a)

finally

b)

try

c)

throw

d)

catch

84.
a)

A

b)

B

c)

Compilation Error

d)

Runtime Error

85.

Which of these keywords are used for the block to be examined for exceptions?

a)

try

b)

catch

c)

throw

d)

finally

86.
a)

Hello

b)

World

c)

HelloWOrld

d)

Compilation Error

87.
a)

Hello

b)

World

c)

HelloWOrld

d)

Compilation Error

88.
a)

Hello

b)

World

c)

HelloWOrld

d)

WorldWorld

89.

Which of these classes is super class of Exception class?

a)

Throwable

b)

System

c)

RunTime

d)

Class

90.

5) What is the need for Autoboxing and Unboxing in Java?

a)

A) Reduces code to be written by developers to convert from Wrapper to Primitive vice versa

b)

B) Compile time is reduced due to fixed methods followed to automatically convert Objects and Primitives

c)

C) Auto-unboxing improves memory efficiency by converting Wrapper to Primitive and process when object version is not needed

d)

D) All the above

Answer [=]

91.

public class ExceptionTest9

{

public static void main(String[] args)

{

System.out.println("I am going to forest.");

throw new ClassNotFoundException();

}

}

a)

I am going to forest.

b)

No output

c)

Compiler error

d)

None of the above

92.

Which is the superclass of Error class in Java?

a)

RuntimeException

b)

Exception

c)

Throwable

d)

Object

93.

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)

a=10 b=20

b)

b=20 a=10

c)

Compiler error

d)

No output

94.

Choose the right statement which does autoboxing in Java?

a)

Integer temperature1 = 100;

b)

int temperature2 = 101;

c)

Integer temperature1 = 100; int temperature2 = 101;

d)

All the above

95.

Choose the correct way of autoboxing Primitives to Wrapper objects below?

a)

Float f1 = 1.0f;

b)

Boolean bull = 0;

c)

int temperature2 = 101;

d)

All the above

96.

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");

    }

}

a)

Noting

b)

phew

c)

Compiler Error

d)

Program Halt

97.

1. Which of these keywords must be used to monitor for exceptions?

a)

a) try

b)

b) finally

c)

c) throw

d)

d) catch

98.

2. Class at the top of exception class hierarchy?

a)

ArithmeticException

b)

Throwable

c)

Object

d)

Exception

99.

3. In which of the following package, Exception class exist?

a)

java.util

b)

java.io

c)

java.lang

d)

java.file

100.

4. Exception generated by try block is caught in ______ block?

a)

catch

b)

throw

c)

throws

d)

finally

101.

5. Which exception is thrown when divide by zero occurs?

a)

NumberFormatException

b)

ArithmeticException

c)

NullPointerException

d)

None of these

102.

What is an exception in Java?

a)

An exception in Java is a feature that allows the program to run faster.

b)

An exception in Java is an event that occurs during the execution of a program that disrupts the normal flow of instructions.

c)

An exception in Java is a type of loop used for iteration.

d)

An exception in Java is a method used to print output to the console.

103.

What is the purpose of exception handling in Java?

a)

The purpose of exception handling in Java is to ignore runtime errors and continue with the program execution.

b)

The purpose of exception handling in Java is to display error messages to the user.

c)

The purpose of exception handling in Java is to increase the performance of the program.

d)

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.

104.

What is the difference between checked and unchecked exceptions in Java?

a)

Checked exceptions are not checked at compile-time, while unchecked exceptions are checked at compile-time.

b)

Checked exceptions are checked at compile-time, while unchecked exceptions are not checked at compile-time.

c)

Checked exceptions are checked at runtime, while unchecked exceptions are checked at compile-time.

d)

Checked exceptions are checked at compile-time, while unchecked exceptions are checked at runtime.

105.

What are the keywords used for exception handling in Java?

a)

try, throw, finally

b)

try, throw, catch

c)

throw, catch, finally

d)

try, catch, finally

106.

What is the use of try-catch block in Java?

a)

Memory management

b)

Exception handling

c)

Loop control

d)

Input/output operations

107.

What is the use of finally block in Java exception handling?

a)

The finally block is used to define a block of code that will be executed regardless of whether an exception is thrown or not.

b)

The finally block is used to handle exceptions in Java.

c)

The finally block is used to terminate the program in case of an exception.

d)

The finally block is used to catch and handle exceptions in Java.

108.

What is the difference between throw and throws in Java?

a)

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.

b)

The 'throw' keyword is used to catch exceptions, while 'throws' is used to throw exceptions.

c)

The 'throw' keyword is used to declare exceptions, while 'throws' is used to handle exceptions.

d)

The 'throw' keyword is used to handle exceptions, while 'throws' is used to declare exceptions.

109.

What is the difference between final, finally, and finalize in Java?

a)

'finalize' is a block used in exception handling to specify cleanup operations

b)

'finally' is a keyword used to specify code that will be executed only if an exception is thrown

c)

'final' is used to indicate that a variable can be changed

d)

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.

110.

What is the purpose of the throws keyword in Java?

a)

The throws keyword in Java is used to handle exceptions within a try-catch block.

b)

The throws keyword in Java is used to specify the return type of a method.

c)

The throws keyword in Java is used to define a custom exception class.

d)

The purpose of the throws keyword in Java is to declare that a method may throw one or more specific exceptions.

111.

What is the difference between Error and Exception in Java?

a)

Errors are minor issues that can be recovered from, while exceptions are serious issues that cannot be recovered from.

b)

Errors and exceptions are the same thing.

c)

Errors occur during compile-time, while exceptions occur during runtime.

d)

Errors are serious issues that cannot be recovered from, while exceptions can be handled and recovered from.

112.

What is the purpose of the finally block in Java exception handling?

a)

The finally block is used to define a block of code that will be executed regardless of whether an exception is thrown or not.

b)

The finally block is used to handle exceptions in Java.

c)

The finally block is used to terminate the program in case of an exception.

d)

The finally block is used to catch and handle exceptions in Java.

113.

What is the use of try-catch block in Java?

a)

Memory management

b)

Exception handling

c)

Loop control

d)

Input/output operations

114.

What is the purpose of the finally block in Java exception handling?

a)

The finally block is used to define a block of code that will be executed regardless of whether an exception is thrown or not.

b)

The finally block is used to handle exceptions in Java.

c)

The finally block is used to terminate the program in case of an exception.

d)

The finally block is used to catch and handle exceptions in Java.

115.

What is the difference between checked and unchecked exceptions in Java?

a)

Checked exceptions are not checked at compile-time, while unchecked exceptions are checked at compile-time.

b)

Checked exceptions are checked at compile-time, while unchecked exceptions are not checked at compile-time.

c)

Checked exceptions are checked at runtime, while unchecked exceptions are checked at compile-time.

d)

Checked exceptions are checked at compile-time, while unchecked exceptions are checked at runtime.