• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

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
reply
    Bookmark Topic Watch Topic
  • New Topic