250+ TOP MCQs on Try & Catch and Answers

Java MCQs on try and catch in Java Programming Language.

1. What is the use of try & catch?
a) It allows us to manually handle the exception
b) It allows to fix errors
c) It prevents automatic terminating of the program in cases when an exception occurs
d) All of the mentioned

Answer: d
Clarification: None.

2. Which of these keywords are used for the block to be examined for exceptions?
a) try
b) catch
c) throw
d) check

Answer: a
Clarification: try is used for the block that needs to checked for exception.

3. Which of these keywords are used for the block to handle the exceptions generated by try block?
a) try
b) catch
c) throw
d) check

Answer: b
Clarification: None.

4. Which of these keywords are used for generating an exception manually?
a) try
b) catch
c) throw
d) check

Answer: c
Clarification: None.

5. Which of these statements is incorrect?
a) try block need not to be followed by catch block
b) try block can be followed by finally block instead of catch block
c) try can be followed by both catch and finally block
d) try need not to be followed by anything

Answer: d
Clarification: try must be followed by either catch or finally block.

6. What will be the output of the following Java code?

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

a) Hello
b) World
c) HelloWOrld
d) Compilation Error

Answer: b
Clarification: None.
Output:

$ javac Output.javac
java Output
World

7. What will be the output of the following Java code?

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

a) Hello
b) World
c) HelloWOrld
d) Compilation Error

Answer: a
Clarification: None.
Output:

$ javac Output.javac
java Output
Hello

8. What will be the output of the following Java code?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.            try 
  6.            {
  7.                int a = 0;
  8.                int b = 5;
  9.                int c = b / a;
  10.                System.out.print("Hello");
  11.            } 
  12.         }
  13.     }

a) Hello
b) World
c) HelloWOrld
d) Compilation Error

Answer: d
Clarification: try must be followed by either catch or finally
Output:

$ javac Output.javac
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Syntax error, insert "Finally" to complete BlockStatements

9. What will be the output of the following Java code?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.            try 
  6.            {
  7.                int a = 0;
  8.                int b = 5;
  9.                int c = a / b;
  10.                System.out.print("Hello");
  11.            }
  12.            finally 
  13.            {
  14.                System.out.print("World");
  15.            } 
  16.         }
  17.     }

a) Hello
b) World
c) HelloWOrld
d) Compilation Error

Answer: c
Clarification: finally block is always executed after try block, no matter exception is found or not.
Output:

$ javac Output.javac
java Output
HelloWorld

10. What will be the output of the following Java code?

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

a) Hello
b) World
c) HelloWOrld
d) WorldWorld

Answer: d
Clarification: finally block is always executed after tryblock, no matter exception is found or not. catch block is executed only when exception is found. Here divide by zero exception is found hence both catch and finally are executed.
Output:

$ javac Output.javac
java Output
WorldWorld

Leave a Reply

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