Thursday, March 6, 2008

Mock Exam on Collections & Generics - 3

1)
Collection c = new ArrayList();
....//line 1

Which line inserted independtly at line 1 will allow to compile?

a)c.add(new Object());

b)c.add("Java");

c)c.add(null);

d)All 3 lines above inserted independtely will allow to compile

e)All 3 lines above inserted independtely will not allow to compile


Answer c)

a)-->we don’t know what the element type c will be,So we cannot add Object to it.

b)-->we don’t know what the element type c will be,So we cannot add String to it.

c)-->null is allowed because it is member of every type.

d)-->Because of option a & b, d also wrong.

e)-->Because of option c, e also wrong.


2) Which line(s) is free from compile time error?

Assume that Parent is a Base class and Child is a Derived class

List alList = new ArrayList(); //line 1
List alList = new ArrayList(); //line 2
List alList = new ArrayList(); //line 3
List alList = new ArrayList(); //line 4
List alList = new ArrayList(); //line 5

a)only 1

b)only 4

c)only 5

d)2 and 3

e)1 and 4


Answer c)

a)-->Reference can hold only Parent class and its super types.

b)-->Reference can hold only Child class and its sub types.

c)-->Wild card types are applicable for references and it can accept anytype parameter object assign to it

d)-->We cannot use wildcard notation in the object creation.

e)-->line 1 failed because Reference can hold only Parent class & its super types and
line 2 failed because Reference can hold only Child class & its sub types.


3)
public static void addandDisp(Collection cs, T t)
{
for (T o : cs)
{
s.add(o);
}
for (T o : cs)
{
System.out.println(o);
}
}

//call 1
List ls1 = new LinkedList();
addandDisp(ls1,new String());

//call 2
List ls2 = new LinkedList();
addandDisp(ls2,new Object());

//call 3
List ls3 = new LinkedList();
addandDisp(ls3,new String());

//call 4
List ls4 = new LinkedList();
addandDisp(ls4,new Object());

Which call(s) to above method(addandDisp) are error free?

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

Answer e)

(call 2)Method call 2 cannot work,because the compiler cannot gurantee
the type to substitute.But in all other calls it is guranteed.

4)What are all types in the below cannot appilcable for Generics

1)Enum
2)Static class
3)Exception
4)nested interfaces
5)Anonymous inner classes
6)Inner class

Answer 1 & 3 & 5

Explanation
Anonymous classes do not have a name, but the name of a generic class is needed for declaring an
instantiation of the class and providing the type arguments.
Generic exception or error types are disallowed because the exception handling
mechanism is a runtime mechanism and the Java virtual machine does not know anything
about Java generics.
An enum type and its enum values are static. Since type parameters cannot be used in any static
context, the parameterization of an enum type would be pointless.

5)
class Base
{
public List asList() { return null; } //line 1
}
class Derived extends Base
{
public List asList() { return null;} // line 2
}

Which one(s) are true?
a)It is overloading
b)It is overriding
c)Compile time error
d)Only public List asList() in Derived class maked the method overriding
e)Run time Error

Answer b)

Explanation
Unknown type paramater can override by legacy code

6)
public class Test
{
static void addandDisp(List list)
{
list.add(987654321);
list.add(12.345);
for(Object e:list)
{
System.out.print(e.getClass()+" ");
}
}
public static void main(String[] args)
{
List ls = new ArrayList();
ls.add("Java");
addandDisp(ls);
}
}

What will be the output?

a)Prints class java.lang.String class java.lang.long class java.lang.Double
b)Prints class java.lang.String class java.lang.Integer class java.lang.Double
c)Prints class java.lang.String class java.lang.Double class java.lang.Float
d)Compile time Error
e)Run time Error

Answer b)
Since 987654321 is within the range of Integer(Max is 2147483647)and decimal value
defaults to Double.

7)What are all Advantages of using Generics?

a)Performance Improvement.

b)100% free from bugs.

c)Code will be more readable.

d)Moving comments to code.

e)Reduce in No.of.Lines of code.

Answer c,d,e

Explanation :
why not a & b means because there is no benchmark proved that Generics has performance Improvement
and Still there is chances for classcast exception at runtime.

No comments: