File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Java Interview Guide this week in the Jobs Discussion forum!

# Mock question

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi!
I don�t understand this question:
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 ;
The answer is B but why x = b??? I think that the correct answer will be (x == true) ? y : z;
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Hi jordi,
The ternary operator x=(a?b:c) allows to replace an if-else statement of this form:
if(a)
x=b
else
x=c
Thus, a has to be a boolean expression.
B is the correct answer because b is a boolean expression and x gets assigned y if b is true and z otherwise.
In this example b is true since it is declared and initialized as such at the top of the code...
Hope that helps
Val

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
leena rane
Ranch Hand

Joined: Aug 13, 2001
Posts: 280
Hi Jordi,
x = b ? y : z ;
consider only this:
b ? y : z
means if b is true - y
else - z
now
x = b ? y : z ;
means if b is true - x=y
else - x=z
I think i have not made it very clear.
Ask if u still have doubt.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thank you, I understand now

I agree. Here's the link: http://aspose.com/file-tools

subject: Mock question