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

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

Moderator: admin

Post Reply
bptoth
Posts: 33
Joined: Mon May 06, 2013 9:41 am
Contact:

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

Post by bptoth »

Perhaps if the question was put in the form of "Select the best option", it could be detailed why the chosen answer regarding covariant types should be considered "better" than plain Object (for me it is quite much context dependent what to use, so I do not consider this a very good phrasing, but maybe there are similar questions in the real exam as well?)

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

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

Post by admin »

The problem statement does say, "(Select the best option.)".

Brian B
Posts: 13
Joined: Mon Jun 16, 2014 1:07 pm
Contact:

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

Post by Brian B »

After reading the explanation, I agree with the answer. However, I think "Select the best option" is subjective. Wouldn't both String and Object be technically valid answers? Would what is considered the "best option" be based on the purpose of the code being written?

As far as reinforcing the concept this question is trying to teach, would it make more sense if it allowed selection of multiple answers? Perhaps including primitives in the choices would help to clarify the point that covariance doesn't apply to them.

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

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

Post by admin »

Yes, they are valid options. However, there will be situations in the exam where multiple valid options may be present but you have to pick only one. So you have to select the option that is most suitable.

You are right about primitives. The explanation has been enhanced to explain that as well.

Paul.

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

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

Post by raphaelzintec »

i hope i wont have Oracle error by saying: Return type can be any class since all objects can be cast to Object.

because imagine Oracle says you are wrong it cannot be anytype such as primitive

Newtoned
Posts: 1
Joined: Thu Aug 29, 2024 3:58 am
Contact:

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

Post by Newtoned »

bptoth wrote:
Wed May 21, 2014 8:31 am
Perhaps if the question was put in the form of "Select the best option",best sex dolls it could be detailed why the chosen answer regarding covariant types should be considered "better" than plain Object (for me it is quite much context dependent what to use, so I do not consider this a very good phrasing, but maybe there are similar questions in the real exam as well?)
In the context of an exam or coding best practices, covariant types are often considered "better" than using Object because they offer a balance of flexibility and safety. However, the "best" option always depends on the specific use case and the context in which the code is being written.

n199a_
Posts: 8
Joined: Wed Jan 08, 2025 9:43 am
Contact:

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

Post by n199a_ »

There is a question:
Which of the following is a legal return type of a method overriding the given method:
public Object myMethod() {...}

(Select the best option.)
The answer that you think is correct is the following:
Return type can be any class since all objects can be cast to Object.
The question clearly states that we should override the method first and then give the answer.

However, your answer is fundamentally wrong for the reason that when overriding a method, it IS POSSIBLE to change the type of the return value. Therefore, the return value cannot be any type of object.

Here is the proof:

Code: Select all

public interface Parent {
    public Object myMethod();
}

Code: Select all

public class Main implements Parent {

    @Override
    public Long myMethod() {
        return Integer.getInteger("1"); // Not compiler!
    }
}

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

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

Post by admin »

Well, the problem statement is asking about the return type of the method. It is not talking about the type of the value or of the variable that is actually returned by the method!

Post Reply

Who is online

Users browsing this forum: nkaragulov and 10 guests