This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a mock exam question

 
suneeta prattipati
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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

 
suneeta prattipati
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jonathan & Anrup,
Thanks. I got it now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic