• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

doubt on this question(from a Mock Test)

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


Answer is given:[c]
but my answer is [b] and [c] both
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think (b) is correct?

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

Hint: What happens if x == y?
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to you all
My personell view is Jesper Young is right.He has given a correct explanation.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic