wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt on this question(from a Mock Test) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt on this question(from a Mock Test)" Watch "doubt on this question(from a Mock Test)" New topic
Author

doubt on this question(from a Mock Test)

pradeep singh
Ranch Hand

Joined: Oct 23, 2007
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


Answer is given:[c]
but my answer is [b] and [c] both


SCJP 5.0(75%), SCWCD 5.0(88%)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Why do you think (b) is correct?

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

Hint: What happens if x == y?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    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:



Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
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

Joined: Dec 28, 2007
Posts: 2258
    
    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 ]
pradeep singh
Ranch Hand

Joined: Oct 23, 2007
Posts: 339
Thanks to you all
My personell view is Jesper Young is right.He has given a correct explanation.
Dale Carnegie
Greenhorn

Joined: Oct 12, 2007
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


A dreamer is one step behind the achiever, and the achiever was once a dreamer.
Dale Carnegie
Greenhorn

Joined: Oct 12, 2007
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

Joined: Dec 29, 2007
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

Joined: Oct 12, 2007
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

Joined: Dec 28, 2007
Posts: 2258
    
    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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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

Joined: Dec 28, 2007
Posts: 2258
    
    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

Joined: Apr 20, 2006
Posts: 3344

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


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt on this question(from a Mock Test)
 
Similar Threads
Mock question
Casting
A question from Mock Exam
x = b ? y : z ;
Chapter 5 K&B -Doubt