Hi everyone, I need an explanation for the foll ques from Mind Qclass MyPoint { void myMethod() { int x, y; x = 5; y = 3; System.out.print( " ( " + x + ", " + y + " ) " ); switchCoords( x, y ); System.out.print( " ( " + x + ", " + y + " ) " ); } void switchCoords( int x, int y ) { int temp; temp = x; x = y; y = temp; System.out.print( " ( " + x + ", " + y + " ) " ); } } What is printed to standard output if myMethod() is executed? a) (5, 3) (5, 3) (5, 3) b) (5, 3) (3, 5) (3, 5) c) (5, 3) (3, 5) (5, 3)
Jonathan Jeban
Ranch Hand
Joined: Oct 08, 2000
Posts: 52
posted
0
The Answer is (c). Reason : Whatever changes u make inside a method will not be reflected back . Remember u are sending only a copy of the parameters to the method. Hope this helps... Jeban
anrup kris
Greenhorn
Joined: Oct 22, 2000
Posts: 17
posted
0
(5,3)(3,5)(5,3) would be printed out to standard output. When you enter amethod() and execute : System.out.print( " ( " + x + ", " + y + " ) " ); (5,3) is written out. Then a copy of the instance variables is passed to the method switchCoords(x,y).Here x and y are arguments to switchCoords() method and are local to the method.The coordinates are swapped,and (3,5) gets printed in the System.out.print() call from within switchCoords() method. The instance variables x,y are unaltered by the switchCoords() call.So the next print stmt writes out (5,3) Hope this helps