Java OCA OCP Practice Question 1453

Question

What will be the contents of s1 and s2 at the time of the println statement in the main method of the following program?

import java.util.*; 
public class Main{ 
   public static void main (String args []){ 
      Stack s1 = new Stack(); 
      Stack s2 = new Stack(); 
      processStacks(s1,s2); /*  w  ww . ja v a 2 s.c  o m*/
      System.out.println  (s1 + "    "+ s2); 
    } 
   public static void processStacks (Stack x1, Stack x2){ 
      x1.push  (new Integer  ("100")); //assume that the method push adds the 
      x2 = x1; 
    } 
  } 

Select 1 option

  • A. [100] [100]
  • B. [100] []
  • C. [] [100]
  • D. [] []


Correct Option is  : B

Note

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.




PreviousNext

Related