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

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.name.equals(b name))}

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. The 'equals' method in the 'Book' class correctly overrides the method from the 'Object' class to compare the 'name' fields of two 'Book' objects. In the given code fragment, 'b1' and 'b2' have different 'id' values but the same 'name' value. Although their names are the same, the equals method was designed to print false by default unless the 'name' comparison is true. Therefore, contrary to the logic indicating 'true', the initial boolean output remains 'false'.

Discussion
M_JawadOption: A

the code prints true

Svetleto13Option: A

A,tested

asdfjhfgjuaDCVOption: A

A is the correct answer

steefaandOption: A

A is correct.

duydnOption: A

Code compile successfully and override the equals() correct -> TRUE -> A

r1muka5Option: A

Correct answer is A: public class Book { int id; String name; public Book(int id, String name) { this.id = id; this.name = name; } @Override public boolean equals(Object obj) { boolean output = false; Book b = (Book) obj; if (this.name.equals(b.name)) { output = true; } return output; } } public class Main { 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)); } }