Java Multiple Choice Questions & Answers on Try & Catch for Freshers

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

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
Answer: b
4. Which of these keywords are used for generating an exception manually?

a) try
b) catch
c) throw
d) check
Answer: c
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

Explanation: try must be followed by either catch or finally block.
6. What is the output of this program?
  1.     class Output {
  2.         public static void main(String args[]) {
  3.            try {
  4.                int a = 0;
  5.                int b = 5;
  6.                int c = b / a;
  7.                System.out.print("Hello");
  8.            }
  9.            catch(Exception e) {
  10.                System.out.print("World");
  11.            } 
  12.         }
  13.     }
a) Hello
b) World
c) HelloWOrld
d) Compilation Error
Answer: b
7. What is the output of this program?
  1.     class Output {
  2.         public static void main(String args[]) {
  3.            try {
  4.                int a = 0;
  5.                int b = 5;
  6.                int c = a / b;
  7.                System.out.print("Hello");
  8.            }
  9.            catch(Exception e) {
  10.                System.out.print("World");
  11.            } 
  12.         }
  13.     }
a) Hello
b) World
c) HelloWOrld
d) Compilation Error
Answer: a
8. What is the output of this program?
  1.     class Output {
  2.         public static void main(String args[]) {
  3.            try {
  4.                int a = 0;
  5.                int b = 5;
  6.                int c = b / a;
  7.                System.out.print("Hello");
  8.            } 
  9.         }
  10.     }
a) Hello
b) World
c) HelloWOrld
d) Compilation Error
Answer: d

Explanation: try must be followed by either catch or finally
9. What is the output of this program?
  1.     class Output {
  2.         public static void main(String args[]) {
  3.            try {
  4.                int a = 0;
  5.                int b = 5;
  6.                int c = a / b;
  7.                System.out.print("Hello");
  8.            }
  9.            finally {
  10.                System.out.print("World");
  11.            } 
  12.         }
  13.     }
a) Hello
b) World
c) HelloWOrld
d) Compilation Error
Answer: c

Explanation: finally block is always executed after try block, no matter exception is found or not.
10. What is the output of this program?
  1.     class Output {
  2.         public static void main(String args[]) {
  3.            try {
  4.                int a = 0;
  5.                int b = 5;
  6.                int c = b / a;
  7.                System.out.print("Hello");
  8.            }
  9.            catch(Exception e) {
  10.                System.out.print("World");
  11.            } 
  12.            finally {
  13.                System.out.print("World");
  14.            } 
  15.         }
  16.     }
a) Hello
b) World
c) HelloWOrld
d) WorldWorld
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.

Related

Multiple Choice Questions 7748771817171636428

Post a Comment

emo-but-icon

item