• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Few remarks about Online Mock Exam 2 (Java OCA 8 Programmer I Study Guide)

 
Shiva Kotha
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In question 56:



The answer says - Answer: F

But there is no methods in LocalDate class with year and month or year and date. So the answer should be D. -> The code does not compile.

 
Shiva Kotha
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
60. Which of the following are true? (Choose all that apply)



A. The code compiles.
B. The code does not compile.
C. If statements that do not compile are removed, the output contains one.
D. If statements that do not compile are removed, the output contains two.
E. If statements that do not compile are removed, the output contains three.
F. If statements that do not compile are removed, the output contains four.

The answer is mentioned as B and D. But D is not correct. s1.equals(s2) returns false in this case. So answer is only B.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shiva Kotha,

First of all, a warm welcome to CodeRanch!

Shiva Kotha wrote:The answer says - Answer: F

But there is no methods in LocalDate class with year and month or year and date. So the answer should be D. -> The code does not compile.

Shiva Kotha wrote:60. Which of the following are true? (Choose all that apply)

The answer is mentioned as B and D. But D is not correct. s1.equals(s2) returns false in this case. So answer is only B.

You are correct on both occasions! These issues were already reported here and are already on the official errata overview.

Hope it helps!
Kind regards,
Roel

PS. Because both topics were about the same mock exam, I merged both topics into one. Hope that's ok with you!
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s1.equals(s2) is false is unfathomable...
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:s1.equals(s2) is false is unfathomable...

It's not at all! StringBuilder doesn't override the equals() method of class Object. So the equals() method will only return true if s1 and s2 refer to the same StringBuilder instance.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Guillermo Ishi wrote:s1.equals(s2) is false is unfathomable...

It's not at all! StringBuilder doesn't override the equals() method of class Object. So the equals() method will only return true if s1 and s2 refer to the same StringBuilder instance.

Is there a method behind that madness?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:
Roel De Nijs wrote:
Guillermo Ishi wrote:s1.equals(s2) is false is unfathomable...

It's not at all! StringBuilder doesn't override the equals() method of class Object. So the equals() method will only return true if s1 and s2 refer to the same StringBuilder instance.

Is there a method behind that madness?

Sure! That's why it's not madness but common sense

A StringBuilder's main and only purpose is to build strings (without creating a new String on every operation), hence it's name So it's kind of a helper class, and you only will use it to do a bunch of string manipulations (append, replace, insert, remove,...) to end up with an actual String (by invoking the toString() method). So the Java API developers decided not to implement/override the equals() method, because nobody will ever be interested to know if two StringBuilders are meaningfully equal. Everybody will use them to create strings, not to compare them.

If I look at my own codebase of our application, only a very tiny percentage of our classes override the equals() (and hashCode()) method(s) for exactly the same reason. For example: a repository is used to access the database and perform some CRUD (create, read, update and delete) operations, not to compare if this repository is meaningfully equal to another one.

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic