Calculate the values of variables a and b after the call to
Calculate the values of variables a and b after the call to the subprogram guessWhat returns, where a is a global variable and b is local to the caller. Assume that all parameters are passed by pass-by-name, and 0-based indices are used for arrays.
// global variable
int[] a = new int[] { 10, 20 };
// subprogram void guessWhat(int x, int y) {
x = (x + 2) % 2;
y = (y + 2) % 2;
a[x] = a[y] + 1;
}
// caller
int b = 1;
guessWhat(b, a[b]);
Solution
before passing the bariable values to the method guessWhat(b,a[b]) as inputs
the value of b is 1 , and
the value of a[b] is 20
the when we call the method guessWhat(b,a[b]) by passing the values guessWhat(1,20) as input arguments
then inside the that method some oprations is performed
guessWhat(int x,int y) here we are passing the value of b to x and a[b] to y
x=(x+2)%2;
x=(1+2)%2
x=3%2
x=1 (when divided by 3 with 2 the remainder is 1)
y = (y + 2) % 2;
y=(20+2)%2
y=22%2
y=0(when divided by 22 with 2.The remainder is zero)
a[x]=a[y]+1;
a[1]=a[0]+1(as x=1 and y=0)
a[1]=10+1
a[1]=11
then a[1] will be overridden by the value 11
then after the sub program got executed the value of b is 1
and the value of a[b] is 11
_____________Thank You
