• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

from mock test

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)Collection<?> c = new ArrayList<String>()

Which line inserted independtly at line 1 will allow to compile?

a)c.add(new Object());

b)c.add("Java");

c)c.add(null);

d)All 3 lines above inserted independtely will allow to compile

e)All 3 lines above inserted independtely will not allow to compile

Answer :c


Why this code allows add method?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there...

ArrayList implements List Interface, which extends the Collection Interface...

ArrayList has the add()method to add objects,primitives etc., to them...

so, there is nothing wrong with the code of your's... that's why its compiling..

understood...
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When it is a wild card, you cannot add any thing into your list. That's what code is doing. Arraylist cannot hold any primitive, you have to wrap primitive.
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
null is allowed because it is member of every type
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic