Java MCQs on creating exceptions in Java Programming Language.
1. Which of these classes is used to define exceptions?
a) Exception
b) Throwable
c) Abstract
d) System
Answer: a
Clarification: None.
advertisement
2. Which of these methods return description of an exception?
a) getException()
b) getMessage()
c) obtainDescription()
d) obtainException()
Answer: b
Clarification: 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
Clarification: None.
4. Which of these methods return localized description of an exception?
a) getLocalizedMessage()
b) getMessage()
c) obtainLocalizedMessage()
d) printLocalizedMessage()
Answer: a
Clarification: None.
5. Which of these classes is super class of Exception class?
a) Throwable
b) System
c) RunTime
d) Class
Answer: a
Clarification: None.
6. What will be the output of the following Java code?
-
class Myexception extends Exception
-
{
-
int detail;
-
Myexception(int a)
-
{
-
detail = a;
-
}
-
public String toString()
-
{
-
return "detail";
-
}
-
}
-
class Output
-
{
-
static void compute (int a) throws Myexception
-
{
-
throw new Myexception(a);
-
}
-
public static void main(String args[])
-
{
-
try
-
{
-
compute(3);
-
}
-
catch(Myexception e)
-
{
-
System.out.print("Exception");
-
}
-
}
-
}
a) 3
b) Exception
c) Runtime Error
d) Compilation Error
Answer: b
Clarification: Myexception is self defined exception.
Output:
advertisement
$ javac Output.java java Output Exception
7. What will be the output of the following Java code?
-
class Myexception extends Exception
-
{
-
int detail;
-
Myexception(int a)
-
{
-
detail = a;
-
}
-
public String toString()
-
{
-
return "detail";
-
}
-
}
-
class Output
-
{
-
static void compute (int a) throws Myexception
-
{
-
throw new Myexception(a);
-
}
-
public static void main(String args[])
-
{
-
try
-
{
-
compute(3);
-
}
-
catch(DevideByZeroException e)
-
{
-
System.out.print("Exception");
-
}
-
}
-
}
a) 3
b) Exception
c) Runtime Error
d) Compilation Error
Answer: c
Clarification: Mexception is self defined exception, we are generating Myexception but catching DevideByZeroException which causes error.
Output:
$ javac Output.javac
8. What will be the output of the following Java code?
advertisement
-
class exception_handling
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
throw new NullPointerException ("Hello");
-
System.out.print("A");
-
}
-
catch(ArithmeticException e)
-
{
-
System.out.print("B");
-
}
-
}
-
}
a) A
b) B
c) Compilation Error
d) Runtime Error
Answer: d
Clarification: 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.
Output:
$ javac exception_handling.java $ java exception_handling Exception in thread "main" java.lang.NullPointerException: Hello
9. What will be the output of the following Java code?
-
class Myexception extends Exception
-
{
-
int detail;
-
Myexception(int a)
-
{
-
detail = a;
-
}
-
public String toString()
-
{
-
return "detail";
-
}
-
}
-
class Output
-
{
-
static void compute (int a) throws Myexception
-
{
-
throw new Myexception(a);
-
}
-
public static void main(String args[])
-
{
-
try
-
{
-
compute(3);
-
}
-
catch(Exception e)
-
{
-
System.out.print("Exception");
-
}
-
}
-
}
a) 3
b) Exception
c) Runtime Error
d) Compilation Error
Answer: b
Clarification: Myexception is self defined exception.
Output:
$ javac Output.javac java Output Exception
10. What will be the output of the following Java code?
advertisement
-
class exception_handling
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a = args.length;
-
int b = 10 / a;
-
System.out.print(a);
-
try
-
{
-
if (a == 1)
-
a = a / a - a;
-
if (a == 2)
-
{
-
int c = {1};
-
c[8] = 9;
-
}
-
}
-
catch (ArrayIndexOutOfBoundException e)
-
{
-
System.out.println("TypeA");
-
}
-
catch (ArithmeticException e)
-
{
-
System.out.println("TypeB");
-
}
-
}
-
}
-
}
Note : Execution command line : $ java exception_handling one
a) TypeA
b) TypeB
c) Compilation Error
d) Runtime Error
Answer: c
Clarification: try without catch or finally
Output:
$ javac exception_handling.java $ java exception_handling error: 'try' without 'catch', 'finally' or resource declarations