Java OCA OCP Practice Question 441

Question

What will the following program print?

public class Main{ 
  static String str = "Hello World"; 
  public static void changeIt (String s){ 
    s = "Good bye world"; 
   } //  w  w w.  ja  v a 2 s .  co  m
  public static void main (String [] args){ 
    changeIt (str); 
    System.out.println (str); 
   } 
} 

Select 1 option

  • A. "Hello World"
  • B. "Good bye world"
  • C. It will not compile.
  • D. It will throw an exception at runtime.
  • E. None of the above.


Correct Option is  : A

Note

Java supports Pass by Value for everything, i.e. primitives as well as Objects).

Primitives are always passed by value.

Object "references" are passed by value.

So it looks like the object is passed by reference but actually it is the value of the reference that is passed.

An example:


Object o1 = new Object (); 

Let us say, the object is stored at memory location 15000.

Since o1 actually stores the address of the memory

location where the object is stored, it contains 15000.

Now, when you call someMethod(o1); the value 15000 is passed to the method.

Inside the method someMethod():

someMethod ( Object localVar)  {  

} 

localVar now contains 15000, which means it also points to the same memory location where the object is stored.

Therefore, when you call a method on localVar, it will be executed on the same object.

However, when you change the value of localVar itself, for example if you do localVar=null, it then it starts pointing to a different memory location.

But the original variable o1 still contains 15000 so it still points to the same object.

This is what happens in the this question.

In the method changeIt(...) you are giving a new value to the local variable but the original reference remains the same.




PreviousNext

Related