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; }

30th Aug 2016, 1:17 PM
Mitchell Carroll
Mitchell Carroll - avatar
6 Answers
+ 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.
30th Aug 2016, 3:57 PM
Ousmane Diaw
+ 1
5
30th Aug 2016, 2:56 PM
Agha Asad Khan
Agha Asad Khan - avatar
+ 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.
31st Aug 2016, 6:16 PM
Abhishek Johari
Abhishek Johari - avatar
+ 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;
6th Nov 2016, 1:35 PM
Mowlana
Mowlana - avatar
0
For object variables, value is the address of the created object.
31st Aug 2016, 10:05 AM
WPimpong
WPimpong - avatar
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
12th Sep 2016, 12:58 AM
Srj More
Srj More - avatar