Win a copy of The Java Performance Companion this week in the Performance forum!

# doubt on this question(from a Mock Test)

Ranch Hand
Posts: 339
The following line of code is equivalent to which ofthe given codes?

if (x>y)
z=x;
else
z=y;

[A] z=x<y ? x:y
[B] z=x<y ?y : x
[c] z= y<x ?x :y
[D] z=y<x?y:x

but my answer is [b] and [c] both

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15359
39
Why do you think (b) is correct?

!(x < y) is not the same as (y < x).

Hint: What happens if x == y?

Roberto Perillo
Bartender
Posts: 2271
3
Howdy.

Honestly, I think this exercise is wrong. None of the answers are correct.

The answers that get closer to be right are both B and C. The exercise assumes that the oposite of x>y is y<x, which is wrong. The oposite of x>y is y<=x. So the equivalent to

is

Letter B would also be correct if it was like this:

Campbell Ritchie
Sheriff
Posts: 49396
62
Jesper Young is correct.

There is no point confusing things by changing < to >=, you have to stick with what the question said. Not as if you had actually got the < and >= the right way round.

Roberto Perillo
Bartender
Posts: 2271
3
Originally posted by Campbell Ritchie:
Jesper Young is correct.

There is no point confusing things by changing < to >=, you have to stick with what the question said. Not as if you had actually got the < and >= the right way round.

I don't agree. Jesper certainly is correct (!(x < y) is not the same as (y < x)). But there's no correct answer in the given list. > is always oposite to <=
[ December 29, 2007: Message edited by: Roberto Perillo ]

Ranch Hand
Posts: 339
Thanks to you all
My personell view is Jesper Young is right.He has given a correct explanation.

Dale Carnegie
Greenhorn
Posts: 9

if (x>y) z=x; else z=y;

[A] z=x<y ? x:y
[B] z=x<y ?y : x
[c] z= y<x ?x :y
[D] z=y<x?y:x

I also think that B and C are the right answers. Look at if statement and what it tells us? Get max. Isn't it?

a) min
b) z=x<y ?y :x
is
z=y>x ?y :x -it means get max
c)the same as b
d)min

Dale Carnegie
Greenhorn
Posts: 9
Another idea... with if x=y... in b we would get z=x not z=y like at if. But what is the difference... only if x and y are objects not primitives. But here we have > = operators... who knows may be x and y objects of Integer
[ December 29, 2007: Message edited by: Dale Carnegie ]

Dean Jones
Ranch Hand
Posts: 129
Both options b and c are correct.
Case 1: x!=y,x<y, x=4, y=5, then z=5, using both b and c.
Case 2: x!=y, x>y, x=6,y=5, then z=6, using both b and c.
Case 3: x=y, x=5,y=5, then z=5, using both b and c.

Dale Carnegie
Greenhorn
Posts: 9
b is not correct because:

Integer a,b;
a = 777;
b = 777;

//(a == b) false
// a.equals(b) true

So

z1=a;
z2=b;
//(z1 == z2) false

So z will have a reference to another object not like at the if statement.

Roberto Perillo
Bartender
Posts: 2271
3
Originally posted by Dale Carnegie:
So z will have a reference to another object not like at the if statement.

I thought we were talking about primitive types!

Ok, ok... let's consider that "equivalent" means "looks almost like"!!!

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15359
39
Originally posted by Roberto Perillo:
Honestly, I think this exercise is wrong. None of the answers are correct.

The answers that get closer to be right are both B and C. The exercise assumes that the oposite of x>y is y<x, which is wrong. The oposite of x>y is y<=x. ...

Sorry Roberto, you're wrong...

The exercise does not assume that x > y is the opposite of y < x. It assumes that x > y is the same as y < x, and that's correct. So answer (c) is correct.

The opposite of x > y is x <= y. Not x < y, so answer (b) is wrong.

Note that the syntax of the ? : construct is like this: expression ? value-if-true : value-if-false

So, an if-statement like

is equivalent to: z = x > y ? x : y;
Roberto, you've got it the wrong way around in your answer above.

Instead of x > y you can write y < x (answer c).

If you're still doubting this, write a small test program to verify how it works.
[ December 30, 2007: Message edited by: Jesper Young ]

Roberto Perillo
Bartender
Posts: 2271
3
Howdy, y'all.

Yes partners, I read and reread carefully the question. I got it the wrong way around, that's right, Jesper... sorry I read the question too quickly at the first time... the essence of this exercise is to know what is the same to x < y, not what is oposite to x < y... correct, x < y is the same as y > x... in this case, the only correct answer is indeed letter (C)...
Thanks Jesper, you got everything cleared, man.

Good evening, everyone... and happy new year!!!
[ December 30, 2007: Message edited by: Roberto Perillo ]

Raghavan Muthu
Ranch Hand
Posts: 3381
That's great Roberto. Don't worry. That's why this forum IS for and very good people here are around.

It was a nice discussion through. Thank you Pradeepson for the question