Question: 6 -
Which exception is thrown when divide by zero statement executes?
-
None of these
-
ArithmeticException
-
NumberFormatException
-
NullPointerException
Answer:
ArithmeticException
Solution:
ArithmeticException is thrown when divide by zero statement executes.
ArithmeticException is thrown when divide by zero statement executes.
Question: 7 -
What will be the output of the program?public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod()
{
throw new Error(); /* Line 22 */
}
}
-
C is printed before exiting with an error message
-
Compilation fails
-
ABCD
-
BC is printed before exiting with an error message
Answer:
C is printed before exiting with an error message
Solution:
Error is thrown but not recognised line(22) because the only catch attempts to catch an Exception and Exception is not a superclass of Error. Therefore only the code in the finally statement can be run before exiting with a runtime error (Exception in thread "main" java.lang.Error).
Error is thrown but not recognised line(22) because the only catch attempts to catch an Exception and Exception is not a superclass of Error. Therefore only the code in the finally statement can be run before exiting with a runtime error (Exception in thread "main" java.lang.Error).
Question: 8 -
In which of the following package Exception class exist?
-
java.lang
-
java.file
-
java.util
-
java.io
Answer:
java.lang
Solution not available.
Question: 9 -
Which of these is a super class of all errors and exceptions in the Java language?
-
Throwable
-
RunTimeExceptions
-
None of the above
-
Catchable
Answer:
Throwable
Solution:
Throwable is a super class of all errors and exceptions in the Java language
Throwable is a super class of all errors and exceptions in the Java language
Question: 10 -
What will be the output of the program?public class Foo
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}
-
Compilation fails
-
The code runs with no output
-
Finally
-
An exception is thrown at runtime
Answer:
Finally