MCQ

Java MCQ – Exception Handling – Part 1

This collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Exception Handling in Java”.
 

1. When do exceptions occur in Java code?

A At the time of execution

B At the time of compilation

C Can occur at any time

D None of the above

A
Exceptions are runtime errors.

 

 

2. Which of these keywords is not part of exception handling?

A catch

B thrown

C finally

D try

B
Exception handling is managed via 5 keywords: try, catch, throw, throws and finally.

 

 

3. Exception is a(n) __________

A Class

B Interface

C Abstract class

D Other

A

 

 

4. In which package in Java, we can find the Exception class?

A java.lang

B java.util

C java.io

D java.awt

A

 

 

5. Exception was introduced in which version of Java?

A Java 1

B Java 2

C Java 3

D Java 4

A

 

 

6. Which of these classes is the highest in the hierarchy in Java?

A java.lang.Exception

B java.lang.Object

C java.lang.Throwable

D java.lang.Error

B


 

 

7. Which of the following keyword is used to explicitly raise an exception?

A raise

B catch

C throw

D throws

C
throw is used to explicitly raise an exception in java. Example:

if(age < 18)  
    throw new ArithmeticException("not valid");

 

 

8. What is the output of the following code?
public class Main 
{
	public static void main(String args[]) 
	{
		try 
		{
			System.out.print("Calculate:" + " " + 1 / 0);
		}
		catch(ArithmeticException e) 
		{
			System.out.print("Exception: Division by zero");        	
		}
	}
}

A Calculate:

B Calculate:Exception: Division by zero

C Exception: Division by zero

D Exception: Division by zeroCalculate:

C
System.out.print() function first converts all parameters to a string, then prints it, before the string “Calculate:” passes to the output stream, an error 1/0 is encountered which is caught by the catch block and displays the string “Exception: Division by zero”.

 

 

9. Which of the following is a parent class of Error?

A Iterable

B Throwable

C Exception

D throws

B
Throwable class is Error’s parent class.


 

 

10. Which of the following statements is correct?
  1. The exception is unrecoverable.
  2. The error is recoverable by debugging.

A 1

B 2

C 1 and 2

D neither 1 nor 2

D
  1. There are two types of exceptions: recoverable and unrecoverable.
  2. The error is unrecoverable.

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *