Forums Register Login

mock test for generics & collections

+Pie Number of slices to send: Send
Can anybody give me a link which has generics & collections examples? I need so.
Thanks in advance.
+Pie Number of slices to send: Send
Hi Dolly,

This has some questions.
http://www.javabeat.net/javabeat/scjp5/mocks/index.php

Regards
Padma
+Pie Number of slices to send: Send
import java.util.*;

class Vehicle {}

class Car extends Vehicle {}

class Bus extends Vehicle {}

class TestSamp {

public static void main(String [] args) {

ArrayList<Car> a = new ArrayList<Car>();

a.add(new Car());

ArrayList b = a;

ArrayList<Bus> c = (ArrayList<Bus> b;

c.add(new Bus());

for (Object obj : b)

System.out.println(obj);

}

}


Here b=new ArrayList<Car>(), when we add new bus then it is adding in a car arraylist. So it can not compile.
I want to make sure that I am right or not?
+Pie Number of slices to send: Send
Your code compiles with a warning in line


It also runs fine.

[edit] I hate smilies. BTW, why is it that smiley replacement is also done inside code blocks?
[ June 26, 2007: Message edited by: Sergio Tridente ]
+Pie Number of slices to send: Send
class Box<T>
{
private T theObject;
public Box(T arg) { theObject = arg; }
public Box() { }
}
class Test
{
public static void main(String[] args)
{
Box<String> b1 = new Box<String>("Java"); //line 1
Object<String> b2 = new Box<String>("Struts"); //line 2
Box<String> b3 = new Box("EJB"); //line 3
Box b4 = new Box<String>("Ajax"); //line 4
Object b5 = new Box<Object>(); //line 5
}
}



what will be the effect of the program?

a)compile time error at line 5 only
b)compile time error at line 2 only
c)compile time error at line 2 and line 5
d)compile time error at line 3 and line 4
e)Run time error



Answer : b



Explanation:
Object class doesnot take any parameters

Why is it so? I am not getting. Can anyone explain?
+Pie Number of slices to send: Send
Shouldn't it give a runtime exception while we are trying to cast b ( which is a) to c?
+Pie Number of slices to send: Send
hi


class Box<T>
{
private T theObject;
public Box(T arg) { theObject = arg; }
public Box() { }
}
class Test
{
public static void main(String[] args)
{
Box<String> b1 = new Box<String>("Java"); //line 1
Object<String> b2 = new Box<String>("Struts"); //line 2
Box<String> b3 = new Box("EJB"); //line 3
Box b4 = new Box<String>("Ajax"); //line 4
Object b5 = new Box<Object>(); //line 5
}
}



No idea. I rather thought there shouldn't be any errors because if we can say

List <Integer> a = new ArrayList<Integer>();

And List is a super class of ArrayList, so we can definitely assign object of ArrayList to the reference to super class type(List) in this case.

So if we follow the same reasoning then the number#2 should be allowed but indeed it is not. Any thoughts.

Note: There is one suggestion. Instead of continuing the same thread you can start a new thread because the subject is misleading.

Regards
Padma
+Pie Number of slices to send: Send
 

Originally posted by dolly shah:
Object<String> b2 = new Box<String>("Struts"); //line 2



Object class is not generic, i.e. it does not take a type argument.
+Pie Number of slices to send: Send
Hi

Thanks!!! we missed the trivial point there.

Regards
Padma
Men call me Jim. Women look past me to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1919 times.
Similar Threads
Generics
generics
Generics
generics
Generics
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:31:11.