This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Generics Question

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Answer: CDG

why B is incorrect//As Ber extends Animal
why E is incorrect as anything which extends Animal should be able to assigned to an Animal.
 
Ranch Hand
Posts: 114
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anurag Blore:


Answer: CDG

why B is incorrect//As Ber extends Animal
why E is incorrect as anything which extends Animal should be able to assigned to an Animal.


Hi Anurag,

"list<Bear>" is assignable to only a "list<Bear>" and nothing else not its subtype, not its supertype, so it cannot be assigned to list<Animal>.

And about "list<? extends Animal>" is assignable to "list<Animal>"- i think you are right!
"list<? extends Animal>" means anything which is a subtype of animal, including animal can be assigned(Bear,Cow,D,Animal).
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel multiple answers are correct here.
A. The type List<Animal> is assignable to List.
List rawList = null;
List<Animal> animalList = null;
rawList = animalList;
This works fine because you are assigning a list that contains only Animal objects to a raw list that can contain anything.
This is legal , because JDK 1.5 supports assiging a generic list to a raw list.
A- Correct
B. The type List<Bear> is assignable to List<Animal>.
This is wrong, a list of type List<Bear> can be assigned only to lists of type List<Bear> or List<?> [Assign me anything am happy] or List<? extends Bear> [Assing me a list that can contain Bear or anything that extends Bear]
B- InCorrect

C. The type List<Object> is assignable to List<?>.
Since List<?> will accept any type of list this is legal.
C- Correct

D. The type List<D> is assignable to List<? extends Bear>.
List<? extends Bear>: A list that can contain Bear or anything that extends Bear. Since D extends Bear List<D> can be assigned to a reference of type List<? extends Bear>
D- Correct


E. The type List<? extends Animal> is assignable to List<Animal>.
List<Animal>: Can be assinged List<Animal> only. Now List<? extends Animal> means that this list can contain objects of type Animal or anything that extends Animal which can be Dog, Cat and so on. And hence this list cannot be assigned to a List of type List<Animal>.
E- InCorrect

F. The type List<Object> is assignable to any List reference.
If any list reference refers to a raw list then this statement is correct but otherwise its not correct and hence this answer is wrong.
F- InCorrect

G. The type List<? extends Bear> is assignable to List<? extends Animal>.
List<? extends Animal> = List<? extends Bear> ???
Here <? extends Animal> means that this list can contain objects of type Animal or anything that extends Animal which can be Bear , Cow or D.
[Bear, Cow, D]
Now List<? extends Bear> means that this list can contain Bear or D since D extends D.
[Bear, D]
Since [Bear, D] is a subset of [Bear, Cow, D], This legal.
G- Correct

Hence correct answer is
A- Correct
C- Correct
D- Correct
G- Correct
[ January 11, 2008: Message edited by: Deepak Jain ]
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepak,
A,C and D are agreed , but I am still not getting how G is correct.
lets take the following code
import java.util.*;

class Animal {}
class Bear extends Animal {}
class Cow extends Animal{}
class D extends Bear {}
public class UseAnimal
{

public static void main(String []s)
{
List <? extends Animal>a=new ArrayList<? extends Bear>();
}
}

It gives a compiler error.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anurag, when you copy a question from a book or mock exam you need to quote your sources.
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am Sorry. That was a mistake answer G is incorrect.
G) The type List<? extends Bear> is assignable to List<? extends Animal>.
List<? extends Animal> can contain [Bear, Cow, D]
List<? extends Bear> can contain [Bear , D].
Since [Bear , D] does not match [Bear, Cow, D] this is incorrect.

Since List<? extends Animal> can contain [Bear, Cow, D], Suppose this list contains Cow's then obiviously this cannot be assigned to List<? extends Bear> because Cow does not extend Bear.

So the correct answer to the question is

A- Correct
C- Correct
D- Correct
 
Charmy Madhvani
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak,
Lets say let mee change my code to

class Animal2 {}
class Bear extends Animal {}
class D extends Bear {}
public class UseAnimal
{

public static void main(String []s)
{
List <? extends Animal>a=new ArrayList<? extends Bear>();
}
}

Still I get a compiler error though [Animal Bear D ] matches with [Bear D]
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

the answer G is correct. The problem of the code is that you cannot use ? with the object instantiation. Therefore
List <? extends Animal>a=new ArrayList<? extends Bear>();
is not allowed ( The instantiation new ArrayList<? extends Bear>() is not possible). But you can prove G with following code

List <? extends Animal> a;
List <? extends Bear> b;

b = new ArrayList<D>(); // D extends Bear
a = b; // assignment is possible because the wildcard ? extends Animal includes the wildcard ? extends Bear

Hope i explain understandable

following code compiles without error:

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Serg, you are absolutely right.
 
Anurag Blore
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,

Thanks for replying to this post, I was not well so cpuld't check your answers.

I got this question from one of the link from this page
http://cafe4java.com/mockexams/mockexamlist.php

I amnot sure which link as I copied all the Generics related questions and started working.

Thanks
 
It's a tiny ad only because the water is so cold.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic