Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1190 : Q51

Posted: Mon May 05, 2014 4:37 pm
by palamchocku
By the way the question is written, should not the correct answer be 'It will not compile'. How can one arbitrarily assume you need to add brackets for the order of precedence to work etc. If we need to make corrections to the choices, then we have more than 2 answers for this question then. Confusing... Is this how it might be on the real test?

BTW - This is on Standard Tests/Test 1
What will the following code snippet print?     
Object t = new Integer(107);     
int k = (Integer) t.intValue()/9;     
System.out.println(k);

Choices:
- 11
-12
- It will not compile
- It will throw Exception at runtime

Correct answer is 12.

Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51

Posted: Mon May 05, 2014 8:07 pm
by admin
I see that "It will not compile" is indeed marked as the correct option.

-Paul.

Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51

Posted: Tue Dec 01, 2015 4:50 pm
by dannysantos1985
I'm confused about methods availablity.
What it matters is the reference or the object in case of instance methods? I believe it is the object.
So why isn't intValue() instance method available in the code?

Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51

Posted: Tue Dec 01, 2015 8:31 pm
by admin
There are two stages in which a call is checked. First is the compilation stage - in this stage the compiler checks if a method call is valid. It can check that only on the basis of the class of the reference variable because there are no objects at compile time. If the class of the reference doesn't have a particular method then you can't call that method.

Second is the runtime stage - in this stage the JVM checks which method it needs to invoke based on the actual object that is referred to by the variable. That is why, if a superclass variable points to a subclass object, then the method of the subclass is invoked.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51

Posted: Wed Dec 02, 2015 7:05 am
by dannysantos1985
"If the class of the reference doesn't have a particular method then you can call that method."
Don't you mean if the class of the reference *does* have a particular method then you can call that method?

Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51

Posted: Wed Dec 02, 2015 7:10 am
by admin
Yes, can't. Sorry.

Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51

Posted: Wed Jan 13, 2016 8:13 am
by Deleted User 2655
ListTest.java:22: error: cannot find symbol
int k = (Integer) t.intValue()/9;
^
symbol: method intValue()
location: variable t of type Object

When you try to run the code snippet, You get this message so the answer is correct, it will not compile.

Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51

Posted: Sat Dec 09, 2017 11:42 pm
by Rinkesh
Has Function Call got higher priority than Dot Operator??

Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51

Posted: Sun Dec 10, 2017 4:06 am
by admin
Both the dot and method call are applied on the right side of a variable. So the question of priority doesn't arise. Which ever one is attached to the variable will be applied to the variable.
Do you have a particular situation in mind where you think it is important?

Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51

Posted: Fri Jan 04, 2019 4:46 pm
by crazymind
int k = (Integer) t.intValue()/9;

Hi, does compiler check the existence of intValue method in Object class at compile time? So it finds nothing therefore the code doesn't compile?

Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51

Posted: Fri Jan 04, 2019 11:51 pm
by admin
That is correct. Compiler does not always know the actual type of object to which a reference will point at run time. So, it goes by the declared type of the reference. In this case, declared type of t is Object and Object class doesn't have intValue() method and therefore, the compiler rejects the call.

Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51

Posted: Fri Nov 22, 2019 6:06 pm
by wdphipps
Not a question the problem itself, it seems clear to me. I'm more asking for any tips the admins have on how to really catch these evasive problems. Now that I know if it I'll have an eye out, but obviously that's just one problem type. Of course the book is helping tremendously, but I doubt any book covers all of the trap cards. What should I look out for?

Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51

Posted: Sat Nov 23, 2019 12:58 am
by admin
This is not really an evasive problem. You will need to remember certain fundamental rules of the language at all times and see whether any given code violates those fundamental rules. There are only a few of them. (The OCP Java 11 Certification Part 1 Fundamentals book by Hanumant Deshmukh covers them all. I am sure other books cover them too.)

Developing code will make you remember these rules and also give you confidence while answer such problems.

Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51

Posted: Mon Nov 25, 2019 5:15 pm
by wdphipps
I'm going through this book now. While it certainly covers casting and precedence in great depth, the syntax of int k = (Integer) t.intValue()/9; isn't something that seems to violate any fundamental rules. If 'write more code' is the answer then so be it, but it definitely seems evasive from my end, especially since the level of the question in the mock test was so high.

Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51

Posted: Mon Nov 25, 2019 10:41 pm
by admin
Will try to come up with something more helpful. Thank you for your feedback!

Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51

Posted: Sun Jan 07, 2024 7:58 am
by Badem48
Hi,

I just want to point out that
"'Integer(int)' is deprecated since version 9 and marked for removal"
so the code line:

Code: Select all

Object t = new Integer(107);
is also not legal and I couldn't see on the explanation.

It should use the factory method like this:

Code: Select all

Object t = Integer.valueOf(107);

Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51

Posted: Sun Jan 07, 2024 8:39 am
by admin
Updated.
thank you for your feedback!