Thursday, March 6, 2008

Mock Exam on Collections & Generics - 2

  1. What is result of compiling and running the following code?

1)ArrayList sampleList = new ArrayList();

2)sampleList.add(40);

3)sampleList.add(20);

4)sampleList.add(70);

5)for(int num : sampleList)

6)System.out.println(num + " ");


Options :

a) compiler error at line 1.

b) compiler error at line 2,3,4.

c) compiler error at line 5.

d) compiles fine and prints 40 20 70.


Answer :

c) ArrayList sampleList is not type safe, so it will return Object in line 5 which cannot be changed in to type int.


  1. What is result of compiling and running the following code?

HashSet hs = new HashSet();

hs.add("scjp");

hs.add("exam");

HashSet s = new HashSet();

s=hs;

System.out.println(s);


Options:

a) compiler error

b) ClassCastException is thrown at runtime.

c) prints [scjp, exam]

d) prints [exam, scjp]


Answer :

a) class will not compile since HashSet and HashSet are incompatible types


  1. 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 a = new ArrayList();

a.add(new Car());

ArrayList b = a;

ArrayList c = (ArrayList)b;

c.add(new Bus());

for (Object obj : b)

System.out.println(obj);

}

}


Options:

  1. compiler error

  2. compiles with warning and gives some output

  3. compiles without warning and gives some output

  4. copiles and run with no output


Answer:

3) ArrayList b = a; This assignment assigns a typesafe arraylist in to a non-typesafe arraylist, So this assignment causes warning during compilation.



4.
List al = new ArrayList(); //line 1
al.add(12); //line 2
al.add(12+ 13); //line 3
for (Number no:al) //line 4
{
System.out.println(no);
}
What will be the possible result of the above program?
a) Error at Line 1
b) Error at Line 2
c) Error at Line 3
d) Error at Line 4
e) Compile and execute Sucuessfully

Answer d)

a)-->(Since 12 is Number.AutoBoxing works Automatically.It is accepatable.

c)-->al.add(12+ 13)Before add method is going to work, addition of number takes place
and 25 is added into al.

d)Error.Because in the declaration we have specified any super type of Integer,But in for each
loop we are specifying Number,We cannot restrict whenever we are using lowerbound.So accepatble
thing in line 4 is for (Object no:al).

e)It wont compile because of error at line 4.



5)
List ls = new ArrayList(); //line 1
ls.add(12); //line 2
ls.add((int)12.0f); //line 3
ls.add((Integer)12*12); //line 4
ls.add((Integer)(12.0f)); //line 5
for (Integer ll:ls)
{
System.out.println(ll);
}

What will be the possible result of the above program?

a)Error at line 3
b)Error at line 4
c)Error at line 5
d)Run Time error
e)Executes Sucuessfully

Answer c)

a)-->float can be type cast into int and added into list.so no problem.

b)-->12*12 will produce 144 and added into list with (r) without using (Integer)Object conversion.

c)-->Result in Error because float value can be convert to int value but not to Integer Object.
To make it work convert into int value and then to Integer Object.Like this (Integer)(int)(12.0f).

d)-->Results in Compile time error.So cannot go for execution.

e)-->Because of line 5 cannot compile and execute.



6) Find errors if any, in the following lines of code

List ls1 = new LinkedList(); //line 1
List ls2 = new LinkedList(); //line 2
List ls3 = new LinkedList(); //line 3
List ls4 =ls1; //line 4
List ls5 =ls2; //line 5
List ls6=ls5=ls4; //line 6

a) Error at Line 1 and Line 2
b) Error at Line 4 and Line 5
c) Error at line 6
d) Run time Error
e) No Error

Answer is e)

a)-->we can very well assign legacy type to Generic Type.

b)-->we can very well assign Generic type to Legacy Type.

c)-->ls6,ls5,ls4 are of legacy type, so no problem in assigning.

d)-->No runtime Error in the above specified lines.

e)-->Executes Sucuessfully, possibly no output.

No comments: