wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

throws keyword + java

Total questions: 89

Worksheet time: 30mins

Name
Class
Date
1.

What is the parameter type of the off parameter in the write method in the Writerclass?

a)
int
b)
char
c)
String
d)
Exception
2.

What is the name of the method that is used to divide two numbers in the image?

a)
divide
b)
main
c)
System
d)
Exception
3.

Which of the following is the correct way to handle the exception in the image?

a)

Use a try-catch block.

b)

Use the throws keyword.

c)

Both try-catch block and throws keyword can be used.

d)

None of the above.

4.

What is the name of the exception that is being thrown in the image?

a)
ArithmeticException
b)
NullPointerException
c)
FileNotFoundException
d)
IOException
5.

What is the output of the program in the image?

a)

Result of Division: 0

b)

java.lang.ArithmeticException: divide by zero

c)

Unreachable code

d)

None of the above

6.

What is the output of the program in the image?

a)

welcome to vote

b)

Exception in thread "main" java.lang.ArithmeticException: not valid

c)

rest of the code.

d)

None of the above

7.

What is the purpose of the try-catch block in the image?

a)

To check if the age is valid.

b)

To print the message "welcome to vote" if the age is valid.

c)

To handle the ExceptionClass exception.

d)

None of the above.

8.

What is the purpose of the throw keyword in the MyString class?

a)

To create a new InvalidStringException object.

b)

To throw an InvalidStringException exception.

c)

To print the message "Invalid string" to the console.

d)

To exit the program.

9.

What is the purpose of the throws keyword in the method(int i) method?

a)

To declare that the method(int i) method may throw an IllegalArgumentException exception.

b)

To throw an IllegalArgumentException exception.

c)

To print the message "Invalid argument" to the console.

d)

To exit the program.

10.

What is the purpose of the throw keyword in the divideInt() method?

a)

To create a new IllegalArgumentException object.

b)

To throw an IllegalArgumentException exception.

c)

To print the message "Divinor cannot be zero!" to the console.

d)

To exit the program.

11.

What is the purpose of the throws keyword in the CustomThrower class?

a)

To declare that the CustomThrower class may throw a MyThrowable exception.

b)

To throw a MyThrowable exception.

c)

To print the message "MyThrowable exception thrown" to the console.

d)

To exit the program.

12.

Which of the following is the correct output of the program in the image?

a)

Line is striine

b)

Unreachable code

c)

java.io.FileNotFoundException: F:\abc.txt (The system cannot find the file specified)

d)

java.lang.NullPointerException

13.

Which of the following is the purpose of the throws keyword in the main() method?

a)

To declare that the main() method may throw a FileNotFoundException exception.

b)

To throw a FileNotFoundException exception.

c)

To print the message "File not found" to the console.

d)

To exit the program.

14.

Question: Which of the following is the purpose of the throw keyword in the m() method?

a)

To throw a NullPointerException exception.

b)

To print the message "NullPointerException" to the console.

c)

To exit the program.

d)

None of the above.

15.

What is the purpose of the throws keyword in the MyClass class?

a)

To declare that the MyClass class may throw a NullPointerException exception.

b)

To throw a NullPointerException exception.

c)

To print the message "NullPointerException" to the console.

d)

To exit the program.

16.

What is the purpose of the throws keyword in the readFromFile() method?

a)

To declare that the readFromFile() method may throw a FileNotFoundException exception.

b)

To throw a FileNotFoundException exception.

c)

To print the message "File not found" to the console.

d)

To exit the program.

17.

What is the output of the program in the image?

a)
12345
b)

Exception in thread "main" java.lang.ArithmeticException: divide by zero

c)

Unreachable code

d)

None of the above.

18.

Question: What is the purpose of the throw keyword in the MyString class?

a)

To throw an InvalidStringException exception.

b)

To print the message "Invalid string" to the console.

c)

To exit the program.

d)

To declare that the MyString class may throw an InvalidStringException exception.

19.

Which of the following is the purpose of the throws keyword in the main() method?

a)

To declare that the main() method may throw a NullPointerException exception.

b)

To throw a NullPointerException exception.

c)

To print the message "NullPointerException" to the console.

d)

To exit the program.

20.

What is the purpose of the throw keyword in the method() method?

a)

To throw an ArithmeticException exception.

b)

To print the message "Invalid argument" to the console.

c)

To exit the program..

d)

To declare that the method() method may throw an ArithmeticException exception.

21.

What is the purpose of the throw keyword in the MyString class?

a)

To throw an InvalidStringException exception.

b)

To print the message "Invalid string" to the console.

c)

To exit the program.

d)

To declare that the MyString class may throw an InvalidStringException exception.

22.

Which of the following is the purpose of the throws keyword in the main() method?

a)

To declare that the main() method may throw a NullPointerException exception.

b)

To throw a NullPointerException exception.

c)

To print the message "NullPointerException" to the console.

d)

To exit the program.

23.

Which of the following is the correct output of the program in the image?

a)

Line is striine

b)

Unreachable code

c)

:java.lang.NullPointerException

d)

java.io.FileNotFoundException

24.

Which of the following is the purpose of the throws keyword in the readFromFile() method?

a)

To declare that the readFromFile() method may throw a FileNotFoundException exception.

b)

To throw a FileNotFoundException exception.

c)

To print the message "File not found" to the console.

d)

To exit the program.

25.

What is the purpose of the throws keyword in the MyString class?

a)

To declare that the MyString class may throw a NullPointerException exception..

b)

To throw a NullPointerException exception.

c)

To print the message "Invalid string" to the console.

d)

To exit the program..

26.

What is the output of the program in the image?

a)

Line is striine

b)

Unreachable code

c)

java.lang.NullPointerException

d)
java.io.FileNotFoundException
27.

What is the purpose of the try-catch statement in the image?

a)

To catch the NullPointerException exception.

b)

To print the message "This is a dummy exception" to the console.

c)

To print the message "Line is striine" to the console.

d)

To exit the program.

28.

What is the output of the program in the image?

a)

Line is striine

b)

This is a dummy exception

c)

Unreachable code

d)
java.lang.NullPointerException
29.

What is the purpose of the throws keyword in the MyString class?

a)

To declare that the MyString class may throw a NullPointerException exception.

b)

To throw a NullPointerException exception.

c)

To print the message "Invalid string" to the console.

d)

To exit the program.

30.

What is the output of the program in the image?

a)

Line is striine

b)

This is a dummy exception

c)
java.lang.NullPointerException
d)

Unreachable code

31.

What is the purpose of the throw keyword in the deleteFile() method?

a)

To throw a FileNotFoundException exception.

b)

To print the message "File not found" to the console.

c)

To exit the program.

d)

To declare that the deleteFile() method may throw a FileNotFoundException exception.

32.

What is the output of the program in the image?

a)

Line is striine

b)

This is a dummy exception

c)
java.lang.NullPointerException
d)

Unreachable code

33.

What is the purpose of the throw keyword in the divideInt() method?

a)

To throw a ArithmeticException exception.

b)

To print the message "Divisor cannot be zero!" to the console.

c)

To exit the program..

d)

To declare that the divideInt() method may throw an ArithmeticException exception.

34.

What is the output of the program in the image?

a)

Line is striine

b)

This is a dummy exception

c)
java.lang.NullPointerException
d)

Unreachable code

35.

Which of the following is the purpose of the throws keyword in the displayFile() method?

a)

To throw a FileNotFoundException exception.

b)

To print the message "File not found" to the console.

c)

To exit the program.

d)

To declare that the displayFile() method may throw a FileNotFoundException exception.

36.

What is the output of the program in the image?

a)

Line is striine

b)

This is a dummy exception

c)
java.lang.NullPointerException
d)

Unreachable code

37.

Which of the following is the type of exception that is thrown in the image?

a)
NullPointerException
b)
ArithmeticException
c)
FileNotFoundException
d)
ArrayIndexOutOfBoundsException
38.

Which of the following is the correct code to fix the error in the image?

a)

public class Main {

public static void main(String[] args) {

String s = "striine";

if (s != null) {

System.out.println("Line is " + s);

} else {

throw new NullPointerException();

}

}

}

b)

public class Main {

public static void main(String[] args) {

String s = "striine";

if (s == null) {

System.out.println("Line is null");

} else {

System.out.println("Line is " + s);

}

}

}

c)

public class Main {

public static void main(String[] args) {

String s;

try {

s = "striine";

System.out.println("Line is " + s);

} catch (NullPointerException e) {

System.out.println("Line is null");

}

}

}

d)

public class Main {

public static void main(String[] args) {

String s = null;

System.out.println("Line is " + s);

}

}

39.

What is the output of the program in the image?

a)

Line is null

b)

This is a dummy exception

c)
java.lang.NullPointerException
d)

Unreachable code

40.

What is the purpose of the throw keyword in the image?

a)

To throw a NullPointerException exception.

b)

To print the message "This is a dummy exception" to the console.

c)

To exit the program.

d)

To declare that the s variable may be null.

41.

Which of the following is the correct code to fix the error in the image?

a)

public class Main {

public static void main(String[] args) {

String s = null;

if (s != null) {System.out.println("Lineis " + s);

} else {

throw new NullPointerException();

}

}

}

b)

public class Main {

public static void main(String[] args) {

String s = null;

if (s == null) {System.out.println("Line is null");

} else {

System.out.println("Line is " + s);

}

}

}

c)

public class Main {

public static void main(String[] args) {

String s;

try {s = "striine";

System.out.println("Line is " + s);

} catch (NullPointerException e) {

System.out.println("Line is null");

}

}

}

d)

public class Main {

public static void main(String[] args) {

String s = "striine";

System.out.println("Line is " + s);

}

}

42.

Which keyword is used to throw an exception?

a)

Throw [Image shows an arrow pointing to the word "Throw".]

b)
Throws
c)
Exception
d)
Method
43.

What is the name of the exception that is thrown in the image?

a)

CustomException [Image shows an arrow pointing to the word "CustonException".]

b)

DenominatorCan'tBeZeroException

c)
ArithmeticException
d)
NullPointerException
44.

Which keyword is used to declare that a method can throw an exception?

a)

throw [Image shows an arrow pointing to the word "throws".]

b)
exception
c)
try
d)
catch
45.

Which exception is declared to be thrown by the main() method in the image?

a)

FileNotFoundException [Image shows an arrow pointing to the line of code throws FileNotFoundException.]

b)
IOException
c)
ArithmeticException
d)
NullPointerException
46.

What is the difference between the throw and throws keywords in Java?

a)

The throw keyword is used to explicitly throw an exception, while the throws keyword is used to declare that a method can throw an exception. [Image shows an arrow pointing to the word "throw" in the first column, and an arrow pointing to the word "throws" in the second column.]

b)

The throw keyword can only be used to throw checked exceptions, while the throws keyword can be used to throw both checked and unchecked exceptions.

c)

The throw keyword can only be used within a method, while the throws keyword can be used in a method declaration or in a class declaration.

d)

The throw keyword can be used to throw multiple exceptions, while the throws keyword can only be used to throw one exception.

47.

Which keyword is used to throw an exception in Java?

a)

throw [Image shows an arrow pointing to the word "throw" in the line of code throw new FileNotFoundException("File not found");.]

b)
throws
c)
Exception
d)
method
48.

Which keyword is used to check if a string is empty in Java?

a)

isEmpty() [Image shows an arrow pointing to the word "isEmpty()" in the line of code if (content.isEmpty()).]

b)

isBlank()

c)
length()
d)
equals("")
49.

What is the name of the exception that is thrown in the image?

a)

ArithmeticException [Image shows an arrow pointing to the word "ArithmeticException" in the line of code throw new ArithmeticException("Person is not eligible to vote");.]

b)
CustomException
c)
DenominatorCan'tBeZeroException
d)
NullPointerException
50.

Who is responsible for handling the exception thrown by the throw keyword?

a)

The JVM [Image shows an arrow pointing to the word "JVM" in the column "Responsibility of handling exception".]

b)

The caller method

c)

The throw keyword itself

d)

The throws keyword

51.

Which keyword is used to declare that a method can throw an exception??

a)

throw [Image shows an arrow pointing to the word "throws" in the column "Keyword used to declare that a method can throw an exception".]

b)
exception
c)
try
d)
catch
52.

Which keyword is used to throw an exception in Java?

a)

throws [Image shows an arrow pointing to the word "throw" in the code.]

b)
exception
c)
try
d)
catch
53.

Which keyword is used to declare that a method can throw an exception?

a)

throw [Image shows an arrow pointing to the word "throws" in the image.]

b)
exception
c)
try
d)
catch
54.

Which exception is thrown by the disp() method in the Exceptl class?

a)

FileNotFoundException [Image shows an arrow pointing to the line of code throw new FileNotFoundException("File not found"); in the disp() method of the Exceptl class.]

b)
IOException
c)
ArithmeticException
d)
NullPointerException
55.

Which keyword is used to throw an exception in Java?

a)

throw [Image shows an arrow pointing to the word "throw" in the line of code throw new CustomException("This is a custom exception");.]

b)
throws
c)
exception
d)
method
56.

Which exception is thrown in the image?

a)

ArrayIndexOutOfBoundsException [Image shows an arrow pointing to the line of code num/arr[i], where i is 10 and the array arr only has 5 elements.]

b)
ArithmeticException
c)
NullPointerException
d)
FileNotFoundException
57.

Which keyword is used to declare that a method can throw an exception?

a)

throw [Image shows an arrow pointing to the word "throws" in the line of code public void disp() throws FileNotFoundException {}.]

b)
exception
c)
try
d)
catch
58.

Which exception is thrown in the image?

a)
ArithmeticException
b)
ArrayIndexOutOfBoundsException
c)
NullPointerException
d)
FileNotFoundException
59.

Which keyword is used to throw an exception in Java?

a)
throw
b)
try
c)
catch
d)
finally
60.

Which keyword is used to declare that a method can throw an exception??

a)
throw
b)
throws
c)
exception
d)
method
61.

Which exception is thrown by the getUserName() method?

a)
UserNotExistException
b)
ArithmeticException
c)
NullPointerException
d)
FileNotFoundException
62.

Which keyword is used to declare that a method can throw a CustomException?

a)
throw
b)
throws
c)

To exit the program.

d)
method
63.

What is the name of the exception that is thrown in the image?

a)
ArithmeticException
b)
ArrayIndexOutOfBoundsException
c)
exception
d)
FileNotFoundException
64.

Which exception is thrown in the image?

a)
FileNotFoundException
b)
ArithmeticException
c)
NullPointerException
d)
ArrayIndexOutOfBoundsException
65.

Which exception is thrown by the disp() method?

a)
CustomException
b)
ArithmeticException
c)
NullPointerException
d)
FileNotFoundException
66.

What keyword is used to declare that a method can throw an exception?

a)
throw
b)
throws
c)
exception
d)
method
67.

Which exception is declared to be thrown by the disp() method?

a)
FileNotFoundException
b)
ArithmeticException
c)
NullPointerException
d)
ArrayIndexOutOfBoundsException
68.

Which keyword is used to throw an exception in Java?

a)
throw
b)
try
c)
catch
d)
finally
69.

Which exception is thrown by the disp() method?

a)
FileNotFoundException
b)
ArithmeticException
c)
NullPointerException
d)
ArrayIndexOutOfBoundsException
70.

Which keyword is used to declare that a method can throw an exception?

a)
throw
b)
throw
c)
exception
d)
method
71.

Which exception is thrown by the disp() method?

a)
ArithmeticException
b)
ArithmeticException
c)
NullPointerException
d)
ArrayIndexOutOfBoundsException
72.

Which exception is thrown by the disp() method?

a)
UserNotExistException
b)
ArithmeticException
c)
NullPointerException
d)
ArrayIndexOutOfBoundsException
73.

What is the name of the exception that is thrown in the image?

a)
ArithmeticException
b)
ArrayIndexOutOfBoundsException
c)
NullPointerException
d)
FileNotFoundException
74.

What is the keyword used to declare that a method can throw an exception?

a)
throw
b)
throws
c)
exception
d)
method
75.

Which exception is thrown by the disp(int i) method?

a)
ArrayIndexOutOfBoundsException
b)
ArithmeticException
c)
NullPointerException
d)
FileNotFoundException
76.

What is the keyword that is used to declare that a method can throw an exception?

a)
throw
b)
throws
c)
exception
d)
method
77.

Which exception is declared to be thrown by the disp() method?

a)
FileNotFoundException
b)
ArithmeticException
c)
NullPointerException
d)
ArrayIndexOutOfBoundsException
78.

What is the keyword used to throw an exception in Java?

a)
throw
b)
tries
c)
catches
d)
finally
79.

What is the name of the custom exception that is thrown by the disp() method?

a)
CustomException
b)
ArithmeticException
c)
NullPointerException
d)
ArrayIndexOutOfBoundsException
80.

Which exception is thrown by the disp() method?

a)
FileNotFoundException
b)
ArithmeticException
c)
NullPointerException
d)
ArrayIndexOutOfBoundsException
81.

What is the keyword used to declare that a method can throw an exception?

a)
throw
b)
throws
c)
exception
d)
method
82.

Which keyword is used to declare that a method can throw an exception?

a)
throw
b)
throws
c)
exception
d)
method
83.

Which exception is thrown by the disp() method?

a)
ArithmeticException
b)
FileNotFoundException
c)
NullPointerException
d)
ArrayIndexOutOfBoundsException
84.

What is the name of the exception that is thrown by the disp() method?

a)
UserNotExistException
b)
ArithmeticException
c)
NullPointerException
d)
ArrayIndexOutOfBoundsException
85.

What is the name of the exception that is thrown by the disp() method?

a)
CustomException
b)
ArithmeticException
c)
NullPointerException
d)
ArrayIndexOutOfBoundsException
86.

What is the keyword used to throw an exception in Java?

a)
throw
b)
tries
c)
catches
d)
finally
87.

What is the keyword used to declare that a method can throw an exception?

a)
throw
b)
throws
c)
exception
d)
method
88.

What is the keyword used to declare that a method can throw an exception?

a)
throws
b)
try
c)
catch
d)
finally
89.

Which exception is declared to be thrown by the disp() method?

a)
FileNotFoundException
b)
ArithmeticException
c)
NullPointerException
d)
ArrayIndexOutOfBoundsException