About Question enthuware.ocpjp.v11.2.3153 :
Moderator: admin
-
- Posts: 3
- Joined: Thu Apr 29, 2021 4:45 am
- Contact:
About Question enthuware.ocpjp.v11.2.3153 :
We have a problem with the answers in this question.
The question ask for Identify the options that will cause compilation failure.
Choose 3 options
1. ai.amazingMethod(AmazingInterface.value, AmazingClass.value);
ai is a reference of type AmazingInterface. AmazingInterface does not have an amazingMethod with two parameters. Therefore, this will not compile.
That's correct answer because compilation will fail.
2. ai.amazingMethod(AmazingInterface.value);
original says that this is wrong answer
In this case compilation will compile, So this is wrong answer
3. ((AmazingClass)ai).amazingMethod("x1", value);
original explanation says that is correct answer
This is invalid because the usage of value is ambiguous. It may refer to AmazingClass's value field or AmazingInterface's value field because AmazingClass implements AmazingInterface.
but actually it will compile without any problems, because variable value point to static variable of AmazingClass.
So that wrong answer
4. ai.amazingMethod(value);
original explanation says that is correct answer
It will fail compilation with the error message:
error: reference to value is ambiguous. Both variable value in AmazingClass and variable value in AmazingInterface match.
You can use AmazingClass.value or AmazingInterface.value to resolve the ambiguity.
but actually it will compile without any problem because
variable value point to static variable of AmazingClass.value
So that wrong answer
5. ai.amazingMethod("x1");
originally says that is wrong answer, without any explanation
that's correct, it will compile without any problem
because variable ai of AmazingInterface has a amazingMethod(String arg), and can accept String value.
So that's wrong answer
The question ask for Identify the options that will cause compilation failure.
Choose 3 options
1. ai.amazingMethod(AmazingInterface.value, AmazingClass.value);
ai is a reference of type AmazingInterface. AmazingInterface does not have an amazingMethod with two parameters. Therefore, this will not compile.
That's correct answer because compilation will fail.
2. ai.amazingMethod(AmazingInterface.value);
original says that this is wrong answer
In this case compilation will compile, So this is wrong answer
3. ((AmazingClass)ai).amazingMethod("x1", value);
original explanation says that is correct answer
This is invalid because the usage of value is ambiguous. It may refer to AmazingClass's value field or AmazingInterface's value field because AmazingClass implements AmazingInterface.
but actually it will compile without any problems, because variable value point to static variable of AmazingClass.
So that wrong answer
4. ai.amazingMethod(value);
original explanation says that is correct answer
It will fail compilation with the error message:
error: reference to value is ambiguous. Both variable value in AmazingClass and variable value in AmazingInterface match.
You can use AmazingClass.value or AmazingInterface.value to resolve the ambiguity.
but actually it will compile without any problem because
variable value point to static variable of AmazingClass.value
So that wrong answer
5. ai.amazingMethod("x1");
originally says that is wrong answer, without any explanation
that's correct, it will compile without any problem
because variable ai of AmazingInterface has a amazingMethod(String arg), and can accept String value.
So that's wrong answer
- Attachments
-
- answer-code.jpg (190.25 KiB) Viewed 7831 times
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3153 :
Did you actually try compiling the code with options 3 and 4? (Don't use IDE. Use command line.)
The answers and the given explanations for options 3 and 4 are correct.
The answers and the given explanations for options 3 and 4 are correct.
-
- Posts: 3
- Joined: Thu Apr 29, 2021 4:45 am
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3153 :
Yeah right.
I tried to compile code , and it really gaves me the same errors like in answers.
I'm sorry, everything is correct
I tried to compile code , and it really gaves me the same errors like in answers.
I'm sorry, everything is correct

-
- Posts: 17
- Joined: Fri Aug 12, 2022 10:19 am
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3153 :
Hi, I read the previous posts. I was wondering if you could explain why the IDE will not cause compilation failure, just fail at runtime (answer 3 and 4)? If I remove the "implements AmazingInterface" from AmazingClass it will fail to compile. Does this mean that since the "value" from AmazingInterface is hidden from AmazingClass it is also hidden from Awesome, even if AmazingInterface is implemented by Awesome too?
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3153 :
1. I did not mean to say that the IDE will NOT cause compilation error. Just asked not to use it because it is very difficult to reproduce and troubleshoot what the other person is observing while using an IDE due to differences in IDE and environment. Too many moving parts. Command line gives a common baseline, which makes pinpointing the issue a lot easier.
Further, sometimes IDEs show compilation errors in a subtle way that is not clearly noticeable to the user. They may also run the code (even though there might be compilation errors) if the the where compilation error exists is not encountered during execution. That causes confusion for beginners who are studying for the exam. Check out the link given in the above response that explains issues encountered while using IDEs.
2. Not sure what you mean. Please post code and output that explains the situation you are talking about.
Further, sometimes IDEs show compilation errors in a subtle way that is not clearly noticeable to the user. They may also run the code (even though there might be compilation errors) if the the where compilation error exists is not encountered during execution. That causes confusion for beginners who are studying for the exam. Check out the link given in the above response that explains issues encountered while using IDEs.
2. Not sure what you mean. Please post code and output that explains the situation you are talking about.
-
- Posts: 17
- Joined: Fri Aug 12, 2022 10:19 am
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3153 :
I did test answer 3 and 4 they don't cause a compilation error in Intellij, they just crash at runtime (because of abiguity). My original post is meant as one question (and an attempt at explaining what is happening). The point was just that is seems like the ambiguity isn't visible before runtime for answer 3 and 4 (but when "implements AmazingInterface" is removed from AmazingClass it IS visible and therefore doesn't compile). I don't think I can make it clearer with code.
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3153 :
I don't have IntelliJ but I tried it in Netbeans and it did show compilation error for the same reason as mentioned in the explanation. This is correct because the ambiguity in value is clearly visible at compilation (when Awesome extends AmazingClass implements AmazingInterface, there are indeed two value variables visible in main). See attached screenshot.
Either way, what IDEs say is immaterial. What javac says on command line is what is relevant for the exam.
Now, if IntelliJ doesn't show compilation error, you have to ask IntelliJ people as this has nothing to do with the exam and is probably a bug or some misinterpretation of the specification.Either way, what IDEs say is immaterial. What javac says on command line is what is relevant for the exam.
-
- Posts: 17
- Joined: Fri Aug 12, 2022 10:19 am
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3153 :
Alright, thanks anyway:)
-
- Posts: 10
- Joined: Fri Jun 28, 2024 3:08 am
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3153 :
Dear Admin,
I always learned that variables come from the left hand-side and methods from the right hand-side (actual object).
How does this print:
Driving a Car
10 ?
``
How come that ``ai.amazingMethod(AmazingInterface.value, AmazingClass.value);`` doesn't work?
Cheers
I always learned that variables come from the left hand-side and methods from the right hand-side (actual object).
How does this print:
Driving a Car
10 ?
``
Code: Select all
interface Automobile {
int x = 10;
default void drive() {
System.out.println("Driving an Automobile");
}
}
class Car implements Automobile {
int x = 15;
public void drive() {
System.out.println("Driving a Car");
}
}
class test {
public static void main(String[] args) {
Automobile myNewCar = new Car();
myNewCar.drive();
System.out.println(myNewCar.x);
}
}
``
Cheers
Last edited by admin on Fri Jun 28, 2024 8:16 pm, edited 1 time in total.
Reason: Please put code inside code tag
Reason: Please put code inside code tag
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3153 :
what error message are you getting?
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3153 :
>How does this print:
>Driving a Car
>10 ?
Which variable is accessed depends on the declared type of the variable while which method is executed depends on the actual type of the object pointed to by a reference variable.
Please go through a good book to learn the basic concepts before attempting mocks.
>Driving a Car
>10 ?
Which variable is accessed depends on the declared type of the variable while which method is executed depends on the actual type of the object pointed to by a reference variable.
Please go through a good book to learn the basic concepts before attempting mocks.
-
- Posts: 10
- Joined: Fri Jun 28, 2024 3:08 am
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3153 :
I think I got it now. At compile time, Java checks the reference type (the type on the left-hand side of the assignment) to determine which methods and fields are available. This means the compiler will look at the interface or class the reference is declared as to validate method calls and field access.
At runtime, Java uses the actual object type (the type on the right-hand side, which is the type of the object that the reference points to) to execute the method.This is where polymorphism comes into play, especially for overridden methods.
At runtime, Java uses the actual object type (the type on the right-hand side, which is the type of the object that the reference points to) to execute the method.This is where polymorphism comes into play, especially for overridden methods.
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3153 :
That's right.
Who is online
Users browsing this forum: Bing [Bot] and 8 guests