This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes operators Q Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "operators Q" Watch "operators Q" New topic
Author

operators Q

saied ims
Ranch Hand

Joined: Jun 21, 2005
Posts: 109
in chapter 4 page 309
Q4
class Fork {
public static void main(String[] args) {
if(args.length == 1 | args[1].equals("test")) {
System.out.println("test case");
} else {
System.out.println("production " + args[0]);
}
}
}
And the command-line invocation:
java Fork live2
A. test case
B. production
C. test case live2
D. Compilation fails.
E. An exception is thrown at runtime.
the output will be production Fork

why the answer is E is correct. Because the short circuit (||) is not used, both operands are evaluated. Sinceargs[1] is past the args array bounds, an ArrayIndexOutOfBoundsException is thrown.

thanks
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
What is the question?
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
If you run code by java Fork live2,

args[1].equals("test") will throw ArrayIndexOutOfBoundsException at runtime.

Answer given in the book is correct and the reason is since short circuit or is not used here, even if first condition goes true, second will still be evaluated.

Naseem
[ August 10, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: operators Q
 
Similar Threads
K&B self test query
Command Line Invocation
operators (sourece k & b)
question about Array index - ojective 7.6 on K&B SCJP book
K&B doubt(unofficial errata) Moderators,Please keep this thread on the top so all to see and add