*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a mock exam question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a mock exam question" Watch "a mock exam question" New topic
Author

a mock exam question

suneeta prattipati
Greenhorn

Joined: Oct 16, 2000
Posts: 8
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
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
(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

Joined: Oct 16, 2000
Posts: 8
Hi Jonathan & Anrup,
Thanks. I got it now.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: a mock exam question