The pages of this web site are not formatted to conserve paper, but my new book (ISBN: 0-9745862-0-X) is formatted to save paper, save your printer cartridge, save a loose-leaf binder, and save money. If you prefer to work my exams from printed pages, then give your printer a rest and buy my new book.
Today, you can find my book at amazon.com.
| 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 | The method m1 is invoked by the method invocation expression m1(pet1,pet2). The value of the reference variable denoted by the argument pet1 is used to initialize the method parameter r1. Inside of method m1, the method invocation expression r1.setName("Bird") uses the copy of the value of the argument pet1 to assign a new name to the instance of GFC301 that is referenced by the local variable pet1 in the main method. Generally speaking, a reference parameter can be used to invoke methods on the referenced object and change the state of the object to the extent provided by the object's methods. The method invocation expression m1(pet1,pet2) has a second argument pet2, and the value of pet2 is used to initialize the method parameter r2. Inside of method m1, the assignment expression r2 = r1 changes the value of the method parameter r2; but the local variable of the main method denoted by the argument pet2 appearing in the method invocation expression m1(pet1,pet2) remains unchanged. |
| 6 | a | Prints: Dog,Cat | The method m1 is invoked by the method invocation expression m1(pet1,pet2). A copy of the reference argument pet1 is assigned to the method parameter pet1. Inside the body of method m1, the assignment expression pet1 = new GFC303("Fish") assigns a reference to a new instance of GFC303 to the method parameter pet1; but the argument pet1 that appears in the method invocation expression m1(pet1,pet2) and the local variable pet1 that is declared in the main method remain unchanged. The method invocation expression m1(pet1,pet2) has a second argument pet2, and a copy of pet2 is assigned to the method parameter pet2. Inside of method m1, the assignment expression pet2 = null changes the value of the method parameter pet2; but the argument pet2 appearing in the method invocation expression remains unchanged in the main method. |
| 7 | b | Prints: 1,3 | The method m1 is invoked by the method invocation expression m1(i1, i2). The argument i1 denotes a local variable of type int[] that is declared in the main method. The value of the argument is a reference to the array, and the argument value is used to initialize the method parameter i1 of method m1. Inside the body of m1, the expression i1 = i2 sets the value of parameter i1 to the value of parameter i2, but the change in the value of the parameter i1 does not change the original argument value or the local variable i1 of the main method that the argument denotes. Similarly, the assignment expression i2 = i3 in method m1 does not change the value of the local variable i1 declared in the main method. |
| 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 parameters. |
| 9 | a | Prints: 1,1 | The method m1 is invoked by the method invocation expression m1(i1). The argument i1 denotes the static member variable i1. Inside the declaration of method m1, the method parameter i1 shadows the static member variable i1. The assignment expression i1 = i2 assigns the value of the member variable i2 to the method parameter i1, but the member variable i1 remains unchanged. Inside of method m1, the member variable i2 is not shadowed; so the assignment expression i2 = i3 assigns the reference value contained by the method local variable i3 to the member variable i2. This question demonstrates that argument values are passed to method parameters by value, and the method parameter is only a copy of the argument value. A change made to the method parameter does not change the value of any variable that is shadowed by the parameter and does not change the value of the argument appearing in the method invocation expression. |
| 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. |