This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

OCA book Array-list doubt

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chapter 3 : (Page number 158)

19. Which of the following are true? (Choose all that apply)
A. Two arrays with the same content are equal.
B. Two ArrayLists with the same content are equal.
C. If you call remove(0) using an empty ArrayList object, it will compile successfully.
D. If you call remove(0) using an empty ArrayList object, it will run successfully.
E. None of the above.

Answer is:
B, C. An array does not override equals() and so uses object equality. ArrayList does override equals() and defines it as the same elements in the same order. The compiler does not know when an index is out of bounds and thus can’t give you a compiler error. The code will throw an exception at runtime, though.

My doubt is Option B,

ArralyList Equals Method working fine for List<String>,List<Integer> ,List<Double> ..
I mean this one is working Class which overrides equals Method . For Example String class Overrides Equals method.

Class Which is not overriding Equals method -> List Equals is not working,

I tried for Custom Class without overriding equals method with same content same order -> Not working Obviously it should not work

So this line is wrong right ?

Please correct me if I am wrong.
 
Marshal
Posts: 64998
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I shall add your question to our exam forum.

Go through the documentation for ArrayList and read the details of the equals() method. It assumes equality is defined by the equals() method, which means your class must correctly override equals() in order for two instances to be recognised as equal. Otherwise it will use the version of equals() inherited unchanged from Object which tests whether o1 == o2. If you have two custom objects with different contents or have made a mistake overriding equals(), your two Lists will be recognised as not equal. That shows that B C is indeed correct. If you expected equality without overriding equals() please review the definition of equality.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!