Welcome Guest | Sign in | Register

Home > Java Programming > Exceptions > Questions and Answers

01. try{
File f = new File("a.txt");
}catch(Exception e){
}catch(IOException io){
}

Does this code create new file name a.txt ?
A. True B. False
C. Compilation Error D. None

Answer and Explanation

Answer: Compilation Error

Explanation:
IOException is unreachable to compiler because all exception is going to catch by Exception block.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. 1. public class A {
2. public void method1() {
3. try {
4. B b = new B();
5. b.method2();
6. // more code here
7. } catch (TestException te) {
8. throw new RuntimeException(te);
9. }
10. }
11. }

1. public class B {
2. public void method2() throws TestException {
3. // more code here
4. }
5. }
6. public class TestException extends Exception {
7. }
Given:
1. public void method() {
2. A a = new A();
3. a.method1();
4. }

Which statement is true if a TestException is thrown on line 3 of class B?
A. Line 3 must be called within a try block. B. The exception thrown by method1 in class A is not required to be caught.
C. The method declared on line 1 must be declared to throw a RuntimeException. D. On line 5 of class A, the call to method2 of class B does not need to be placed in a try/catch block

Answer and Explanation

Answer: The exception thrown by method1 in class A is not required to be caught.

Explanation:
Normal flow of execution.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. 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){

}

}

}
A. Overridden: hello (String, Integer[]) B. hello (String, Integer[])
C. This code throws an Exception at Runtime D. Compile with error

Answer and Explanation

Answer: Compile with error

Explanation:
Exception Exception is not compatible with throws clause in SuperClass.doIt(String, Integer[]). The same exception or subclass of that exception is allowed.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. 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");
}
A. Prints out: Exception Finally B. Prints out: Finally
C. Prints out: Exception D. Compile with error

Answer and Explanation

Answer: Compile with error

Explanation:
 Finally block always executed whether exception occurs or not.

0/3 = 0 Does not throws exception.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. Which are most typically thrown by an API developer or an application developer as opposed to being thrown by the JVM? (Choose all that apply.)
A. ClassCastException
B. IllegalStateException
C. NumberFormatException
D. IllegalArgumentException
E. ExceptionInInitializerError
A. ABC B. BDE
C. BCD D. ADE

Answer and Explanation

Answer: BCD

Explanation:
B is typically used to report an environment problem such as trying to access a resource that’s closed. C is often thrown in API methods that attempt
to convert poorly formed String arguments to numeric values. D is often thrown in API
methods that receive poorly formed arguments.
-- A and E are thrown by the JVM.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. 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){

}
}
}
A. Overridden: hello (String, Integer[]) B. hello (String, Integer[])
C. This code throws an Exception at Runtime D. Compile with error

Answer and Explanation

Answer: Compile with error

Explanation:
Exception is not compatible with throws clause in SuperClass.doIt(String, Integer[]).
The same exception or subclass of that exception is allowed.


Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. Which of the below statement is true about Error?

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
A. AC B. AD
C. BC D. BD

Answer and Explanation

Answer: AC

Explanation:
 An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. Which of the following is type of RuntimeException?
A. IOException B. ArrayIndexOutOfBoundsException
C. Exception D. Error

Answer and Explanation

Answer: ArrayIndexOutOfBoundsException

Explanation:
Below is the tree.
java.lang.Object
 java.lang.Throwable
  java.lang.Exception
   java.lang.RuntimeException
    java.lang.IndexOutOfBoundsException
     java.lang.ArrayIndexOutOfBoundsException

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. The program given below will test your understanding of Float primitive data types and functions in Java program.
Given below the sample code :
public class SuperClass {
public static void main(String[] args) {
System.out.println(stringConvert("0.3"));
System.out.println(stringConvert("0.3A"));
System.out.println(stringConvert(null));
}

public static boolean stringConvert(String s) {

float factor = 0;
try {
factor = Float.valueOf(s).floatValue();
return true;
} catch (NumberFormatException e) {
System.out.println("Exception in number formatting " + s);
factor = Float.NaN;
} finally {
System.out.println("Finally");
}
return false;
}

}
Find the output of the following code ?
A. true Finally B. true Exception in number formatting 0.3A Finally 
C. true Finally False D. Prints : "Finally" and give error message.

Answer and Explanation

Answer: Prints : "Finally" and give error message.

Explanation:
Because the number "0.3A" is not a valid number and also error occurs in converting null to string.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10. Given:

31. // some code here
32. try {
33. // some code here
34. } catch (SomeException se) {
35. // some code here
36. } finally {
37. // some code here
38. }

Under which three circumstances will the code on line 37 be executed? (Choose three.)
A. The instance gets garbage collected.
B. The code on line 33 throws an exception.
C. The code on line 35 throws an exception.
D. The code on line 31 throws an exception.
E. The code on line 33 executes successfully.

A. BCD B. ABC
C. CDE D. BCE

Answer and Explanation

Answer: BCE

Explanation:
As we know if no exception occurs (means in 33 line) then directly finally block, if exception occurs then try ,catch ,finally (means line 33 as well as 35). Or else directly after 31 line finally block will execute. So A) is correct.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.