Here are multiple-choice questions (MCQs) on throw and throws in Java:
Question 1
What is the purpose of the throw keyword in Java?
A) To declare that a method might throw an exception.
B) To actually throw an exception from a method or block of code.
C) To handle an exception that has been thrown.
D) To prevent exceptions from being thrown.
Answer: B) To actually throw an exception from a method or block of code.
Question 2
What does the throws keyword do in a method declaration?
A) It handles the exception that may occur.
B) It specifies the exceptions that a method may throw.
C) It rethrows an exception caught in the method.
D) It stops the execution of the method.
Answer: B) It specifies the exceptions that a method may throw.
Question 3
Which of the following statements is true about the throws keyword?
A) It is used to throw an exception immediately.
B) It must be followed by the type of exception that the method might throw.
C) It is used within a method to handle an exception.
D) It can only be used with unchecked exceptions.
Answer: B) It must be followed by the type of exception that the method might throw.
Question 4
What is the correct syntax for throwing an exception in Java?
A) throws new Exception();
B) throw new Exception();
C) throw Exception;
D) throws Exception;
Answer: B) throw new Exception();
Question 5
Consider the following method declaration:
java
Copy code
public void method() throws IOException, SQLException {
// method implementation
}
What does this declaration indicate?
A) The method will handle IOException and SQLException.
B) The method will throw IOException and SQLException which must be handled by the caller.
C) The method will throw runtime exceptions IOException and SQLException.
D) The method will not throw any exceptions.
Answer: B) The method will throw IOException and SQLException which must be handled by the caller.
Question 6
What happens if a method with a throws clause does not handle or declare the exception specified?
A) The program will compile but may not run.
B) The method must use the throw keyword.
C) The compiler will generate an error if the exception is not handled or declared.
D) The exception will be automatically caught by the JVM.
Answer: C) The compiler will generate an error if the exception is not handled or declared.
Question 7
Which of the following correctly uses the throw keyword?
A) throws new IOException("Error occurred");
B) throw new IOException("Error occurred");
C) throws IOException("Error occurred");
D) throw IOException("Error occurred");
Answer: B) throw new IOException("Error occurred");
Question 8
If a method calls another method that throws a checked exception, what must the calling method do?
A) Catch the exception and handle it.
B) Ignore the exception.
C) Declare that it throws the exception using the throws keyword.
D) Automatically convert the exception to an unchecked exception.
Answer: C) Declare that it throws the exception using the throws keyword.
Question 9
Which of the following statements about the throw keyword is false?
A) throw is used to explicitly throw an exception.
B) The throw keyword can only be used with checked exceptions.
C) throw can be used with any type of exception, including checked and unchecked.
D) throw must be followed by an instance of Throwable or its subclasses.
Answer: B) The throw keyword can only be used with checked exceptions.
Question 10
Which keyword is used to throw an exception from within a method?
A) throws
B) throw
C) throwing
D) exception
Answer: B) throw
Question 11
In which scenario is the throws keyword used?
A) When creating a new exception object.
B) When catching exceptions inside a method.
C) When declaring exceptions that a method might throw.
D) When defining exception classes.
Answer: C) When declaring exceptions that a method might throw.
Question 12
How do you throw a checked exception from within a method?
A) By using throw keyword.
B) By declaring it in the throws clause.
C) By catching it with a try-catch block.
D) By creating an instance of Exception class.
Answer: A) By using throw keyword.
Question 13
Which of the following is an example of using throws in a method declaration?
A) public void method() throws IOException
B) public void method() throw IOException
C) public void method() { throw IOException; }
D) public void method() { throws IOException; }
Answer: A) public void method() throws IOException
Question 14
When using throws in a method signature, what must the calling code do?
A) Handle the exception using a try-catch block.
B) Ignore the exception.
C) Ensure that the exception is declared in its own method signature.
D) Use the throw keyword to handle the exception.
Answer: C) Ensure that the exception is declared in its own method signature.
Question 15
Which of the following is true about the throw keyword?
A) It can be used to handle exceptions.
B) It can only throw exceptions of type Exception.
C) It must be used to create a new instance of an exception.
D) It is used to declare that a method throws exceptions.
Answer: C) It must be used to create a new instance of an exception.
Question 16
Which keyword is used to specify multiple exceptions that a method might throw?
A) throw
B) throws
C) exception
D) catch
Answer: B) throws
Question 17
What will happen if a method that declares throws Exception is called without handling or declaring the exception?
A) The method will execute normally.
B) The program will compile and run without issues.
C) The program will not compile.
D) The exception will be handled by the JVM automatically.
Answer: C) The program will not compile.
Question 18
Which exception is thrown if the throw keyword is used incorrectly?
A) ClassNotFoundException
B) IllegalArgumentException
C) SyntaxException
D) CompileTimeException
Answer: B) IllegalArgumentException
Question 19
In which of the following scenarios is it appropriate to use throws?
A) When you want to handle an exception in the method.
B) When you want to create an instance of an exception.
C) When a method needs to propagate an exception to its caller.
D) When you want to declare that a method will not throw any exceptions.
Answer: C) When a method needs to propagate an exception to its caller.
Question 20
Which of the following is a correct example of throwing an exception and handling it?
A)
java
Copy code
public void method() throws IOException {
throw new IOException("IO Error");
}
B)
java
Copy code
public void method() {
throws new IOException("IO Error");
}
C)
java
Copy code
public void method() {
throw new IOException("IO Error");
}
D)
java
Copy code
public void method() throws IOException {
throw IOException("IO Error");
}
Answer: A) public void method() throws IOException { throw new IOException("IO Error"); }
No comments:
Post a Comment