Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questions from JavaCaps Mock Exam 1

 
Richa Jeetah
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Run it and you'll see why. You're going off the end of the array.
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Richa Jeetah
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i ran the code for Q20 and Q10, now its clear to me.
Thank You
 
smita raval
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richa,
"And if it were [5] then correct answer would have been A ??"
Yes, of course.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic