About Question enthuware.ocajp.i.v7.2.1190 : Q51
Moderator: admin
-
- Posts: 1
- Joined: Mon May 05, 2014 4:31 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1190 : Q51
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.
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.
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51
I see that "It will not compile" is indeed marked as the correct option.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Tue Nov 24, 2015 4:34 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51
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?
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?
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51
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.
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.
-
- Posts: 12
- Joined: Tue Nov 24, 2015 4:34 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51
"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?
Don't you mean if the class of the reference *does* have a particular method then you can call that method?
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51
Yes, can't. Sorry.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51
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.
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.
-
- Posts: 35
- Joined: Sat Nov 25, 2017 4:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51
Has Function Call got higher priority than Dot Operator??
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51
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?
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.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51
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?
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?
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51
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.
-
- Posts: 21
- Joined: Mon Sep 23, 2019 4:55 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51
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?
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51
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.
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.
-
- Posts: 21
- Joined: Mon Sep 23, 2019 4:55 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51
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.
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51
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.
-
- Posts: 26
- Joined: Thu Aug 24, 2023 4:33 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51
Hi,
I just want to point out that is also not legal and I couldn't see on the explanation.
It should use the factory method like this:
I just want to point out that
so the code line:"'Integer(int)' is deprecated since version 9 and marked for removal"
Code: Select all
Object t = new Integer(107);
It should use the factory method like this:
Code: Select all
Object t = Integer.valueOf(107);
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1190 : Q51
Updated.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 138 guests