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

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

Moderator: admin

Post Reply
palamchocku
Posts: 1
Joined: Mon May 05, 2014 4:31 pm
Contact:

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

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

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

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

Post by admin »

I see that "It will not compile" is indeed marked as the correct option.

-Paul.
If you like our products and services, please help us by posting your review here.

dannysantos1985
Posts: 12
Joined: Tue Nov 24, 2015 4:34 pm
Contact:

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

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

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

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

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

dannysantos1985
Posts: 12
Joined: Tue Nov 24, 2015 4:34 pm
Contact:

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

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

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

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

Post by admin »

Yes, can't. Sorry.
If you like our products and services, please help us by posting your review here.

Deleted User 2655

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

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

Rinkesh
Posts: 35
Joined: Sat Nov 25, 2017 4:13 pm
Contact:

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

Post by Rinkesh »

Has Function Call got higher priority than Dot Operator??

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

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

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

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

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

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

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

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

wdphipps
Posts: 21
Joined: Mon Sep 23, 2019 4:55 pm
Contact:

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

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

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

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

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

wdphipps
Posts: 21
Joined: Mon Sep 23, 2019 4:55 pm
Contact:

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

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

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

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

Post by admin »

Will try to come up with something more helpful. Thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 0 guests