Win a copy of Modern frontends with htmx this week in the Spring 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 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

OCP Practice Exams Sef-Assessment Test 1 Q14

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did not understand solution to the following question:



What is the most likely result?
A. [3, 7, 5]
B. [3, 7, 5, 9]
C. [3, 7, 5, 9, 1]
D. Compilation fails.
E. An exception is thrown at runtime.

Answer (for Objective 7.1):
D is correct. MyPancake.doStuff() must be marked public. If it is, then C would be
correct.
A, B, C, and E are incorrect based on the above.
 
Greenhorn
Posts: 22
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeet

This question is testing your knowledge about method "overriding" rather than any generics.

All interface methods are implicitly "public abstract" and when you implement any interface you basically overriding its methods. One of the "overriding" rule is the new method can not be MORE restrictive than the original method but could be less restrictive.

in your case we changing the access type from public to default and thus making it more restrictive which violates the method overriding rules.

Hope that helps.
 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it:) thank you so much
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeet Jain wrote:Got it:) thank you so much



As for me, I am confused about why x is now [3,7,5,9,1] instead of [3,7,5,9]. I thought a new ArrayList y was made from X. so, Y =[3,7,5,9] and then 1 is added to the y arrayList and not X. So, anyone care to explain why IF the scope of the doStuff had been public, the right answer would be C?

Thanks
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The result is:
[3, 7, 5, 9, 1]

Note that in doStuff() you are adding "9" do list named x and then the reference to that list is stored in variable y.
This results in x and y pointing to the same instance of the list.
So, whatever you do to y is reflected in x.

You are wrong about new ArrayList being created. The only thing that happens is passing reference to an ArrayList object created at line 4.
 
henry joe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:The result is:
[3, 7, 5, 9, 1]

Note that in doStuff() you are adding "9" do list named x and then the reference to that list is stored in variable y.
This results in x and y pointing to the same instance of the list.
So, whatever you do to y is reflected in x.

You are wrong about new ArrayList being created. The only thing that happens is passing reference to an ArrayList object created at line 4.




Thank you alot. I get it now.
 
I was her plaything! And so was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic