Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# x = b ? y : z ;

Ranch Hand
Posts: 56
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 ;
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
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