Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Thu May 03, 2012 8:47 am
by ETS User
Which of the following implementations of a max() method will correctly return the largest value?

Answer:

Code: Select all

int max(int x, int y){
       if (x > y) 
           return x;       
       return y; }
I clicked : None of the above.

Bec. for Example : x and y are 5, which variable is the max?

If there wouldnt be "None of the above" box, it would maybe less iritation.

Are any questions like this in the Oracle exam?

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Thu May 03, 2012 11:59 am
by admin
In that case, it wouldn't matter which variable you return. The code still works as expected. I think this is a valid situation that you can expect in the real exam.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Wed Sep 04, 2013 1:19 pm
by javanaut
I answered this problem correctly, but wanted to point out that the explanation for why the second answer is incorrect is confusing.

The test engine explains that #2 could be correct if:
It would work if the first return and the corresponding brackets is removed.
I think this should say it would work if the first return and corresponding parentheses are removed. I coded this in a class with main() and it worked when doing this.



:idea: :)

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Sun Sep 08, 2013 2:01 pm
by M.Komarov
It seems to me that had boolean been suitable for switch expression, the compiler would complain because of the semicolon after the switch closing curly brace. It is unreachable code.

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Mon Sep 09, 2013 5:50 pm
by admin
M.Komarov wrote:It seems to me that had boolean been suitable for switch expression, the compiler would complain because of the semicolon after the switch closing curly brace. It is unreachable code.
I don't think so because that would happen only if the condition in the switch expression were a compile time constant. In this case, i.e. x< y is not a compile time constant so there wouldn't be "unreachable code".

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Tue Sep 10, 2013 2:41 am
by M.Komarov
Thanks a lot for the explanation!

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Sat May 16, 2015 11:24 am
by Ad9999
Wish this question took into account equal values. I guess technically, the largest value is both numbers in that case. If the question said return the "larger" value, the answer would be none of the above? Over thinking or maybe the question wording should be better?

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Sat May 16, 2015 9:19 pm
by admin
I think it is fine. The question doesn't explicitly specify what needs to be done if both the values are same but that doesn't affect the answer because the answer that satisfies the case when both the values are not same, satisfies this situation as well.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Fri May 22, 2015 6:41 am
by Sergiy Romankov
This question for me is really ambiguous.
The part - return y; is not "else" part and looks like it will be executed in both cases.
Though it works right, it is not clear why the second return will not execute after first in case when first will?

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Fri May 22, 2015 9:37 am
by admin
...looks like it will be executed in both cases.
Why do you think so?

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Thu Jan 26, 2017 8:52 am
by PeterD
Made the same choice as the first respondent. If x and y both have the same value, eg. x = 5 and y =5, there is no larger value, they are both the same. Since no answer is provided for this specific case I find it logical to choose 'none of the above'. My first answer was the correct one, then I changed for the incorrect answer. This was also because there are so many trick questions asked that I have come to expect a trick everywhere.

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Sun Jun 11, 2017 8:23 am
by carlosrgc
Same here . I selected "None of the above." thinking in the possibility that x==y :(((. I had to drill my brain to accept the selected answer :)

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Sun Jul 08, 2018 3:29 am
by st.lisker
))-$ I selected "None of the above too.

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Thu May 09, 2019 2:37 am
by flex567
I don't think so because that would happen only if the condition in the switch expression were a compile time constant. In this case, i.e. x< y is not a compile time constant so there wouldn't be "unreachable code".
Switch expression doesn't need to be compile time constant?
A switch expression must evaluate to one of the following three kinds:...

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Thu May 09, 2019 3:48 am
by admin
Not sure what you mean. Switch expression should not obviously be a compile time constant. If it were a compile time constant why would you even need a switch statement! The statement that you have quoted is about a hypothetical issue.

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Tue Jul 16, 2019 1:28 pm
by chiran12345
In option 4, could you explain why the second return will not execute when the condition is true? confused!!!

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Wed Jul 17, 2019 5:02 am
by admin
chiran12345 wrote:
Tue Jul 16, 2019 1:28 pm
In option 4, could you explain why the second return will not execute when the condition is true? confused!!!
Because if the condition is true, the first return will execute. The control will return from the method once it is executed.

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Sat Jan 09, 2021 10:50 am
by Denyo1986
admin wrote:
Sat May 16, 2015 9:19 pm
I think it is fine. The question doesn't explicitly specify what needs to be done if both the values are same but that doesn't affect the answer because the answer that satisfies the case when both the values are not same, satisfies this situation as well.

HTH,
Paul.
Hey Paul,

I would like to challenge you here and side with the quoted user.
The question says "which option CORRECTLY returns the largest value"

As we (hopefully) all agree, in order to CORRECTLY implement such a method, one has to take equal values into consideration. Anything else would be poor programming. Assuming that, we would look at implementations that are mayb 80%/90% correct, but not entirely correct.
Therefore it is absolutely reasonable that people opt for the "none of the above" option, thus the question is ambiguos.

Why not change it and either say (equal values do not have to be considered) or change the code? Then its clear. Wont hurt anyone but confuse less students :)


And on another sidenote....is it normal that the timer adds 2 seconds instead of one when you are over the exam time? Is that an implementation error or a punishment for missing the time target? :)

Re: About Question enthuware.ocajp.i.v7.2.1267 :

Posted: Sat Jan 09, 2021 11:58 am
by admin
But it does CORRECTLY return the largest value. Does it not? Even if two equal are values are passed, either one can be returned.
Observe that the code is required to return the largest value and not the "larger of the two".

>And on another sidenote....is it normal that the timer adds 2 seconds instead of one when you are over the exam time? Is that an implementation error or a punishment for missing the time target
Not sure I understand. Please create a separate thread for it.