For questions 5-8, consider a class that stores 2 int values. These values can be assigned int values with the messages set1(x) and set2(x) where x is an int, and these values can be accessed through get1( ) and get2( ). Assume that y and z are two objects of this class. The following instructions are executed:
y.set1(5);
y.set2(6);
z.set1(3);
z.set2(y.get1( ));
y = z;
5) The statement z.get2( ); will
a) return 5
b) return 6
c) return 3
d) return 0
e) cause a run-time error
Answer: a. Explanation: The statement y.get1( ) returns the value 5, and therefore the statement z.set2(y.get1( )) sets the second value of z to be 5, so that z.get2( ) returns 5.
6) The statement y.get2( ); will
a) return 5
b) return 6
c) return 3
d) return 0
e) cause a run-time error
Answer: a. Explanation: The statement y = z; causes y and z to be aliases where y.get2( ); returns the same value as z.get2( );. Since z.set2(y.get1( )); was performed previously, z and y's second value is 5.
7) If the instruction z.set2(y.get1( )); is executed, which of the following is true?
a) (y = = z) is still true
b) (y.get1( ) = = z.get1( )) but (y.get2( ) != z.get2( ))
c) (y.get1( ) = = z.get1( )) and (y.get2( ) = = z.get2( )) but (y != z)
d) (y.get1( ) = = z.get2( )) and (y.get2( ) = = z.get1( )) but (y != z)
e) the statement causes a run-time error
Answer: a. Explanation: Since y=z; was performed previously, y and z are aliases, so that a change to one results in a change to the other. The statement z.set2(y.get1( )); is essentially the same as z.set2(z.get1( )); or y.set2(y.get1( )); and in any case, it sets the second value to equal the first for the object which is referenced by both y and z.
8) If the instructions z.set2(5); and y.set1(10); are performed, which of the following is true?
a) (y = = z) is still true
b) (y.get1( ) = = z.get1( )) but (y.get2( ) != z.get2( ))
c) (y.get1( ) = = z.get1( )) and (y.get2( ) = = z.get2( )) but (y != z)
d) (y.get1( ) = = z.get2( )) and (y.get2( ) = = z.get1( )) but (y != z)
e) this statement causes a run-time error
Answer: a. Explanation: Since y=z; was peformed previously, y and z are aliases meaning that they refer to the same object. The statement z.set2(5); causes a change to the object's second value while y.set1(10); causes a change to the object's first value but neither change the fact that y and z are aliases.