Examveda
Examveda

Determine output:
public class Test{
        public static void main(String[] args){
                int[] x = {1, 2, 3, 4};
                int[] y = x;

                x = new int[2];

                for(int i = 0; i < x.length; i++)
                        System.out.print(y[i] + " ");
        }
}

A. 1 2 3 4

B. 0 0 0 0

C. 1 2

D. 0 0

E. None of these

Answer: Option C


This Question Belongs to Java Program >> Array

Join The Discussion

Comments ( 2 )

  1. Santhosh
    Santhosh :
    3 years ago

    previously y is initialized with x in that case y[4] and then later x is initialized with size 2 that means in for loop x.length means it size is 2 but initial value for "i" in for loop is given as zero that means it satisfies two conditions that is 0

  2. Janitha Prabath
    Janitha Prabath :
    8 years ago

    a good explanation : http://stackoverflow.com/questions/34462460/how-does-x-new-int2-work-in-this-java-code

Related Questions on Array