Saturday, August 10, 2024

Exceptions MCQ

 MCQ 1: What is the superclass of all exceptions in Java?

A) Error

B) Throwable

C) Exception

D) RuntimeException


Answer: B) Throwable


MCQ 2: Which of the following is an unchecked exception in Java?

A) IOException

B) ClassNotFoundException

C) ArithmeticException

D) FileNotFoundException


Answer: C) ArithmeticException


MCQ 3: Which of the following keywords is used to handle exceptions in Java?

A) try

B) catch

C) finally

D) All of the above


Answer: D) All of the above


MCQ 4: What happens if an exception is not caught in a Java program?

A) The program will compile but not run.

B) The program will terminate abruptly.

C) The program will continue executing from the next line.

D) The program will skip the exception and run normally.


Answer: B) The program will terminate abruptly.


MCQ 5: Which of the following is true about the finally block?

A) It is executed only when an exception is thrown.

B) It is executed only when no exception is thrown.

C) It is executed whether or not an exception is thrown.

D) It is never executed.


Answer: C) It is executed whether or not an exception is thrown.


MCQ 6: Which of the following is a checked exception?

A) NullPointerException

B) ArrayIndexOutOfBoundsException

C) IOException

D) ArithmeticException


Answer: C) IOException


MCQ 7: What will happen if a try block does not have a corresponding catch block?

A) It will cause a compile-time error.

B) It will cause a runtime error.

C) It will cause a warning.

D) It is allowed if there is a finally block.


Answer: D) It is allowed if there is a finally block.


MCQ 8: Which of the following exceptions is thrown when a method cannot find a file that it needs to open?

A) FileNotFoundException

B) IOException

C) NullPointerException

D) EOFException


Answer: A) FileNotFoundException


MCQ 9: Which exception is thrown when an arithmetic operation results in an overflow or underflow?

A) ArithmeticException

B) IllegalArgumentException

C) IndexOutOfBoundsException

D) NumberFormatException


Answer: A) ArithmeticException


MCQ 10: What is the purpose of the throw keyword in Java?

A) To declare an exception.

B) To propagate an exception.

C) To handle an exception.

D) To manually throw an exception.


Answer: D) To manually throw an exception.



Here are multiple-choice questions (MCQs) on checked exceptions in Java:


Question 1

What is a checked exception in Java?


A) An exception that occurs due to a syntax error.


B) An exception that is checked at runtime.


C) An exception that must be either caught or declared in the method signature.


D) An exception that can be ignored during compilation.


Answer: C) An exception that must be either caught or declared in the method signature.


Question 2

Which of the following is a checked exception?


A) NullPointerException


B) ArrayIndexOutOfBoundsException


C) FileNotFoundException


D) ArithmeticException


Answer: C) FileNotFoundException


Question 3

What must a method do if it throws a checked exception?


A) Catch the exception using a try-catch block.


B) Ignore the exception.


C) Declare the exception in the method signature using the throws keyword.


D) Terminate the program.


Answer: C) Declare the exception in the method signature using the throws keyword.


Question 4

Which of the following statements about checked exceptions is true?


A) They are a type of runtime exception.


B) They are not checked at compile time.


C) They must be handled using try-catch or declared in the method signature.


D) They can be thrown without being declared.


Answer: C) They must be handled using try-catch or declared in the method signature.


Question 5

Which of the following is NOT a checked exception?


A) IOException


B) SQLException


C) ClassNotFoundException


D) RuntimeException


Answer: D) RuntimeException


Question 6

What is the primary purpose of checked exceptions in Java?


A) To handle errors that occur due to incorrect user inputs.


B) To handle unexpected errors that occur during program execution.


C) To enforce error handling for conditions that a reasonable application might want to catch.


D) To handle exceptions that are related to arithmetic operations.


Answer: C) To enforce error handling for conditions that a reasonable application might want to catch.


Question 7

Which of the following checked exceptions is thrown when an attempt is made to access a file that does not exist?


A) IOException


B) FileNotFoundException


C) IllegalArgumentException


D) SecurityException


Answer: B) FileNotFoundException


Question 8

How can a method indicate that it might throw a checked exception?


A) By including the exception in a catch block.


B) By using the throws keyword in the method signature.


C) By using the throw keyword inside the method.


D) By documenting the exception in the comments.


Answer: B) By using the throws keyword in the method signature.


Question 9

What happens if a method does not handle a checked exception and does not declare it with throws?


A) The exception is silently ignored.


B) The program will not compile.


C) The exception is automatically converted to a runtime exception.


D) The exception is thrown at runtime.


Answer: B) The program will not compile.


Question 10

Which of the following checked exceptions is related to database access?


A) SQLException


B) NullPointerException


C) ClassCastException


D) NumberFormatException


Answer: A) SQLException


Question 11

Can checked exceptions be caught by a try-catch block?


A) No, they cannot be caught.


B) Yes, they must be caught or declared in the method signature.


C) Only if they are runtime exceptions.


D) Only if they are not declared in the method signature.


Answer: B) Yes, they must be caught or declared in the method signature.


Question 12

Which exception is a subclass of Exception but not a subclass of RuntimeException?


A) IOException


B) IllegalArgumentException


C) IndexOutOfBoundsException


D) ClassCastException


Answer: A) IOException


Question 13

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


A) catch


B) throw


C) throws


D) final


Answer: B) throw


Question 14

In which package are most of the checked exceptions found in Java?


A) java.util


B) java.io


C) java.lang


D) java.net


Answer: B) java.io


Question 15

Which of the following methods does not throw a checked exception?


A) Thread.sleep(long millis)


B) FileReader(String fileName)


C) Integer.parseInt(String s)


D) Class.forName(String className)


Answer: C) Integer.parseInt(String s)


Question 16

What does the throws keyword indicate in a method declaration?


A) The method can throw runtime exceptions.


B) The method must handle all exceptions internally.


C) The method can potentially throw specified checked exceptions.


D) The method does not need to handle exceptions.


Answer: C) The method can potentially throw specified checked exceptions.


Question 17

Which of the following scenarios will lead to a checked exception?


A) Dividing by zero


B) Accessing an invalid array index


C) Reading a file that does not exist


D) Converting a string to an integer


Answer: C) Reading a file that does not exist


Question 18

Can a checked exception be a superclass of RuntimeException?


A) Yes, checked exceptions are always superclasses of RuntimeException.


B) No, checked exceptions cannot be superclasses of RuntimeException.


C) Only if they are declared as final.


D) Yes, but only in custom exception classes.


Answer: B) No, checked exceptions cannot be superclasses of RuntimeException.


Question 19

What will happen if an exception is thrown by a method but not handled or declared?


A) The program will continue execution.


B) The program will ignore the exception.


C) The program will not compile.


D) The program will terminate unexpectedly at runtime.


Answer: C) The program will not compile.


Question 20

What is the role of the try block in Java?


A) To declare exceptions that might be thrown.


B) To define the code that might throw exceptions.


C) To handle exceptions that are thrown.


D) To finalize resources.


Answer: B) To define the code that might throw exceptions.




Here are multiple-choice questions (MCQs) on unchecked exceptions in Java:


Question 1

What is an unchecked exception in Java?


A) An exception that must be caught or declared in the method signature.


B) An exception that occurs due to syntax errors.


C) An exception that occurs at runtime and is not checked at compile time.


D) An exception that can only be thrown by the Java Virtual Machine (JVM).


Answer: C) An exception that occurs at runtime and is not checked at compile time.


Question 2

Which of the following is an example of an unchecked exception in Java?


A) IOException


B) SQLException


C) FileNotFoundException


D) NullPointerException


Answer: D) NullPointerException


Question 3

Which class is the superclass of all unchecked exceptions in Java?


A) Throwable


B) Exception


C) RuntimeException


D) Error


Answer: C) RuntimeException


Question 4

Which of the following exceptions is NOT an unchecked exception?


A) ArrayIndexOutOfBoundsException


B) NumberFormatException


C) ClassCastException


D) InterruptedException


Answer: D) InterruptedException


Question 5

When does a NullPointerException typically occur?


A) When an object is not found.


B) When trying to use an object reference that has the value null.


C) When accessing an out-of-bounds array index.


D) When the JVM runs out of memory.


Answer: B) When trying to use an object reference that has the value null.


Question 6

Which of the following is true about unchecked exceptions?


A) They must be handled using a try-catch block.


B) They can be caught at runtime but are not required to be declared in the method signature.


C) They are checked at compile time.


D) They must always be declared with the throws keyword.


Answer: B) They can be caught at runtime but are not required to be declared in the method signature.


Question 7

What will happen if an unchecked exception is not caught in a Java program?


A) The program will terminate immediately.


B) The exception will be ignored.


C) The program will compile but may throw an exception at runtime.


D) The program will compile and run without any issues.


Answer: C) The program will compile but may throw an exception at runtime.


Question 8

Which unchecked exception is thrown when a method receives an argument that is inappropriate?


A) IllegalArgumentException


B) IOException


C) SQLException


D) FileNotFoundException


Answer: A) IllegalArgumentException


Question 9

Can unchecked exceptions be caught using a try-catch block in Java?


A) No, unchecked exceptions cannot be caught.


B) Yes, but only if they are declared in the method signature.


C) Yes, they can be caught, but it's not mandatory.


D) Yes, but only by the JVM.


Answer: C) Yes, they can be caught, but it's not mandatory.


Question 10

Which of the following is a characteristic of unchecked exceptions?


A) They are checked at compile time.


B) They must be declared in the method signature.


C) They are caused by programming errors such as logical flaws.


D) They are primarily used for handling hardware issues.


Answer: C) They are caused by programming errors such as logical flaws.


Question 11

Which of the following unchecked exceptions occurs when an object is cast to a class of which it is not an instance?


A) ClassCastException


B) ArrayIndexOutOfBoundsException


C) ArithmeticException


D) NoSuchElementException


Answer: A) ClassCastException


Question 12

Which unchecked exception is thrown when an illegal operation is performed on a string, such as accessing an invalid index?


A) ArrayStoreException


B) StringIndexOutOfBoundsException


C) IndexOutOfBoundsException


D) ArrayIndexOutOfBoundsException


Answer: B) StringIndexOutOfBoundsException


Question 13

What does the IllegalStateException indicate in Java?


A) The program has entered an invalid state.


B) An illegal or inappropriate operation has occurred.


C) An attempt was made to access an element that does not exist.


D) A null reference was used in a method call.


Answer: A) The program has entered an invalid state.


Question 14

In Java, what is the main difference between checked and unchecked exceptions?


A) Checked exceptions are subclasses of Exception, while unchecked exceptions are subclasses of RuntimeException.


B) Unchecked exceptions must be declared in the method signature, while checked exceptions do not.


C) Checked exceptions occur at runtime, while unchecked exceptions are checked at compile time.


D) There is no difference between checked and unchecked exceptions.


Answer: A) Checked exceptions are subclasses of Exception, while unchecked exceptions are subclasses of RuntimeException.


Question 15

Which exception is thrown when an arithmetic error occurs, such as dividing by zero?


A) ArithmeticException


B) ArrayStoreException


C) NumberFormatException


D) NullPointerException


Answer: A) ArithmeticException


Question 16

Which of the following statements about unchecked exceptions is false?


A) Unchecked exceptions extend RuntimeException.


B) They can be handled using try-catch blocks.


C) They are checked by the compiler.


D) They represent programming errors that could have been prevented.


Answer: C) They are checked by the compiler.


Question 17

Which unchecked exception indicates that an operation requires a positive integer, but a negative value is provided?


A) IllegalArgumentException


B) NegativeArraySizeException


C) NumberFormatException


D) ArithmeticException


Answer: B) NegativeArraySizeException


Question 18

What does the ArrayStoreException unchecked exception indicate in Java?


A) An attempt to store the wrong type of object into an array.


B) An attempt to access an invalid array index.


C) An attempt to store a null value in an array.


D) An attempt to resize an array.


Answer: A) An attempt to store the wrong type of object into an array.


Question 19

What will be the output if the following code is executed?


java

Copy code

int[] arr = new int[5];

arr[5] = 10;

A) The value 10 is stored at the 5th index of the array.


B) A compilation error occurs.


C) An ArrayIndexOutOfBoundsException is thrown.


D) The program runs without errors.


Answer: C) An ArrayIndexOutOfBoundsException is thrown.


Question 20

Why is it not necessary to declare unchecked exceptions in a method's throws clause?


A) They are considered less important than checked exceptions.


B) They can never be caught.


C) They are typically caused by programming errors and can occur anywhere.


D) They do not have any impact on program execution.


Answer: C) They are typically caused by programming errors and can occur anywhere.


No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...