• 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:

javabeat mock test

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 14:The question is about arrays and generics. Which of the following lines can be compiled?
a)Basket<Apple>[] b = new Basket<Apple>[10];
b)Basket<?>[] b = new Basket<Apple>[10];
c)Basket<?>[] b = new Basket<?>[10];
d)public <T> T[] test() {return null;}
e)public <T> T[] test() {return new T[10];}


All of them seem to be correct to me except C
Please explain me the concept
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a and d will compile, others wont!
 
Shaili Merchant
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer that they have provided is c&d
Please do provide me an explanation hw to go bout solving this .
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answers they provided are true, i checked it (didnt know also).

As far as i learned by now, you are only allowed to instantiate an Array of generic types if the generic type symbol is ?
e.g. new List<String>[3]; is not allowed,
but new List<?>[3]; is.
Therefor you can only declair List<?> []l ; as a variable to this construct.



The other point is that in a generic Method we can not
instantate the generic type, eg:

<T> T method(T t){
return new T(); //not ok !!! What would T be?
}
but we can return null or also return ((T) new XObject()), but for the latter we should write <T extends XObject>

Hope this is right, thats it as far as i understand
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

to add one more similar point,

we cannot perform an instanceof check with a type parameter
a instanceof T //compiler error
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All

I am realy confused with the
above answer as C and D


i thought that the answer is A

Please if anyone can ellaborate and explain
the concept of
Generics with Arrays.
or any link which explains this concept.....

It will be realy kinda of you.

Thanks in advance.
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic