File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a mock exam question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply 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.
 
 
subject: a mock exam question
 
Threads others viewed
Static initializer order
static block understanding
Doubt from Whizlab Question
static doubt
static reference to nonstatic var & methods
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture