Consider the following program segment in a Java-like programming language, clas
ID: 3616992 • Letter: C
Question
Consider the following program segment in a Java-like programming language,
class Sample {
static int i;
static int M[] = new int[3];
static void pass (int x, int y, int z)
{ i = z;
z = y;
x = x + 1;
y = i;
}
static void main (String argv[])
{ i = 1;
M[1] = 3;
M[2] = 1;
pass(i, M[i], M[i+1]);
System.out.println (i, M[1], M[2]);
}
}
What numbers will be printed by this program if parameter correspondance is
implemented by
==>> call by name?
I already knows the numbers, but I don't know how I got them?
They are (2, 3, 2)
So, if anybody knows please explain:
Explanation / Answer
Function Call: pass(i, M[i], M[i+1]); //here i=1, M[i]=3, M[i+1] =1Next it enters the function PASSIn PASS:i=z //here z=M[i+1] = 1, therefore i becomes 1z=y //here y=M[i] =M[1]=3, therefore z=M[2]=3x = x+1 //here x=i=1, therefore i =i+1 = 2y =i, therefore y will be become i which is equal to 2Therefore the final values of i =2, M[1] = 3, M[2] = 2