Java Multiple Choice Questions & Answers on Throw, Throws & Nested Try for Freshers

1. Which of these keywords is used to generate an exception explicitly?

a) try
b) finally
c) throw
d) catch
Answer: c
2. Which of these class is related to all the exceptions that are explicitly thrown?

a) Error
b) Exception
c) Throwable
d) Throw
Answer: c
3. Which of these operator is used to generate an instance of an exception than can be thrown by using throw?

a) new
b) malloc
c) alloc
d) thrown
Answer: a

Explanation: new is used to create instance of an exception. All of java’s built in run-time exceptions have two constructors : one with no parameters and one that takes a string parameter.
4. Which of these handles the exception when no catch is used?

a) Default handler
b) finally
c) throw handler
d) Java run time system
Answer: a
5. Which of these keywords is used to by the calling function to guard against the exception that is thrown by called function?

a) try
b) throw
c) throws
d) catch
Answer: c

Explanation: If a method is capable of causing an exception that it does not handle. It must specify this behaviour the behaviour so that callers of the method can guard themselves against that exception. This is done by using throws clause in methods declaration.
6. What is the output of this program?
  1.     class exception_handling {
  2.         public static void main(String args[]) {
  3.             try {
  4.                 int a = args.length;
  5.                 int b = 10 / a;
  6.                 System.out.print(a);
  7.                 try {
  8.                     if (a == 1)
  9.                         a = a / a - a;
  10.                     if (a == 2) {
  11.                         int []c = {1};
  12.                         c[8] = 9;
  13.                     }
  14.                 }
  15.                 catch (ArrayIndexOutOfBoundException e) {
  16.                     System.out.println("TypeA");
  17.                 }
  18.                 catch (ArithmeticException e) {
  19.                     System.out.println("TypeB");
  20.                 }
  21.             }
  22.         }
  23.     }
a) TypeA
b) TypeB
c) Compiler Time Error
d) 0TypeB
Answer: c

Explanation: Because we can’t go beyond array limit
7. What is the output of this program?
  1.     class exception_handling {
  2.         public static void main(String args[]) {
  3.             try {
  4.                 System.out.print("A");
  5.                 throw new NullPointerException ("Hello");
  6.             }
  7.             catch(ArithmeticException e) {
  8.                 System.out.print("B");         
  9.             }
  10.         }
  11.     }
a) A
b) B
c) Hello
d) Runtime Exception
Answer: d
8. What is the output of this program?
  1.     class exception_handling {
  2.         static void throwexception() throws ArithmeticException {        
  3.             System.out.print("0");
  4.             throw new ArithmeticException ("Exception");
  5.         }
  6.         public static void main(String args[]) {
  7.             try {
  8.                 throwexception();
  9.             }
  10.             catch (ArithmeticException e) {
  11.                 System.out.println("A");
  12.             }
  13.         }
  14.     }
a) A
b) 0
c) 0A
d) Exception
Answer: c
9. What is the output of this program?
  1. public class San 
  2.  {  
  3.     public static void main(String[] args) 
  4.     {
  5.         try 
  6.         { 
  7.             return; 
  8.         } 
  9.         finally 
  10.         {
  11.             System.out.println( "Finally" ); 
  12.         } 
  13.     } 
  14. }
a) Finally
b) Compilation fails
c) The code runs with no output
d) An exception is thrown at runtime
Answer: a

Explanation: Because finally will execute always.
10. What is the output of this program?
  1. public class San 
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         try 
  6.         {
  7.             System.out.print("Hello world ");
  8.         }
  9.         finally 
  10.         {
  11.             System.out.println("Finally executing ");
  12.         }
  13.     }
  14. }
a) The program will not compile because no exceptions are specified
b) The program will not compile because no catch clauses are specified
c) Hello world
d) Hello world Finally executing
Answer: d

Related

Multiple Choice Questions 846054815783881031

Post a Comment

emo-but-icon

item