| Author |
In case you are following K&B SCJP 6.0
|
harsha balluru
Greenhorn
Joined: Feb 06, 2009
Posts: 14
|
|
Hi
I just observed a small error in K&B SCJP 6.0 in "Operators" chapter. The question 9 in the self test is incorrectly explained in the answer section. The question is as follows:
Given:
3. public class Spock{
4. public static void main(String args[]){
5. int mask=0;
6. int count=0;
7. if( (5<7) || (++count < 10) | mask++ < 10) mask = mask+1;
8. if( (6>8) ^ false ) mask = mask+10;
9. if( !(mask > 1) && ++count > 1) mask = mask + 100;
10. System.out.println(mask + " " + count);
11. }
12. }
Which two answers are true about the value of mask and value of count?
A. mask is 0
B mask is 1
C mask is 2
D mask is 10
E mask is greater than 10
F count is 0
G count is greater than 0
The correct answer is "mask is 1 and count is 1";
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
In the answer's explanation, it is said that the non-short-circuit OR "|" allows the mask to be incremented which is conflicting. The short-circuit operator on that line makes the compiler not bother the right side of the equation. So effectively mask value is "1" after the line 7 is complete.
On line line 9, the left side of the && operator, !(mask>1) evaluates to true but right side value, ++count > 1 executes and evaluates to false and hence the "if" condition will not pass.
Thus finally the mask and count values are both "1".
Cheers
Harsha[color=darkblue] [/color]
|
 |
Ninad Kulkarni
Ranch Hand
Joined: Aug 31, 2007
Posts: 754
|
|
Hi Harsha,
What I know is if left hand side of short circuit operator evaluates to true when short circuit OR is used or false when short circuit && is used then no evaluation of right hand side of short circuit operator occurs.
See the Java Language Specification for details.
Some words are included to clear the meaning due to this reason message edited.
|
SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
|
 |
Madhu Desai
Ranch Hand
Joined: Jun 14, 2009
Posts: 42
|
|
harsha balluru wrote:Hi
3. public class Spock{
4. public static void main(String args[]){
5. int mask=0;
6. int count=0;
7. if( (5<7) || (++count < 10) | mask++ < 10) mask = mask+1;
8. if( (6>8) ^ false ) mask = mask+10;
9. if( !(mask > 1) && ++count > 1) mask = mask + 100;
10. System.out.println(mask + " " + count);
11. }
12. }
[color=darkblue] [/color]
You have missed one set of '( )' in line 7 before '(5 <' & after '<10)', which makes all the difference. May be this is causing you confusion. You put the parenthesis there, and answer will correct according to K&B.
It should be:
if ( ( (5<7) || (++count<10) ) | mask++ < 10)
|
Thanks
Preparing for SCJP 6
|
 |
harsha balluru
Greenhorn
Joined: Feb 06, 2009
Posts: 14
|
|
Hi Madhu
Thanks for the clarification. As you quoted, the parantheses made the difference there. With that in place, K&B's explanation is correct...
|
 |
João Martinho
Greenhorn
Joined: Aug 26, 2009
Posts: 12
|
|
Sorry for bringing back to life this topic but i have a doubt on it.
the book also says:
At line 8 the ^ returns true only if exactly one operand is true.
but:
6>8 -> true
false -> false
So:
if(true ^ false) -> is true, after all only one operand is true or am i reading something wrong?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 14606
|
|
João Martinho wrote:6>8 -> true
Six is greater than eight?
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9090
|
|
Henry Wong wrote:
João Martinho wrote:6>8 -> true
Six is greater than eight?
You've got a point here , on a serious note, it seems an honest mistake that's all so no offenses...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
João Martinho
Greenhorn
Joined: Aug 26, 2009
Posts: 12
|
|
IT seams that i was reading something between the lines, after all this certiifcation is all about tricks and i thought 6>8 was a trick question.
Hey my mistake maybe i need to read everything slowly.
At least i made people laght and that is always good
PS: But if oneday math proves that 6 is bigger then 8 then what i have said is true right?
|
 |
 |
|
|
subject: In case you are following K&B SCJP 6.0
|
|
|