Showing posts with label Exceptions Handing. Show all posts
Showing posts with label Exceptions Handing. Show all posts

Exception Handling Ques: 03

Exception Handling
Ques: 03 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.


Answer: B, C, E

Exception Handling Ques: 02

Exception Handling

Ques: 02 Given:

84. try {
85. ResourceConnection con = resourceFactory.getConnection();
86. Results r = con.query("GET INFO FROM CUSTOMER");
87. info = r.getData();
88. con.close();
89. } catch (ResourceException re) {
90. errorLog.write(re.getMessage());
91. }

92. return info;
Which statement is true if a ResourceException is thrown on line 86?
A. Line 92 will not execute.
B. The connection will not be retrieved in line 85.
C. The resource connection will not be closed on line 88.
D. The enclosing method will throw an exception to its caller.


Answer: C

Exception Handling Ques: 01

Exception Handling

Q: 01 Given:

11. public static void parse(String str) {
12. try {
13. float f = Float.parseFloat(str);
14. } catch (NumberFormatException nfe) {
15. f = 0;
16. } finally {
17. System.out.println(f);
18. }
19. }
20. public static void main(String[] args) {
21. parse("invalid");
22. }

What is the result?
A. 0.0
B. Compilation fails.
C. A ParseException is thrown by the parse method at runtime.
D. A NumberFormatException is thrown by the parse method at runtime.


Answer: B

Exception Handling - 2

Ques 2:
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

Answer: 
-- B , C, and D are correct. 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.

Exceptions Handing -1

Que 1:
class Input {
   public static void main(String[] args) {
     String s = "-";
     try {
          doMath(args[0]);
          s += "t "; // line 6
      }
      finally { System.out.println(s += "f "); }
    }
   public static void doMath(String a) {
       int y = 7 / Integer.parseInt(a);
   } 
}

And the command-line invocations:
java Input
java Input 0
Which are true? (Choose all that apply.)
A. Line 6 is executed exactly 0 times.
B. Line 6 is executed exactly 1 time.
C. Line 6 is executed exactly 2 times.
D. The finally block is executed exactly 0 times.
E. The finally block is executed exactly 1 time.
F. The finally block is executed exactly 2 times.
G. Both invocations produce the same exceptions.
H. Each invocation produces a different exception.
Answer:
-> A , F, and H are correct. Since both invocations throw exceptions, line 6 is never reached. Since both exceptions occurred within a try block, the finally block will always execute. The first invocation throws an   ArrayIndexOutOfBoundsException, and the second invocation throws an ArithmeticException for the attempt to divide by zero.
-> B, C, D, E, and G are incorrect based on the above.