SCJP : Exception Mock Exam Practice Questions
Questions no -1
What is the output for the below code ?
public class SuperClass {
public int doIt(String str, Integer... data)throws ArrayIndexOutOfBoundsException{
String signature = "(String, Integer[])";
System.out.println(str + " " + signature);
return 1;
}
}
public class SubClass extends SuperClass{
public int doIt(String str, Integer... data) throws Exception
{
String signature = "(String, Integer[])";
System.out.println("Overridden: " + str + " " + signature);
return 0;
}
public static void main(String... args)
{
SuperClass sb = new SubClass();
try{
sb.doIt("hello", 3);
}catch(Exception e){
}
}
}
options
A)Overridden: hello (String, Integer[])
B)hello (String, Integer[])
C)This code throws an Exception at Runtime
D)Compile with error
Correct answer is : D
Explanations : Exception Exception is not compatible with throws clause in SuperClass.doIt(String, Integer[]).
The same exception or subclass of that exception is allowed.
Questions no -2
What is the result of executing the following code, using the parameters 0 and 3 ?
public void divide(int a, int b) {
try {
int c = a / b;
} catch (Exception e) {
System.out.print("Exception ");
} finally {
System.out.println("Finally");
}
options
A)Prints out: Exception Finally
B)Prints out: Finally
C)Prints out: Exception
D)Compile with error
Correct answer is : D
Explanations : finally block always executed whether exception occurs or not.
0/3 = 0 Does not throws exception.
Questions no -3
Which of the below statement is true about Error?
options
A)An Error is a subclass of Throwable
B)An Error is a subclass of Exception
C)Error indicates serious problems that a reasonable application should not try to catch.
D)An Error is a subclass of IOException
Correct answer is : A and C
Explanations : An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.
Questions no -4
Which of the following is type of RuntimeException?
options
A)IOException
B)ArrayIndexOutOfBoundsException
C)Exception
D)Error
Correct answer is : B
Explanations : Below is the tree.
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.IndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
|