| No. | Answer | Remark | |
|---|---|---|---|
| 1 | b | Prints: 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. |
| 2 | f | Compile-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. |
| 3 | b | Prints: 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. |
| 4 | a | Prints: 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. |
| 5 | c | Prints: 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. |
| 6 | a | Prints: 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. |
| 7 | b | Prints: 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. |
| 8 | c | Prints: 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. |
| 9 | a | Prints: 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. |
| 10 | c | Prints: 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. |
| 11 | d | Prints: 3,1 | Inside of method m2, the local variables i1 and i2 remain unchanged while the shadowed instance variables are changed. |