• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When to assume that imports are present?

 
Ranch Hand
Posts: 58
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm new to CodeRanch.

I was working through the review questions of the book OCA Study Guide Java SE 8 Jeanne & Scott.

IN the following question, I selected the option: "Code doesn't compile" (which I later found was wrong)


The code doesn't have line numbers. Why do we assume that imports for ArrayList/List are present? Can someone please explain when are we to assume that all necessary imports are present?
Thanks
Swift
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch Charlie. I hope you find the community friendly and helpful.

I would say that you can assume the correct imports are present unless the code fragment starts with line one.
For instance this code fragment is invalid because it starts on line one.

However this code starts are line 35 so it's imports are assumed.

And this code has no line numbers so the imports are assumed as well.
 
charlie swift
Ranch Hand
Posts: 58
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Pete Letkeman
Thanks a lot for replying so quickly. I understand it now
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch and thank you for supplying enough information to identify where the code comes from.
Does the book say anything about assuming imports?

I would think that you shou‍ld assume that is correct code because there are no compiler errors in the code. Otherwise you would say that it won't compile because it is not inside a method. And assuming the code compiles, as PL has told you, what would your answer be? And what would you say if the third line read like this?
List<String> two = new LinkedList<>();
 
charlie swift
Ranch Hand
Posts: 58
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch and thank you for supplying enough information to identify where the code comes from.
Does the book say anything about assuming imports?

I would think that you shou‍ld assume that is correct code because there are no compiler errors in the code. Otherwise you would say that it won't compile because it is not inside a method. And assuming the code compiles, as PL has told you, what would your answer be? And what would you say if the third line read like this?
List<String> two = new LinkedList<>();



@Campbell Ritchie Thanks:)

I think now my answer would be B. (ArrayList implements equals method; compares whether two ArrayLists contain the same elements in the same order).

Even if the third line was replaced with a LinkedList, the answer would still be B ( i think LinkedList also overrides equals() method) . Am I correct in assuming so?

 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing Charlie. I suspect that you are currently preparing for the OCA exam.
If this is the case you may want to look at the OCA Wall Of Fame
https://coderanch.com/wiki/659980/Ocajp-Wall-Fame
This could be a very useful resource and it could save you time, money and effort.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

charlie swift wrote:. . . Am I correct in assuming so?

No.

Go and look in the documentation and that will give you the answer. I think B is correct for both situations, however.
 
charlie swift
Ranch Hand
Posts: 58
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

charlie swift wrote:. . . Am I correct in assuming so?

No.

Go and look in the documentation and that will give you the answer. I think B is correct for both situations, however.



I looked at the docs and came to know that equals() method in AbstractList(the class inherited by LinkedList & ArrayList) compares the size & content ... and

two lists are defined to be equal if they contain the same elements in the same order



Everything is clear now Thanks for all your help:)
 
reply
    Bookmark Topic Watch Topic
  • New Topic