Can you please explain why answer is A and not D ?? 10. What is the result when you compile the and run the following code? public class ThrowsDemo { static void throwMethod() { System.out.println("Inside throwMethod."); throw new IllegalAccessException("demo"); } public static void main(String args[]) { try { throwMethod(); } catch (IllegalAccessException e) { System.out.println("Caught " + e); } } } A) Compilation error B) Runtime error C) Compile successfully, nothing is printed. D) Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption: demo Answer 10: A) Compilation error
------------- Can you please explain why answer is C and not A or B?
20. What is the output of the following code? public class TestLocal { public static void main(String args[]) { String s[] = new String[6]; System.out.print(s[6]); } } A) A null is printed B) Compile time error C) Exception is thrown D) null followed by 0 is printed on the screen Answer 20: C) Exception is thrown thanx [ September 17, 2002: Message edited by: Richa Jeetah ]
smita raval
Greenhorn
Joined: Aug 31, 2002
Posts: 28
posted
0
Hi, For Q:20 Because Array is length of 6. i.e. [0] to [5].. When it's going to print [6] th element, it throws ArrayIndexOutOfBoundsException.. [ September 17, 2002: Message edited by: skraval ] [ September 17, 2002: Message edited by: skraval ]
Smita Raval
Ron Newman
Ranch Hand
Joined: Jun 06, 2002
Posts: 1056
posted
0
Run it and you'll see why. You're going off the end of the array.
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Shishio San
Ranch Hand
Joined: Aug 29, 2002
Posts: 223
posted
0
Hi, With s[6] The value of the index is equal to s.lenght and thus you're trying to access a value ouside the bounds (min=0, max=5) and that's why an exception is throw and more prcisely ArrayIndexOutOfBoundsException
Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Richa Jeetah
Greenhorn
Joined: Sep 27, 2001
Posts: 29
posted
0
Originally posted by skraval: Hi, For Q:20 Because Array is length of 6. i.e. [0] to [5].. When it's going to print [6] th element, it throws NullPointerException.. [ September 17, 2002: Message edited by: skraval ]
And if it were [5] then correct answer would have been A ??
[ September 17, 2002: Message edited by: Richa Jeetah ]
Ron Newman
Ranch Hand
Joined: Jun 06, 2002
Posts: 1056
posted
0
For Q10: IllegalAccessException is a checked exception, but you have not declared it in a throws clause. For Q20: I strongly recommend that you compile and run this code, then edit it and compile and run it again. That's the best way to understand what it does. [ September 17, 2002: Message edited by: Ron Newman ]
Richa Jeetah
Greenhorn
Joined: Sep 27, 2001
Posts: 29
posted
0
i ran the code for Q20 and Q10, now its clear to me. Thank You
smita raval
Greenhorn
Joined: Aug 31, 2002
Posts: 28
posted
0
Richa, "And if it were [5] then correct answer would have been A ??" Yes, of course.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.