Java Questions & Answers – Try & Catch

This section of our 1000+ Java MCQs focuses 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
View Answer

Answer: d
Explanation: None.

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

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: None.
advertisement
advertisement

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

Answer: c
Explanation: 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
View Answer

Answer: d
Explanation: try must be followed by either catch or finally block.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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
View Answer

Answer: b
Explanation: None.
Output:

advertisement
$ javac Output.javac
java Output
World

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

advertisement
  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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: d
Explanation: 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

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.