Java Multiple Choice Questions & Answers on Creating Exceptions for Freshers

1. Which of these classes is used to define exceptions?

a) Exception
b) Trowable
c) Abstract
d) System
Answer: a
2. Which of these methods return description of an exception?

a) getException()
b) getMessage()
c) obtainDescription()
d) obtainException()
Answer: b

Explanation: getMessage() returns a description of the exception.
3. Which of these methods is used to print stack trace?

a) obtainStackTrace()
b) printStackTrace()
c) getStackTrace()
d) displayStackTrace()
Answer: b
4. Which of these methods return localized description of an exception?

a) getLocalizedMessage()
b) getMessage()
c) obtainLocalizedMessage()
d) printLocalizedMessage()
Answer: a
5. Which of these classes is super class of Exception class?

a) Throwable
b) System
c) RunTime
d) Class
Answer: a
6. What is the output of this program?
  1.     class Myexception extends Exception {
  2.  int detail;
  3.         Myexception(int a) {
  4.             detail = a;
  5.  }
  6.  public String toString() {
  7.      return "detail";
  8.  }
  9.     }
  10.     class Output {
  11.         static void compute (int a) throws Myexception {
  12.       throw new Myexception(a);  
  13.  }
  14.  public static void main(String args[]) {
  15.             try {
  16.                 compute(3);
  17.             }
  18.            catch(Myexception e) {
  19.                System.out.print("Exception");
  20.            } 
  21.         }
  22.     }
a) 3
b) Exception
c) Runtime Error
d) Compilation Error
Answer: b

Explanation: Myexception is self defined exception.
7. What is the output of this program?
  1.     class Myexception extends Exception {
  2.  int detail;
  3.         Myexception(int a) {
  4.         detail = a;
  5.  }
  6.  public String toString() {
  7.      return "detail";
  8.  }
  9.     }
  10.     class Output {
  11.         static void compute (int a) throws Myexception {
  12.       throw new Myexception(a);  
  13.  }
  14.  public static void main(String args[]) {
  15.             try {
  16.                 compute(3);
  17.             }
  18.            catch(DevideByZeroException e) {
  19.                System.out.print("Exception");
  20.            } 
  21.         }
  22.     }
a) 3
b) Exception
c) Runtime Error
d) Compilation Error
Answer: c

Explanation: Mexception is self defined exception, we are generating Myexception but catching DevideByZeroException which causes error.
8. What is the output of this program?
  1.     class exception_handling {
  2.         public static void main(String args[]) {
  3.             try {
  4.                 throw new NullPointerException ("Hello");
  5.                 System.out.print("A");
  6.             }
  7.             catch(ArithmeticException e) {
  8.          System.out.print("B");         
  9.             }
  10.         }
  11.     }
a) A
b) B
c) Compilation Error
d) Runtime Error
Answer: d

Explanation: try block is throwing NullPointerException but the catch block is used to counter Arithmetic Exception. Hence NullPointerException occurs since no catch is there which can handle it, runtime error occurs.
9. What is the output of this program?
  1.     class Myexception extends Exception {
  2.  int detail;
  3.         Myexception(int a) {
  4.         detail = a;
  5.  }
  6.  public String toString() {
  7.      return "detail";
  8.  }
  9.     }
  10.     class Output {
  11.         static void compute (int a) throws Myexception {
  12.       throw new Myexception(a);  
  13.  }
  14.  public static void main(String args[]) {
  15.             try {
  16.                 compute(3);
  17.             }
  18.            catch(Exception e) {
  19.                System.out.print("Exception");
  20.            } 
  21.         }
  22.     }
a) 3
b) Exception
c) Runtime Error
d) Compilation Error
Answer: b

Explanation: Myexception is self defined exception.
10. 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.     }
a) TypeA
b) TypeB
c) Compilation Error
d) Runtime Error
Note : Execution commandline : $ java exception_handling one
Answer: c

Explanation: try without catch or finally

Related

C Programming Questions and Answers on For Loop for Freshers

1. The following code ‘for(;;)’ represents an infinite loop. It can be terminated by. a) breakb) exit(0)c) abort()d) all of the mentioned Answer: a 2. The correct syntax for running two variable ...

CSS Multiple Choice Questions & Answers on CSS Text for Freshers

1. Which of the following CSS property is used to set the text formatting? a) fontb) font-stylec) text-decorationd) all of the mentioned Answer: c 2. Which of the following sets the color of any ...

CSS Multiple Choice Questions & Answers on CSS border for Freshers

1. Which of the following CSS property defines the different properties of all four sides of an element’s border in a single declaration? a) borderb) paddingc) border-collapsed) border-width Answe...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item