Ask a Question
Send an email to me.
 
Java Question and Answer Forums
JavaRanch Big Moose Saloon
Marcus Green's Discussion Forum
java.sun.com Forums, Chat and User Groups
 
Other Resources
Java Language Specification
Java Virtual Machine Specification
Java 2 Platform, Standard Edition, v 1.4.0 API Specification
 
Tutorials
Learning the Java Language
Operator Precedence Chart, Expressions, Statements, Blocks
Programming with Assertions
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1Prints: 1,2  Primitive arguments are passed by value. The method m1 increments the parameter x, and the result is returned. The local variable x of main remains unchanged.  
2Compile-time error  Method m1 is an instance method, and must be invoked with reference to an instance of type GFC402. Method m1 can not be invoked from a static context.  
3Prints: 2,3  Variables of primitive type are passed to methods by value: Only a copy of the value of the variable is passed to the method. While the method works with a local copy of the variable, the original variable remains unchanged by any actions performed on the method parameter. For that reason, method m1 does not change the value of the variable y in the main method. However, method m1 does have direct access to the class variable x and the content of the class variable is modified by method m1.  
4Prints: 1,3  Variables of primitive type are passed to methods by value: Only a copy of the value of the variable is passed to the method. While the method works with a local copy of the variable, the original variable remains unchanged by any actions performed on the method parameter. For that reason, method m1 does not change the contents of the variable y in the main method or the class variable x.  
5Prints: Bird,Cat  Although the reference variable r2 is assigned the value of reference variable r1 in method m1, the reference pet2 remains unchanged in the main method. Object references are passed by value: the invoked method gets a copy of the object reference. The reference parameter r1 can be used to modify the state of the instance referenced by pet1, but r2 can not be used to force pet2 to reference a different instance.  
6Prints: Dog,Cat  Although the reference parameters pet1 and pet2 are reassigned inside of m1, the change has no impact outside of m1. Object references are passed by value: the invoked method gets a copy of the object reference.  
7Prints: 1,3  Although the reference parameters i1 and i2 are reassigned inside of m1, the change has no impact outside of m1. Array references are passed by value: the invoked method gets a copy of the array reference.  
8Prints: 3,1  Method m1 is not able to change the value of the local variables that are declared in the main method and serve as the arguments in the method invocation expression. However, method m1 is able to modify the contents of the arrays that are referenced by the method arguments.  
9Prints: 1,1  Although the reference parameter i1 is reassigned inside of m1, the change has no impact outside of m1. Array references are passed by value: the invoked method gets a copy of the array reference.  
10Prints: 1,3  Although the reference parameters i1 and i2 are reassigned inside of m1, the change has no impact outside of m1. Array references are passed by value: the invoked method gets a copy of the array reference.  
11Prints: 3,1  Inside of method m2, the local variables i1 and i2 remain unchanged while the shadowed instance variables are changed.  

Copyright © 2002-2003, Dan Chisholm
All rights reserved.