aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
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: 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.

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://aspose.com/file-tools
 
subject: operators Q