0

tutorial problem

In the sololearn tutorial for the value and reference types, i come across this question "What is the output of this code? public static void main(String[ ] args) { int x = 4; square(x); System.out.println(x); } static void square(int x) { x = x*x; }" witch simply boils down to 4^2, witch is 16. the problem is that the area where you input the answer only outputs one character. encoding it in hex doesen't work either. the "right" answer is 4. is there something i'm missing or is the creator of the tutorial that oblivious?

27th Jan 2017, 2:29 PM
Ninjacat
2 Answers
+ 1
the answer is 4 as the function modifies x only locally and does not return the modified value to main. so as you print x from the main it is still 4. only if you had the print within the function it would output 16.
27th Jan 2017, 5:44 PM
ifl
ifl - avatar
0
4
3rd Oct 2022, 3:05 AM
Sujit suresh shinde