Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 27

Generics Java 1.

5 Unplugged Below is the documentation of some more than a 100 questions on Generics based on different low to high level concepts,scenarios invloved. The answers are expected by the user to evaluate themselves which is by just the easy way of copy pasting the code in any IDE like eclipse.Check the compile time or run time behaviour after that to know the answer. Check the description of error/warnings shown by compiler for in depth analysis. Its more like a hands on approach whether reader learn all concepts practically with a situation in front.

Q (1-13)

Which will compile fine ?


1.) List lst= new ArrayList<String>(); 2.) If List lst= new ArrayList<String>(); List<Object> lst1 = lst; 3.) If List lst= new ArrayList<String>(); List<String> lst1 = lst; 4.) List<Object> lst1 = new ArrayList<String>(); 5.) List<String> lst1 = new ArrayList<Object>(); 6.) List<?> lst1 = new ArrayList<String>(); 7.) List<?> lst1 = new ArrayList(); 8.) List<?> lst1 = new ArrayList<>(); 9.) List<String> ls1 = new ArrayList<String>(); //1 List<Object> lo1 = ls1; //2 10.) voidprintCollections(Collection<?> c) {
for (Object e : c) { System.out.println(e); } }

11.) Collection<?>c = new ArrayList<String>();

c.add(new Object());

12.) public void addRectangle(List<? extends Shape>shapes) { shapes.add(0, new Rectangle()); } 13.)
static void fromArrayToCollection(Object[] a, Collection<?>c) { for(Object o : a) { c.add(o); } } All questions from 15-21 based on below code snipped .Plz tell which code willcompile well

and what

will T inferred as ?
static<T>void mapArrayToCollection(T[] a, Collection<T>c) { for(T o : a) { c.add(o); }} Also :Object[] oa = new Object[100]; Collection<Object>co = new ArrayList<Object>(); String[] sa = new String[100]; Collection<String>cs = new ArrayList<String>(); Integer[] ia = new Integer[100]; Float[] fa = new Float[100]; Number[] na = new Number[100]; Collection<Number>cn = new ArrayList<Number>();

14.) Will above code compiles well ? 15.) mapArrayToCollection(oa, co);// T will be inferred as ______ ? 16.) mapArrayToCollection(sa, cs);// T will be inferred as _______ ? 17.) mapArrayToCollection(sa, co);// T will be inferred as Object or String ? 18.) mapArrayToCollection(ia, cn);// T will be inferred as ______ ? 19.) mapArrayToCollection(fa, cn);// T will be inferred as ______ ? 20.) mapArrayToCollection(na, co);// T will be inferred as Object or Number ? 21.) mapArrayToCollection(na, cs);// T will be inferred as ______ ?
Will below code 22-23 run fine at compile time and run time?

22.) public String loophole1(Integer x) { List<String>ys = new LinkedList<String>(); List xs = ys; xs.add(x); returnys.iterator().next(); } 23.) public String loophole2(Integer x) {

List ys = new LinkedList(); List xs = ys; xs.add(x); return (String) ys.iterator().next(); } 24.) List <String>l1 = new ArrayList<String>(); List<Integer>l2 = new ArrayList<Integer>(); Below will return true or false ?
System.out.println(l1.getClass() == l2.getClass());

25.) Will below code compile ?


Collection cs = new ArrayList<String>(); if (cs instanceof Collection<String>) { ...}

26.) Will below code compile and run fine? If compile fine will type casting be checked at run time?
Collection cs = new ArrayList<String>(); Collection<String>cstr = (Collection<String>) cs;

27.) Will below codecompile and run fine? If compile fine will type casting be checked at run time?
<T>T badCast(T t, Object o) {return (T) o; }

28.) Will this Code Compile ?


List<String>[] lsa = new List<String>[10];

29.) Will this C ompile ?


List<?>[] lsa = new List<?>[10];

30.) Will this C ompile ?


List<String>[] lsa = new List<?>[10];

31.) Will below code compile and run fine?


List<?>[] lsa = new List<?>[10]; Object o = lsa; Object[] oa = (Object[]) o; List<Integer>li = new ArrayList<Integer>(); li.add(new Integer(3)); oa[1] = li; String s = (String) lsa[1].get(0);

32.) Will below code compile and run fine?


List<String>[] lsa = new List<?>[10]; Object o = lsa; Object[] oa = (Object[]) o; List<Integer>li = new ArrayList<Integer>(); li.add(new Integer(3)); oa[1] = li; String s = lsa[1].get(0);

33.) Will below code compile and run fine?


<T>T[] makeArray(T t) { returnnew T[100]; }

34.) interface Sink<T>{


flush(T t); }

public static <T>T writeAll(Collection<T>coll, Sink<T>snk){ T last; for(T t : coll) { last = t; snk.flush(last); } returnlast; } Will below code compile and run fine ? Sink<Object>s; Collection<String>cs; String str = writeAll(cs, s);

35.)
interfaceSink<T>{ flush(T t); } public static <T> T writeAll(Collection<? Extends T> c, Sink<T> s) {.. } Will below code compile and run fine ? Sink<Object>s; Collection<String>cs; String str = writeAll(cs, s);

36.)
interfaceSink<T>{ flush(T t); } public static <T> T writeAll(Collection<T> c, Sink<? super T> s) {.. } Will below code compile and run fine ? Sink<Object>s; Collection<String>cs; String str = writeAll(cs, s);

37.) Will below code compile and run fine ? public classTestGenerics { public static void main(String[] args) { Collection<Foo>cf = new ArrayList<Foo>(); TestGenerics.max(cf); } public static <T extends Comparable<T>>

T max(Collection<T>coll){ return null; } class Foo implements Comparable<Object> { @Override publicintcompareTo(Object arg0) { // TODO Auto-generated method stub return 0; } } } 38.) Will below code compile and run fine ? public classTestGenerics { public static void main(String[] args) { Collection<Foo>cf = new ArrayList<Foo>(); TestGenerics.max(cf); } public static <T extends Comparable<? super T>> T max(Collection<T>coll){ return null; } class Foo implements Comparable<Object> { @Override publicintcompareTo(Object arg0) { // TODO Auto-generated method stub return 0; } } }

39.) Will below code compile and run fine ? public classTestGenerics { public static void main(String[] args) {
Set<?>unknownSet = newHashSet<String>(); addToSet(unknownSet, abc); // illegal

}
/** Add an element t to a Set s */ publicstatic<T>voidaddToSet(Set<T> s, T t) {

} 40.) Will below code compile and run fine ? public classTestGenerics { public static void main(String[] args) {
Set<?>unknownSet = newHashSet<String>(); Set<?>s = Collections.unmodifiableSet(unknownSet}

} The syntax for unmodifiableSet in Collections class is as :classCollections { <T>public static Set<T>unmodifiableSet(Set<T>set) { ... } . }

41.) Will below code signature for a method will compile fine ?
public static <T extends Object & Comparable<? superT>> T max(Collection<T>coll) {}

42.) What will be used as the erasure of the type variables in below method Signature ?
publicstatic<T extendsCloneable & Serializable & Closeable> T max11(Collection<T>coll) { returnnull; }

43.) Will below code signature for a method will compile fine ?
publicstatic<T extends Object & Cloneable & Serializable> T max1(Collection<T>coll) { returnnull; }

44.) Will below code signature for a method will compile fine ? publicstatic<T extends Cloneable & Object & Serializable> T max1(Collection<T>coll) { returnnull; } 45.)
Will below code signature for a method will compile fine ? publicstatic<T extends Cloneable & String & Serializable> T max1(Collection<T>coll) { returnnull; }

46.) Will below code compile& Runfine ? class Foo { public Foo create(){//...}// Factory, should create an instance of whatever //class it is declared in } class Bar extends Foo { public Bar create(){//...}} 47.) Will below code compile& Runfine ? class Foo { public Foo create(){//...}// Factory, should create an instance of whatever //class it is declared in } class Bar extends Foo { public Bar create(){//...}}
classBazextends Bar { publicFoo create(){...} }

48.) Will below class compile fine ?


public class Item<Element> { private Element value; public static List<Item<?>> items = newArrayList<Item<?>>(); public Item (Element value) { this.value = value; items.add(this); } }

49.) Will below code compilefine? public classTestGenerics { public static void main(String[] args) {
System.out.println("Calling setRTE ..."); Item<Object> item = newItem(new Object()); item.setRTE(item);

}
publicvoidsetRTE (Item<? super Exception>item) { item.setValue(newRuntimeException()); }

}
class Item<Element> { private Element value; public Item(Element value) { this.value = value; } public Element value() { returnvalue; } publicvoidsetValue(Element value) { this.value = value; } }

50.) Will below code compile fine? public classTestGenerics { public static void main(String[] args) {
System.out.println("Calling setRTE ..."); Item<Object> item = newItem(new Object()); item.setRTE(item);

}
publicvoidsetRTE (Item<? super Exception>item) { item.setValue(newThrowable()); }

}
class Item<Element> { private Element value; public Item(Element value) { this.value = value; } public Element value() { returnvalue; } publicvoidsetValue(Element value) { this.value = value; } }

51.) Will below code compile fine? public classTestGenerics { public static void main(String[] args) {
System.out.println("Calling setRTE ..."); Item<Object> item = newItem(new Object()); item.setRTE(item);

}
publicvoidsetRTE (Item<? super Exception>item) { item.setValue(new MyException()); }

}
class Item<Element> { private Element value; public Item(Element value) { this.value = value; } public Element value() { returnvalue; } publicvoidsetValue(Element value) { this.value = value; } } classMyException extends Exception{}

52.) Will below code compile fine? public classTestGenerics { public static void main(String[] args) {
System.out.println("Calling setRTE ..."); Item<Object> item = newItem(new Object()); item.setRTE(item);

}
publicvoidsetRTE (Item<? extends Exception>item) { item.setValue(newThrowable()); }

}
class Item<Element> { private Element value; public Item(Element value) { this.value = value; } public Element value() { returnvalue; } publicvoidsetValue(Element value) { this.value = value;

} } classMyException extends Exception{}

53.) Will below code compile fine? public classTestGenerics { public static void main(String[] args) {
System.out.println("Calling setRTE ..."); Item<MyException> item = newItem(new Object()); item.setRTE(item);

}
publicvoidsetRTE (Item<? extends Exception>item) { item.setValue(newThrowable()); }

}
class Item<Element> { private Element value; public Item(Element value) { this.value = value; } public Element value() { returnvalue; } publicvoidsetValue(Element value) { this.value = value; } } classMyException extends Exception{}

54.) Will below code compile fine? public classTestGenerics { public static void main(String[] args) {
System.out.println("Calling setRTE ..."); Item<MyException> item = newItem(new Object()); item.setRTE(item);

}
publicvoidsetRTE (Item<? extends Exception>item) { item.setValue(newMyException()); }

class Item<Element> { private Element value; public Item(Element value) { this.value = value; } public Element value() { returnvalue; } publicvoidsetValue(Element value) { this.value = value; } } classMyException extends Exception{}

55.) Will below code compile fine? public classTestGenerics { public static void main(String[] args) {
System.out.println("Calling setRTE ..."); Item<MyException> item = newItem(new Object()); item.setRTE(item);

}
publicvoidsetRTE (Item<? extends Exception>item) { item.setValue(newObject()); }

}
class Item<Element> { private Element value; public Item(Element value) { this.value = value; } public Element value() { returnvalue; } publicvoidsetValue(Element value) { this.value = value; } } classMyException extends Exception{}

56.) Will below code compile fine? public classTestGenerics { public static void main(String[] args) {
System.out.println("Calling setRTE ..."); Item<Throwable> item = newItem(new Object()); item.setRTE(item);

}
publicvoidsetRTE (Item<? extends } Exception>item) {

}
class Item<Element> { private Element value; public Item(Element value) { this.value = value; } public Element value() { returnvalue; } publicvoidsetValue(Element value) { this.value = value; } } classMyException extends Exception{}

57.) Will below generic method compile ?


public void testGenericWildCard(List<?> lst){ String obj = lst.get(0); }

58.) Will below generic method compile ?


public void testGenericWildCard(List<?> lst){ Object obj = lst.get(0); }

59.) Will below generic code compile ?


public class TestGenericWildcard { public static void main(String[] args) { List<String> lst = new ArrayList<String>(); testGenericWildCard(lst); } public static void testGenericWildCard(List<?> lst){ Object obj = lst.get(0); } }

60.) Will below generic code compile ?


public class TestGenericWildcard { public static void main(String[] args) { List<Object> lst = new ArrayList<Object>(); testGenericWildCard(lst); } public static void testGenericWildCard(List<?> lst){ Object obj = lst.get(0); } }

61.) Will below generic code compile ? public class TestGenericWildcard { public static void main(String[] args) { List<Object> lst = new ArrayList<Object>(); testGenericWildCard(lst); } public static void testGenericWildCard(List<? extends Exception> lst){ } } class MyException extends Exception { } 62.) Will below generic code compile ? public class TestGenericWildcard { public static void main(String[] args) { List< MyException > lst = new ArrayList< MyException >(); testGenericWildCard(lst); } public static void testGenericWildCard(List<? extends Exception> lst){ } } class MyException extends Exception { } 63.) Will below generic code compile ? public class TestGenericWildcard { public static void main(String[] args) { List< Exception > lst = new ArrayList< MyException >(); testGenericWildCard(lst); } public static void testGenericWildCard(List<? extends Exception> lst){

} } class MyException extends Exception { } 64.) Will below generic code compile ? public class TestGenericWildcard { public static void main(String[] args) { List< ? extends Exception > lst = new ArrayList< MyException >(); testGenericWildCard(lst); } public static void testGenericWildCard(List<? extends Exception> lst){ } } class MyException extends Exception { } 65.) Will below generic code compile ? public class TestGenericWildcard { public static void main(String[] args) { List< MyException > lst = new ArrayList< Exception >(); testGenericWildCard(lst); } public static void testGenericWildCard(List<? extends Exception> lst){ } } class MyException extends Exception { } 66.) Will below generic code compile ? public class TestGenericWildcard { public static void main(String[] args) { List< ? super MyException > lst = new ArrayList< Exception >(); testGenericWildCard(lst); } public static void testGenericWildCard(List<? extends Exception> lst){ } } class MyException extends Exception { } 67.) Will below generic code compile ?

public class TestGenericWildcard { public static void main(String[] args) {


List<Exception> lst = new ArrayList<Exception>(); lst.add(new RuntimeException()); testGenericWildCard(lst);

} public static void testGenericWildCard(List<? extends Exception> lst){ MyException myException = lst.get(0); } } class MyException extends Exception { } 68.) Will below generic code compile ? public class TestGenericWildcard { public static void main(String[] args) {
List<Exception> lst = new ArrayList<Exception>(); lst.add(new RuntimeException()); testGenericWildCard(lst);

} public static void testGenericWildCard(List<? extends Exception> lst){ Object myException = lst.get(0); } } class MyException extends Exception { } 69.) Will below generic code compile ? public class TestGenericWildcard { public static void main(String[] args) {
List<Exception> lst = new ArrayList<Exception>(); lst.add(new RuntimeException()); testGenericWildCard(lst);

} public static void testGenericWildCard(List<? extends Exception> lst){ Exception myException = lst.get(0); } } class MyException extends Exception { } 70.) Will below generic code compile ? public class TestGenericWildcard {

public static void main(String[] args) { List<Object> lst = new ArrayList<Object>(); testGenericWildCard(lst); } public static void testGenericWildCard(List<? super Exception> lst){ } } class MyException extends Exception { } 71.) Will below generic code compile ? public class TestGenericWildcard { public static void main(String[] args) { List< MyException > lst = new ArrayList< MyException >(); testGenericWildCard(lst); } public static void testGenericWildCard(List<? super Exception> lst){ } } class MyException extends Exception { } 72.) Will below generic code compile ? public class TestGenericWildcard { public static void main(String[] args) { List< Exception > lst = new ArrayList< MyException >(); testGenericWildCard(lst); } public static void testGenericWildCard(List<? super Exception> lst){ } } class MyException extends Exception { }

73.) Will below generic code compile ? public class TestGenericWildcard { public static void main(String[] args) { List< ? extends Exception > lst = new ArrayList< MyException >(); testGenericWildCard(lst); }

public static void testGenericWildCard(List<? super Exception> lst){ } } class MyException extends Exception { } 74.) Will below generic code compile ? public class TestGenericWildcard { public static void main(String[] args) { List< MyException > lst = new ArrayList< Exception >(); testGenericWildCard(lst); } public static void testGenericWildCard(List<? super Exception> lst){ } } class MyException extends Exception { } 75.) Will below generic code compile ? public class TestGenericWildcard { public static void main(String[] args) { List< ? super MyException > lst = new ArrayList< Exception >(); testGenericWildCard(lst); } public static void testGenericWildCard(List<? super Exception> lst){ } } class MyException extends Exception { } 76.) Will below generic code compile ? public class TestGenericWildcard { public static void main(String[] args) {
List<Exception> lst = new ArrayList<Exception>(); lst.add(new RuntimeException()); testGenericWildCard(lst);

} public static void testGenericWildCard(List<? super Exception> lst){ MyException myException = lst.get(0); } }

class MyException extends Exception { } 77.) Will below generic code compile ? public class TestGenericWildcard { public static void main(String[] args) {
List<Exception> lst = new ArrayList<Exception>(); lst.add(new RuntimeException()); testGenericWildCard(lst);

} public static void testGenericWildCard(List<? super Exception> lst){ Object myException = lst.get(0); } } class MyException extends Exception { }

78.) Will below generic code compile ? public class TestGenericWildcard { public static void main(String[] args) {
List<Exception> lst = new ArrayList<Exception>(); lst.add(new RuntimeException()); testGenericWildCard(lst);

} public static void testGenericWildCard(List<? super Exception> lst){ Exception myException = lst.get(0); } } class MyException extends Exception { } 79.) Will below generic code compile ? public class TestGenericWildcard { public static void testGenericWildCard(List<? super Exception> lst){ lst.add(new MyException()); } } class MyException extends Exception { } 80.) Will below generic code compile ? public class TestGenericWildcard {

public static void testGenericWildCard(List<? super Exception> lst){ lst.add(new Object()); } } class MyException extends Exception { } 81.) Will below generic code compile ? public class TestGenericWildcard { public static void testGenericWildCard(List<? super Exception> lst){ lst.add(new Exception ()); } } class MyException extends Exception { }

82.) Will below generic code compile ? public class GenericPair<T, S>{ public GenericPair(T f, S s){ first = f; second = s; } } 83.) Will below generic code compile ? public class GenericPair<T, S>{ private final T first; private final S second; private static String last; public static List<T> lst1; // check compilation for this line only public GenericPair(T f, S s){ first = f; second = s; } } 84.) Will below generic code compile ? public class GenericPair<T, S>{ private final T first; private final S second;

private static String last; public static List<String> lst1; // check compilation for this line only public GenericPair(T f, S s){ first = f; second = s; } } 85.) Will below generic code compile ? public class GenericPair<T, S>{ private final T first; private final S second; private static String last; public static T genericLast; // check compilation for this line only public GenericPair(T f, S s){ first = f; second = s; } } 86.) Will below generic code compile ? public class GenericPair<T, S>{ private final T first; private final S second; private static String last; public GenericPair(T f, S s){ first = f; second = s; } public static void checkGenerics(T t){ } } 87.) Will below generic code compile ? public class GenericPair<T, S>{ private final T first; private final S second; private static String last; public GenericPair(T f, S s){ first = f; second = s;

// check compilation for this line only

} public static void checkGenerics(){ List<String> lst = new ArrayList<String>(); List<T> genericLst = new ArrayList<T>(); } } 88.) Will below generic code compile ?

// check compilation for this line only

public class GenericMethods { static <T> void printType(T anyType) //check compilation for this line only { System.out.println(anyType.getClass().getName()); } public static void main(String[] args) { GenericMethods.printType(String.class); GenericMethods.printType(new String("")); } }

89.) Will below generic code compile ? List<String> strObjects = new ArrayList<String>(); strObjects.add(new String("A String")); List<Object> objects = strObjects; //check compilation for this line only 90.) Will below generic code compile ? List<String> strObjects = new ArrayList<String>(); strObjects.add(new String("A String")); List<?> objects = strObjects; //check compilation for this line only 91.) Will below generic code compile ? List<String> strObjects = new ArrayList<String>(); strObjects.add(new String("A String")); List<?> objects = strObjects; //check compilation for this line Objects.add(Hi); //check compilation for this line 92.) Will below generic code compile ? List<String> strObjects = new ArrayList<String>();

strObjects.add(new String("A String")); List<?> objects = strObjects; Objects.add(null); 93.) Will below generic code compile ?

//check compilation for this line //check compilation for this line

public class GenericMethods { public static void main(String[] args) { List<? super Dog> dogs = new ArrayList<Animal>(); dogs.add( new Animal ()); } //check compilation for this line } class Animal {} class Dog extends Animal {} 94.) Will below generic code compile ? public class GenericMethods { public static void main(String[] args) { List<? super Dog> dogs = new ArrayList<Animal>(); dogs.add( new Dog ()); } //check compilation for this line } class Animal {} class Dog extends Animal {} 95.) Will below generic code compile ? public class GenericMethods { public static void main(String[] args) { List<? super Dog> dogs = new ArrayList<Animal>(); dogs.add( new MyDog ()); } //check compilation for this line } class Animal {}

class Dog extends Animal {} class MyDog extends Dog {} 96.) What is the output of the following program? public class Gen<G> { G g; Gen(G g) { this.g =g; } public static void main(String[] args) { Gen<String> arr[] = new Gen[5]; //line 1 arr[0] = new Gen("Java"); //line 2 arr[1] = new Gen(1); //line 3 arr[2] = (Gen<String>)new Gen(1); //line 4 arr[3] = (Gen<String>)new Gen<Integer>(1); //line 5 for(Gen o:arr) { System.out.println(o); } } } a)Compile time Error at line 1 b)Compile time Error at line 3 c)Compile time Error at line 4 d)Compile time Error at line 5 e)Run Time Error

97.) What is the output of compiling an running the following code? import java.util.*; class CheckTest { public static void main(String [] args) { Hashtable ht = new Hashtable<String, String>(); ht.put("chec", "check"); ht.put(1000, "check"); // line 1 ht.put("check", 20.01); // line 2 System.out.print(ht.get("chec") + " "); System.out.print(ht.get(1000) + " "); //line 3 System.out.print(ht.get("check")); //line 4 } } a) Compilation fails due to error at line 1,2,3,4 b) Compilation fails due to error at line 3,4

c) Compiles fine and exception is thrown at runtime. d) Compiles fine and prints "check check 20.01" e) Compilation fails due to error at line 1,2

98.) Which of the following statements hold true in the following program? package generics; import java.util.ArrayList; import java.util.List; public class Ques04 { public static void main(String[] args) { List<? extends Instrument> allInstruments = new ArrayList<Instrument>(); // -->X allInstruments.add(new Guitar()); allInstruments.add(new Violin()); } } interface Instrument { public void play(); } class Guitar implements Instrument { public void play(){ System.out.println("Playing Guitar."); } } class Violin implements Instrument { public void play(){ System.out.println("Playing Violin."); } } 1. The program will not compile. 2. The program will compile but will throw run-time exception. 3. The statement 'X' manifests that objects of type Instrument as well as sub-types can be added to the list 'allInstruments'. 4. It is not possible to add any type of objects to the list 'allInstruments'. 5. The only possible element that can be added to the list 'allInstruments' is 'null'. 99.) What is the result compiling and running the following piece of code? import java.util.*; class Test { public static void main(String [] args) { Set vals = new TreeSet<String>(); vals.add("one"); vals.add(1);

vals.add("two"); System.out.println(vals); } } a)Does not Compile b)Compiles with warning and prints output [one, 1, two] c)Compiles without warning and prints output [one, 1, two] d)Compiles with warning and throws exception at runtime e)Compiles without warning and throws exception at runtime 100.) Which call(s) to method(addandDisp) are error free?

public static <T> void addandDisp(Collection<T> cs, T t) { for (T o : cs) { s.add(o); } for (T o : cs) { System.out.println(o); } } //call 1 List<? super Object> ls1 = new LinkedList<Object>(); addandDisp(ls1,new String()); //call 2 List<? extends Object> ls2 = new LinkedList<Object>(); addandDisp(ls2,new Object()); //call 3 List<Object> ls3 = new LinkedList<Object>(); addandDisp(ls3,new String()); //call 4 List<? super Object> ls4 = new LinkedList<Object>(); addandDisp(ls4,new Object()); a)only 3 b)only 1 and 3 c)only 1 and 4 d)only 1,2 and 3 e)only 1,3 and 4

101.) What is result of compiling and running the following code? 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); } } 1. 2. 3. 4. compiler error compiles with warning and gives some output compiles without warning and gives some output copiles and run with no output

102.)

Given the following definitions, which assignments are legal? class Box<T>{} class SuperBox<T> extends Box<T>{}

I. Box<Object> b = new Box<String>(); II. Box<String> b = new SuperBox<String>(); III Box<Object> b = new SuperBox<String>(); (a) I, II, III (b) II only (c) I and III only 103.) Which overloaded method is called by the following program? // Overload.java import java.util.Date; public class Overload { public static void someOverloadedMethod (Object o) { System.out.println ("call to someOverloadedMethod(Object o)"); } public static void someOverloadedMethod (Date d)

{ System.out.println ("call to someOverloadedMethod(Date d)"); } public static <T> void methodCaller (T t) { someOverloadedMethod (t); } public static void main (String [] args) { methodCaller (new Date ()); } } a.)Compile time error b.) run time error c.) someOverloadedMethod (Object o) d.) someOverloadedMethod (Date d)

104.) 105.)

You might also like