• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Boyarsky/Selikoff OCA Java 8 Pratise Tests exam question CH6Q15

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone help me with this from Boyarsky/Selikoff OCA Java 8 Pratise Tests exam question CH6Q15?



15. Given the following application, which diagram best represents the state of the
mySkier, mySpeed, and myName variables in the main() method after the call to the
slalom() method?

A:
mySkier -> null
myName  -> "Rosie"
mySpeed -> int[] 18

B:
mySkier -> Ski.age 18
myName  -> "Wendy"
mySpeed -> int[] 11

C:
mySkier -> Ski.age 18
myName  -> "Rosie"
mySpeed -> int[] 0

D:
mySkier -> Ski.age 16
myName  -> "Wendy"
mySpeed -> int[] 0


Answer in book:
To solve this problem, it helps to remember that Java is a pass-by-value language in which copies of primitives and object references are sent to methods.
This also means that an object’s data can be modified within a method and shared with the caller, but not the reference to the object.
Any changes to the object’s reference within the method are not carried over to the caller.

In the slalom() method, the Ski object is updated with an age value of 18. Although, the last line of the slalom() method changes the variable value to null,
it does not affect the mySkier object or reference in the main() method.
Therefore, the mySkier object is not null and the age variable is set to 18, making Options A and D incorrect.

Next, the name variable is reassigned to the Wendy object, but this does not change the reference in the main() method, so myName remains Rosie.

Finally, the speed array is assigned a new object and updated. Since the array is updated after the reference is reassigned,
it does not affect the mySpeed array in the main() method.
The result is that mySpeed continues to have a single element with the default int value of 0. For these reasons,
Option B is incorrect, and Option C is correct.



My conclusion and struggle:
I got a little bit confused because answer to this part says: the name variable is reassigned to the Wendy object,
but this does not change the reference in the main() method, so myName remains Rosie.

Reassigned to the Wendy object? Wendy an object?
Also I do not fully understand why the IDE warns that method Parameter name is not used?
Is this because it is a void method and doesn't return a value or it doesn't print out a value in the method?
Because when I add System.out.print(name); in method or change to a non-void method and return name then the warning disappears.
So my conclusion is that myName doesn't have any relation with name. Both a different address in String-pool.
Right?
Also it feels strange to pass a final variable to a method where it is altered.

Passing the complete mySkier instance as parameter to slalom method is, I assume, a polymorphic parameter objective.
So racer is infact mySkier instance and racer can change instance variables of mySkier, right?
And why is this assigned value never used? -> racer = null;

And last in my opinion has int[] speed nothing to do with mySpeed which is initialized with new int[1]; what produces 0.

P.S.
I started learning (myself) Java a few month's ago with an amateur level PHP background for several years and I really want to take the 1Z0-808 exam
Sometimes I'm suffering from a tunnelvision and need some help to broaden my research horizon and appreciate to get some advise.
Thanks in advance.
 
Marshal
Posts: 67443
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wendy might be a woman (I know several) but, in this program, "Wendy" is an object, in fact a String literal, which per se points to (=references) a String object. See this section and this part of the Java® Language Specification (-JLS).
The name parameter in line 9 is reassigned, but that does not affect the original name local variable in line 18. Line 18 gives you a hint; that String reference is marked final, so you can be confident it will continue to point to "Rosie" for the whole of its scope. Yes, I have known women called Rosie too. At no point during the method starting line 7 did you actually use name. Whatever information it contained is lost to the remainder of that method because you overwrote it with a reference to "Wendy" before doing anything with name. that is why you are getting the IDE warning.

Neither name nor myName is in the String pool. They are local variables, so they are on the stack. The two String literals are in the String pool, but the String pool has no relevance to the current problem.
When you pass myName, the method argument is not a final variable, but a copy of the reference behind myName. Changes in the method are not reflected back. As the answer says, Java® uses pass by value. Always.

You might think that line 11 will set the single element in speed to 11, but the preceding line reassigned speed. If you comment out line 10, you will retain a reference to a mutable object and the change of its state will be reflected in line 24. But the first reassignment in line 10 means the speed array in lines 17‑24 doesn't show any changes; since “new” arrays are filled with null/zero/false, it will print out as, “[0]”.

Again you cannot change the mySkier reference in the distant method. Solution A is wrong in that it could never print null. Use of a null would cause it to throw an exception. What is a polymorphic parameter? I have never heard of it. The only polymorphic things are instance methods, and they are polymorphic on the object they are called on.  The method in line 7 is static and static methods don't take part in polymorphism. Yes, racer and mySkier point to the same object in lines 7‑11 (not line 12), so the changes to the private field are reflected back and shown in line 22: 18. Though why an 18‑year‑old “racer” would be travelling at a speed of 0 is beyond me

Don't sit the exam now; write lots of code for applications and become proficient writing cod first.
 
Jeff Stam
Greenhorn
Posts: 7
 
Campbell Ritchie
Marshal
Posts: 67443
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeff Stam wrote:@Cambell Ritchie thanks . . .

That's a pleasure. I am afraid neither of your links would open.
 
Jeff Stam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha I didn't noticed the attachment button, this will work I hope.
20200203_174407_compress65.jpg
[Thumbnail for 20200203_174407_compress65.jpg]
20200203_175252_compress68.jpg
[Thumbnail for 20200203_175252_compress68.jpg]
 
Campbell Ritchie
Marshal
Posts: 67443
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that's what they mean by polymorphic parameters.
 
Do you want ants? Because that's how you get ants. And a tiny ads:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!