wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Exception in Java

Total questions: 41

Worksheet time: 20mins

Name
Class
Date
1.

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

2.

What is the parent class of All Exceptions in JAVA

a)

Throw

b)

Exception

c)

Error

d)

Bug

3.

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

a)

try

b)

catch

c)

finally

d)

throw

4.

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

5.

Exception classes belongs to following package

a)

import java.io.*

b)

import java.lang.*

c)

import java.util.*

d)

import java.lang.Exception.*

6.

How to create our own exception

a)

using 'throw' keyword

b)

using 'throws' keyword

c)

using 'finally' keyword

d)

using 'throwable' keyword

7.

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

8.

What will it print?

a)

1

2

3

30

b)

1

2

30

c)

1

2

3

11

d)

1

2

11

9.

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}

10.

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.

11.

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

12.

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

13.

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

14.

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

a)

Class

b)

Interface

c)

Abstract class

d)

Other

15.

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

16.

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

17.

What is the parent class of All Exceptions in JAVA

a)

Throw

b)

Exception

c)

Error

d)

Bug

18.

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

a)

try

b)

catch

c)

finally

d)

throw

19.

try block may or may not contains catch blocks

a)

True

b)

False

20.

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

21.

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

22.

How to create our own exception

a)

using 'throw' keyword

b)

using 'throws' keyword

c)

using 'finally' keyword

d)

using 'throwable' keyword

23.

Exception classes belongs to following package

a)

import java.io.*

b)

import java.lang.*

c)

import java.util.*

d)

import java.lang.Exception.*

24.

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

a)

try

b)

finally

c)

thrown

d)

catch

25.

java.lang.NullPointerException is a

a)

Error

b)

runtime exception

c)

Compile time exception

d)

None

26.

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

27.

Choose the CORRECT exception


String department="JTMK";

Integer no=Integer.parseInt(department);

a)

ArithmeticException

b)

NullPointerException

c)

NumberFormatException

d)

ArrayIndexOutOfBoundsException

28.

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

29.

What will it print?

a)

1

2

3

30

b)

1

2

30

c)

1

2

3

11

d)

1

2

11

30.

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

a)

3

b)

4

c)

6

d)

2

31.

What will this print?

a)

There was an error

b)

1

c)

9

d)

0

32.

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}

33.

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.

34.

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.

35.

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

36.

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

37.

FileNotFoundException

a)

Is a subclass/extends IOException

b)

Is a Compile time exception

c)

Found in java.io package

d)

All

38.

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

39.

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

40.

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

41.

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