File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what's the output 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 "what Watch "what New topic

what's the output

Samrat Pujara

Joined: Jan 06, 2001
Posts: 7
public class ShortCkt {
public static void main(String args[]) {
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t | | ((i++) == 0));
b = (f | | ((i+=2) > 0));

a] 0
b] 1
c] 2
d] 3

Answer is probably c) but i feel it should have been d)
Any suggestions ?

Sam Wong
Ranch Hand

Joined: Dec 07, 2000
Posts: 133
Answer is c. Here's why.
| | is a short circuited operation meaning that it will only do as much work as necessary to determine the answer.
Hence, in the line of code: b = (t| |((i++) == 0)), t is true and thus determine the output of the | | operator regardless of the output of ((i++) == 0). In such a case, the second operand being ((i++) == 0) will not be executed and thus i will not be incremented.
The answer would have been d if the operands were reversed like b = (((i++) == 0) | | t);
I agree. Here's the link:
subject: what's the output
It's not a secret anymore!