This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

mock test for generics & collections

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody give me a link which has generics & collections examples? I need so.
Thanks in advance.
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dolly,

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

Regards
Padma
 
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
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?
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
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
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?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shouldn't it give a runtime exception while we are trying to cast b ( which is a) to c?
 
Padma Asrani
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Padma Asrani
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Thanks!!! we missed the trivial point there.

Regards
Padma
 
She'll be back. I'm just gonna wait here. With this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic