Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

x = b ? y : z ;  RSS feed

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
10. Select the equivalent answer for the code given below?
boolean b = true;
if ( b ) {
x = y;
} else {
x = z;
}
A) x = b ? x = y : x = z ;
B) x = b ? y : z ;
C) b = x ? y : z ;
D) b = x ? x = y : x = z ;
Answer 10:
B) x = b ? y : z ;
---------
is the ans a legal statement? I have never seen it before,I think the normal format should be 'b?x=y:x=z;'
 
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Wei -
Answer B is legal; but it's really two operations in one statement. The conditional (or ternary) operator is evaluated first, then the assignment. Therefore the result of the ternary operator is assigned to x.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!