0
Can you have different variables with the same label eg. "x"? What is the printed value?
In this code, what is the value of x after it's run: Public void Main(args[]){ Int x = 5; square(x); print.ln(x); } Public void square(int x) { x = x * x; }
6 Réponses
+ 7
The printed value will remain as 5, this is known as pass by copy. You're basically passing a copy of the variable "x" to another variable "x". With pass by copy the original value is inaffected however with pass by reference the original value will be affected.
+ 1
5
+ 1
when square method is called only a copy of x is passed to the method but in main x remains unchanged.so value of x remain 5.
+ 1
x remains 5.
if you want to change the value of x, then you need little bit change
1- assign x: square(x);
2- method square must return int x;
0
For object variables, value is the address of the created object.
0
5 is the correct answer..
as u knw that u call a function square(x);
pass goes on to that function..
and new variable called x act as another variable for that square function which holds the value of previous x ie 5...
and if u want to see the changed value of x..
u just display in square function instead of main