I just took a quiz for my programming languages class and came across this question:
Assuming call by name, what are the results of following code:
public class MyClass {
static int i = 1;
static float f(int x, int i){
int s = 0;
for(i = 0; i <3; i++){
s = s + x;
}
return s;
}
static void g(){
int[] a= {10,30,50};
int[] b = {20,40,60};
System.out.println(f(i, i));
i = 1;
System.out.println(f(a[i],i));
i = 1;
System.out.println(f((a[i] *b[i]), i));
}
public static void main(String[]args){
g();
}
}
The above code is exactly as presented on my quiz, but it was on paper at the time so I did the calculations manually and got the results 3.0, 90.0, and 3600.0
. After running the code on my own computer, the results are the same as what I calculated.
However, the question was multiple choice and the closest option available was 3.0, 90.0, and 4400.0
. This leads me to assume that the words "Call by name" change how the function f
is called on the line System.out.println(f((a[i] *b[i]), i));
Can someone please explain how 4400.0 is possible in Java or C++?