This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "operators Q" Watch "operators Q" New topic

operators Q

saied ims
Ranch Hand

Joined: Jun 21, 2005
Posts: 109
in chapter 4 page 309
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.

Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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.

[ 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:
subject: operators Q
Similar Threads
Command Line Invocation
operators (sourece k & b)
question about Array index - ojective 7.6 on K&B SCJP book
K&B self test query
K&B doubt(unofficial errata) Moderators,Please keep this thread on the top so all to see and add