exam questions

Exam 1z0-809 All Questions

View all questions & answers for the 1z0-809 exam

Exam 1z0-809 topic 1 question 182 discussion

Actual exam question from Oracle's 1z0-809
Question #: 182
Topic #: 1
[All 1z0-809 Questions]

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?

  • A. The program prints true.
  • B. The program prints false.
  • C. A compilation error occurs. To ensure successful compilation, replace line n1 with: boolean equals (Book obj) {
  • D. A compilation error occurs. To ensure successful compilation, replace line n2 with: System.out.println (b1.equals((Object) b2));
Show Suggested Answer Hide Answer
Suggested Answer: B 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
Manuel7000
Highly Voted 3 years, 4 months ago
Agree and tested
upvoted 6 times
...
steefaand
Most Recent 5 months, 4 weeks ago
Selected Answer: B
B is correct.
upvoted 1 times
...
duydn
10 months, 2 weeks ago
Selected Answer: B
Totally FALSE, the equals() method just compare the id -> false -> B is correct
upvoted 1 times
...
Alazem
10 months, 3 weeks ago
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 } }
upvoted 1 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago