Java Reference
In-Depth Information
17. C. The class compiles successfully, so A is incorrect. B is incorrect because an equals
method can use any business logic you want to determine if two objects are equal. However,
the rule for proper overriding of equals and hashCode is that if two objects are equal, they
should generate the same hash code. The hashCode method does not properly follow this
rule. Two teams with the same city and mascot but different numberOfPlayers would be
equal but would generate different hash codes. Therefore, D is incorrect and the answer is C.
18. D. The variable x is an int and s is a reference. These two data types are incomparable
because neither variable can be converted to the other variable's type.
The compiler error occurs on line 5 when the comparison is attempted, so the answer is D.
19. A. The code compiles successfully, so C and D are incorrect. The value of b after line 4 is
false. However, the if statement on line 5 contains an assignment, not a comparison. The
value of b is assigned to true on line 5, and the assignment operator returns true, so line 6
executes and displays “Success”.
20. C. The code compiles successfully, so F is incorrect. On line 7, c is incremented to 8 before
being used in the expression because it is a pre-increment. The 8 is added to result, which
is 4, and the resulting 12 is assigned to result and displayed on line 8. Therefore, the
answer is C.
21. E. The class declaration on line 1 contains the static modifi er, which is not a valid
modifi er for a top-level class. This causes a compiler error, so the correct answer is E.
Search WWH ::




Custom Search