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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post 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?

admin
Site Admin
Posts: 9116
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

javanaut
Posts: 22
Joined: Wed Aug 21, 2013 12:29 am
Contact:

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

Post 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: :)

M.Komarov
Posts: 16
Joined: Fri Sep 06, 2013 1:37 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 9116
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

M.Komarov
Posts: 16
Joined: Fri Sep 06, 2013 1:37 am
Contact:

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

Post by M.Komarov »

Thanks a lot for the explanation!

Ad9999
Posts: 15
Joined: Fri May 15, 2015 12:06 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 9116
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Sergiy Romankov
Posts: 31
Joined: Thu Feb 19, 2015 8:25 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 9116
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

...looks like it will be executed in both cases.
Why do you think so?
If you like our products and services, please help us by posting your review here.

PeterD
Posts: 4
Joined: Thu Jan 26, 2017 8:45 am
Contact:

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

Post 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.

carlosrgc
Posts: 3
Joined: Mon Feb 20, 2017 9:37 am
Contact:

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

Post 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 :)

st.lisker
Posts: 22
Joined: Sat Jun 30, 2018 6:11 am
Contact:

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

Post by st.lisker »

))-$ I selected "None of the above too.

flex567
Posts: 201
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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:...

admin
Site Admin
Posts: 9116
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

chiran12345
Posts: 5
Joined: Fri Apr 12, 2019 12:00 pm
Contact:

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

Post by chiran12345 »

In option 4, could you explain why the second return will not execute when the condition is true? confused!!!

admin
Site Admin
Posts: 9116
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Denyo1986
Posts: 38
Joined: Thu Jan 07, 2021 2:47 am
Contact:

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

Post 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? :)

admin
Site Admin
Posts: 9116
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest