File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes marcus mock exam-- reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "marcus mock exam-- reference" Watch "marcus mock exam-- reference" New topic

marcus mock exam-- reference

chao-long liao
Ranch Hand

Joined: Jul 29, 2001
Posts: 78

1) 10030
2) 20030
3) 209930
4) 10020
Ans is 4,in my opinion,I think the answer should be 10010,because v = vh, so the
System.out.print( v.i ) should be 10,so what's the problem ?
[ Jess added UBB [code] tags to preserve whitespace -- check 'em out! ]
[ July 13, 2002: Message edited by: Jessica Sant ]
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
Ok, the thing to remember here is that all variables are passed by value and not by reference. However, when the variable is a reference to an object, then any changes made to the object through that reference will be apparent to other methods. in the class another, you first change the variable i to 20 through the reference v. then you create a new instance of calss ValHold and store the reference to vh which has a new copy of the variable i which has a value of 10. Ofcourse when you change the variable i through the reference v, the change is apparent to the amethod method.
[ July 13, 2002: Message edited by: Amir Ghahrai ]

Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 34
Here is a clearer picture of what Amir said:

At 0 you create a reference var and point it to an object created by new operator in memory
The variable v at 2 is a local member of the meth another(), when u call this method you are passing out object (at 1) of the ValHold that you created at 0.This means that v refers to an object that lies in memory created at 0.

Now at 3 when you set i=20, you are changing the value of object reference ,laying in memory, and the one referred (pointed) by v at 0
so at 3 there is a global change to the reference created by new operator at 0
At 4 you set the meth another()'s local variable v to refer to a new object created by new operator, which has i=10 .
When you come out of another() the local var v fails to exist and therefore the new reference that it had no is longer pointed by any reference( a perfect member for gc())
But inside amethod() v still holds the reference of object created at 0 and whoose member was altered at 3.
Semantics remains the same as mentioned by Amir..
Bindesh Vijayan
I agree. Here's the link:
subject: marcus mock exam-- reference
It's not a secret anymore!