Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 182

Given:

class Book {

int id;

String name;

public Book (int id, String name) {

this.id = id;

this.name = name;

}

public boolean equals (Object obj) { //line n1

boolean output = false;

Book b = (Book) obj;

if (this.id = = b.id) {

output = true;

}

return output;

}

}

and the code fragment:

Book b1 = new Book (101, "Java Programing");

Book b2 = new Book (102, "Java Programing");

System.out.println (b1.equals(b2)); //line n2

Which statement is true?

    Correct Answer: B

    The program prints false because the equals method compares the id fields of the two Book objects. In this case, b1 has id 101 and b2 has id 102, which are not equal. Therefore, the equals method returns false.

Discussion
Manuel7000

Agree and tested

steefaandOption: B

B is correct.

duydnOption: B

Totally FALSE, the equals() method just compare the id -> false -> B is correct

AlazemOption: B

B, tested class Book { int id; String name; public Book (int id, String name) { this.id = id; this.name = name; } public boolean equals (Object obj) { //line n1 boolean output = false; Book b = (Book) obj; if (this.id == b.id) { output = true; } return output; } } public class Question182 { public static void main(String[] args) { Book b1 = new Book (101, "Java Programing"); Book b2 = new Book (102, "Java Programing"); System.out.println (b1.equals(b2)); //line n2 } }