File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assesment test question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assesment test question" Watch "Assesment test question" New topic
Author

Assesment test question

Lavanya Raguram
Greenhorn

Joined: Apr 03, 2006
Posts: 27
which of following correctly describes how variablers are passed to methods?

a)Arguments are always passed by value
b)Arguments are always passed by reference
c)Arguments that are primitive types are passed by value
d)Arguments that are passed with & operator are passed by reference

Pls explain
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

c is correct. there is no &variable name in java to refer to the address of a variable. Object types are passed by reference and primitives are passed by value which makes a and b incorrect.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
Hi John,

I've a doubt here. When we pass any object references, a copy of that is passed to the function and I think thats the reason why we cannot change the reference itself but are allowed to change the object state that it is referring. In that case don't you think "option a" is correct.

Asha


SCJP 1.5
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

When passing objects the reference value is passed rather than the entire object. However it can be argued that you are still passing a value ( which contains that reference ). The SCJP book written by Kathy and Bert does say that java is pass by value for all variables. From the exam's point of view i guess you could say a is also right. Thanks for pointing that out. If someone thinks the answer could be otherwise please feel free to correct me.

Reference page 204: SCJP 1.5 by Kathy and Bert, line 3 under the heading "Does java use pass by value-semantics"
[ April 16, 2006: Message edited by: John Meyers ]
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hey guys,

Don't get confused about changes in the object state thinking it's passed as reference.

Everything in Java is passed by value, even object references.

For example, even though you can change a state of an object passed as reference to a given method, such reference is still passed by value. So the correct answers are A and C.

Let me explain by using code :



Output is : 999 2 3 4 5 6

The key point here is that even though you can change the array elements or any object reference's state, you can NEVER change the original variable passed to the method. You can NEVER change the memory place where the original reference is "pointing to" from within the method. So the argument is passed ALWAYS by value.
[ April 16, 2006: Message edited by: Edisandro Bessa ]

"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
The key is to recognize that object variables are just references to an object and not the object itself.

Then, object reference variables are passed by value just like any other variable.

Let's say we have these methods:




When String person = "Elvis" and saySomething(person) are executed:



We can conclude that person and someone are two different reference variables poiting to the same object.

Then after String someone = "Hello "+someone



Altering the variable reference of someone will not alter the reference value of hello. Hence we can conclude that references are passed by value, altering the reference of an argument will not alter the reference of the actual parameter.

I hope this helps.
[ April 16, 2006: Message edited by: Edwin Dalorzo ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assesment test question