For questions 1-4, assume x and y are String variables with x = "Hello" and y = null.
1) The result of (x = = y) is
a) true
b) false
c) a syntax error
d) a run-time error
e) x being set to the value null
Answer: b. Explanation: x is a String instantiated to the value "Hello" and y is a String that has not yet been instantiated, so they are not the same String. (x = = y) is a condition, testing to see if x and y are the same String (that is, x and y reference the same item in memory), which they don't, so the result is false.
2) The result of x.length( ) + y.length( ) is
a) 0
b) 5
c) 6
d) 10
e) a thrown exception
Answer: e. Explanation: The statement y.length( ) results in a thrown NullPointException because it is not possible to pass a message to an object that is not currently instantiated (equal to null).
3) If the operation y = "Hello"; is performed, then the result of (x = = y) is
a) true
b) false
c) x and y becoming aliases
d) x being set to the value null
e) a run-time error
Answer: b. Explanation: While x and y now store the same value, they are not the same String, that is, x and y reference different objects in memory, so the result of the condition (x = = y) is false.
4) If the operation y = x; is performed, then the result of (x = = y) is
a) true
b) false
c) x being set to the value null while y retains the value "Hello"
d) y being set to the value null while x retains the value "Hello"
e) x being set to y, which it is already since y = x; was already performed
Answer: a. Explanation: When y = x; was performed, the two variables are now aliases, that is, they reference the same thing in memory. So, (x = = y) is now true.