Exceptional Handling Basics Questions & Answers

Java Questions & Answers – Exceptional Handling Basics

This section of our 1000+ Java MCQs focuses on exception handling of Java Programming Language.

1. What will be the output of the following Java program?

  1.     class exception_handling
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             try
  6.             {
  7.                 System.out.print("Hello" + " " + 1 / 0);
  8.             }
  9.             catch(ArithmeticException e)
  10.             {
  11.         	System.out.print("World");
  12.             }
  13.         }
  14.     }

a) Hello
b) World
c) HelloWorld
d) Hello World

Explanation: System.ou.print() function first converts the whole parameters into a string and then prints, before “Hello” goes to output stream 1 / 0 error is encountered which is cached by catch block printing just “World”.
Output: 

$ javac exception_handling.java
$ java exception_handling
World

2. What will be the output of the following Java program?

  1.     class exception_handling
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             try
  6.            {
  7.                 int a, b;
  8.                 b = 0;
  9.                 a = 5 / b;
  10.                 System.out.print("A");
  11.             }
  12.             catch(ArithmeticException e)
  13.             {
  14.         	System.out.print("B");
  15.             }
  16.         }
  17.     }

a) A
b) B
c) Compilation Error
d) Runtime Error
Explanation: None.
Output:

 

$ javac exception_handling.java
$ java exception_handling
B

3. What will be the output of the following Java program?

  1.     class exception_handling
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             try
  6.             {
  7.                 int a, b;
  8.                 b = 0;
  9.                 a = 5 / b;
  10.                 System.out.print("A");
  11.             }
  12.             catch(ArithmeticException e)
  13.             {
  14.         	System.out.print("B");
  15.             }
  16.             finally
  17.             {
  18.     	        System.out.print("C");
  19.             }
  20.         }
  21.     }

a) A
b) B
c) AC
d) BC

Explanation:Before the try and catch block ends, the finally keyword is used to execute the code.
Output:

 

$ javac exception_handling.java
$ java exception_handling
BC

4. What will be the output of the following Java program?

  1.     class exception_handling
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             try
  6.             {
  7.                 int i, sum;
  8.                 sum = 10;
  9.                 for (i = -1; i < 3 ;++i)
  10.                     sum = (sum / i);
  11.             }
  12.             catch(ArithmeticException e)
  13.             {
  14.         	System.out.print("0");
  15.             }
  16.             System.out.print(sum);
  17.         }
  18.     }

a) 0
b) 05
c) Compilation Error
d) Runtime Error

Explanation: The value of the variable sum is written outside of the try block; sum is only declared in the try block, and it is undefined outside of it.
Output:

 

$ javac exception_handling.java
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	sum cannot be resolved to a variable

5. When does Exceptions in Java arises in code sequence?
a) Run Time
b) Compilation Time
c) Can Occur Any Time
d) None of the mentioned

Explanation: Exceptions in Java are run-time errors.

6. Which of these keywords is not a part of exception handling?
a) try
b) finally
c) thrown
d) catch

Explanation: Exceptional handling is managed via 5 keywords – try, catch, throws, throw and finally.

7. Which of these keywords must be used to monitor for exceptions?
a) try
b) finally
c) throw
d) catch

Explanation: None.

8. Which of these keywords must be used to handle the exception thrown by try block in some rational manner?
a) try
b) finally
c) throw
d) catch

Explanation: If an exception occurs within the try block, it is thrown and cached by catch block for processing.

9. Which of these keywords is used to manually throw an exception?
a) try
b) finally
c) throw
d) catch

Explanation: None.

The three keywords try, catch, and throw are used to handle exceptions in C++. When a programme encounters a problem, it throws an exception. The throw keyword is used to accomplish this. An exception handler in a programme captures an exception at the point in the programme where you wish to handle the problem. Exception handling is the process of responding to the occurrence of exceptions – abnormal or unusual conditions requiring special processing – during the execution of a programme in computers and computer programming.

Leave a Reply

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