Java Reference
In-Depth Information
5. What is the result of compiling the following class?
1. public class Book {
2. private int ISBN;
3. private String title, author;
4. private int pageCount;
5.
6. public int hashCode() {
7. return ISBN;
8. }
9.
10. public boolean equals(Object obj) {
11. if(!(obj instanceof Book)) {
12. return false;
13. }
14. Book other = (Book) obj;
15. return this.ISBN == other.ISBN;
16. }
17. }
A. The class compiles successfully.
B. Line 6 causes a compiler error because hashCode does not return a unique value.
C. Line 10 causes a compiler error because the equals method does not override the par-
ent method correctly.
D. Line 14 does not compile because the ClassCastException is not handled or declared.
E. Line 15 does not compile because other.ISBN is a private field.
6. What is the outcome of the following statements? (Select one answer.)
6. String s1 = “Canada”;
7. String s2 = new String(s1);
8. if(s1 == s2) {
9. System.out.println(“s1 == s2”);
10. }
11. if(s1.equals(s2)) {
12. System.out.println(“s1.equals(s2)”);
13. }
A. There is no output.
B. s1 == s2
C. s1.equals(s2)
D. Both B and C
Search WWH ::




Custom Search