Java is the future !: Quiz of the Day | November 6, 2010

Saturday, November 6, 2010

Quiz of the Day | November 6, 2010

What value is printed out by executing the above code?

a. 2.7 
b. 1.7 
c. 0.0 
d. -1.0


Solution : Choice A is the correct answer.

In Java, all parameters are passed by value. In case of primitives, the copy of the variable is passed, while in case of object references, it's the copy of the reference that is passed. 

When the argument is a primitive type, pass-by-value means that the method can change the value of the passed argument (in the method scope) but, the called method cannot change the value of the variable in the calling method.

When the argument is of reference type, pass-by-value means that the method cannot change the object reference, but can invoke the object's methods and modify the accessible variables within the object.

In the example above, d is passed as value, hence the change done in the doMinus() method doesn't reflect on d outside the doMinus() method. Thus, the value printed is 2.7


Source : "Facebook community"

No comments:

Post a Comment