wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes javaprepare question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "javaprepare question" Watch "javaprepare question" New topic
Author

javaprepare question

geetha nagarajan
Ranch Hand

Joined: Jul 13, 2001
Posts: 94
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));
System.out.println(i);
}
}
The answer acc.to javaprepare is 2.
I think it's 3.
Pl.help.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Have you tried it? I'll bet that will tell you the correct answer.
Corey


SCJP Tipline, etc.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
The conditional or || doesn't evalute the right operand "(i++) == 0)" if the left one "t" is true. Thus 2 is printed.
Similarly, && doesn't evalute the right operand if the left is false.
This is done for the sake of speed.


SCJP2. Please Indent your code using UBB Code
geetha nagarajan
Ranch Hand

Joined: Jul 13, 2001
Posts: 94
Yes,i tried it out.The o/p is 2.
After the statement: b=t||((i++)==0));
The value of i is 0,not 1.
Why is it so?That's my confusion.
geetha nagarajan
Ranch Hand

Joined: Jul 13, 2001
Posts: 94
Thanks Jose,for the clarification.
Got it.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by geetha nagarajan:
After the statement: b=t||((i++)==0));
The value of i is 0,not 1.
Why is it so?That's my confusion.

That would have been a great question to start with.
Lloyd Wilson
Greenhorn

Joined: May 16, 2002
Posts: 2
The operation within the if () clause does not proceed testing an operation if it first meets a condition that equates to a true statement.
If reading 'if (true, ((i++) == 0))' the condition starts checking the left side of the operation which validates to true. Because the boolean is true, it continues performing the operation within the if block and doesn't check the right side of the condition. The '||' operation only looks for a true result first, and if successful (true), performs no other operations (meaning, the right hand side does not get looked at).
In the case of 'if (false, ((i+=2) > 0))' the condition that is first checked is false, which in the case of an '||' or operation, it looks to find a condition that is equal to true, and in this case, the operation i+=2 > 0 is performed.
This only works because the '||' condition on the left first produced a false result. The condition on the right executes because the '||' condition found no true statement.
In your response, if the condition of the boolean in the first check was false, you would get the value of '3'.
Execute the code and play around to see how it works.
Cheers,
Lloyd Wilson
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: javaprepare question
 
Similar Threads
short ckt ques from mock exam
Operator question
what's the output
question from anilbachi site
question from javacross.com