Thursday, March 6, 2008

Mock Exam on Collections & Generics - 4

1) 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();

vals.add("one");

vals.add(1);

vals.add("two");

System.out.println(vals);

}

}

Options :

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

Answer :

D are correct Answers.

Compiles with warning due to un-safe assignment List vals = new ArrayList();

Since TreeSet is used, it will try to sort by natural order. Due to the presence of Integer (vals.add(1);) in the collection, it will throw ClassCastException at runtime(While try to cast Integer in to String).



2) which of the following piece of code can be inserted to make the following code to compile?

import java.util.*;

class PickThePiece {

public static void main(String [] args) {

//insert the first line here

datas.add("delhi");

datas.add(new Object());

//insert the second line here

}

}

Options :

a) List datas = new LinkedList();

String data = datas.get(0);

b) List datas = new LinkedList();

String data = (String)datas.get(0);

c) List datas = new LinkedList();

String data = (String)datas.get(0);

d) List datas = new LinkedList();

String data = datas.get(0);

e) all the above

Answer :

B is the correct answer.

A is wrong because datas.get(0) will return a Object which cannot be directly assigned to a String without casting. C and D are wrong because Object cannot be added to a List of String.



3) What is the result of compiling and running the following code?

import java.util.*;

class SampleTest {

public static void main(String [] args) {

List samples = new ArrayList();

samples.add("100");

samples.add(200);

samples.add("300");

printData(samples);

}

static void printData(List samples) {

for(String sample : samples) {

System.out.print(sample + " ");

}

}

}

Options :

a) Prints 100 200 300

b) Compile time error

c) Compiles without warning

d) Compiles with warning

e) Runtime Exception

Answer :

D, E are correct answers.

It produces warning since un-safe List samples is passed to a type safe collections(as a method argument).

Since samples.add(200), adds a Integer in to collection. While iterating through enhanced for loop, Integer is tried to cast to String causes ClassCastException.



4) Consider the following code, select the valid options given below.

class Fruit {}

class Apple extends Fruit {}

class Orange extends Fruit {}

Options :


a) List stmt = new ArrayList();

b) List stmt = new ArrayList();

c) List stmt = new ArrayList();

d) List stmt = new ArrayList();

e) All the above

f) None of these

Answer :

E is the correct answer. All these options are valid.

Keyword "super " – allows the type followed by keyword and its super type(parent ).

Keyword "extends" – allows the type followed by keyword and its sub type(child).



5) What is the output of the following code?

import java.util.*;

class Color {}

class Blue extends Color {}

class Red extends Color {}

class TestColor {

public static void main(String [] args) {

1) List colors = new ArrayList();

2) colors.add(new Color());

3) colors.add(new Blue());

4) colors.add(new Red());

5) alterColor(colors);

6) System.out.println(colors);

}

static void alterColor(List clrs) {

7) clrs.add(new Object());

}

}

Options :

a) Compile time error due to lines 3 and 4.

b) Compile time error due to line 5.

c) Compile time error due to line 7.

d) Compiles with warning and produces some output.

e) Compiles without warning and produces some output.

f) Compiles fine and Exception is thrown at runtime.

Answers :

D is the correct answer.

Warning is due to non-type safe method call. Within the alterColor() method adding a new Object is not a issue because the collection becomes non-type safe.


6) what is the result of the following code ?

import java.util.*;

class Bird {}

class Duck extends Bird {}

class Hen extends Bird {}

class FuzzyTest {

public static void main(String [] args) {

Map birds = new HashMap();

birds.put("bird", new Bird());

birds.put("hen", new Hen());

birds.put("duck", new Duck());

Map bs = addBirds(birds);

for(String b : bs.keySet())

System.out.print(b + " ");

}

static Map addBirds(Map brds) {

brds.put("bird", new Object());

return brds;

}

}

Options :

a) Compiles and prints output "bird hen duck".

b) Compiles and prints output "bird duck hen".

c) Compiles and prints some output order cannot be determined.

d) Run time Exception.

e) Compilation fails.

Answer :

E is the correct answer.

Since bs is non-typesafe collection, bs.keySet() returns Object. But in enhanced for loop string is used to catch the returned values, that leads to compilation error.


7) what are the valid statements can be filled in the blank, to make the code to compile and run?

import java.util.*;

interface Eat{}

class Animal implements Eat{}

class Dog extends Animal {}

class Cat extends Animal {}

class AnimalTest {

public static void main(String[] args) {

List a = new ArrayList();

List d = new ArrayList();

List c = new ArrayList();

checkAnimal(a);

checkAnimal(d);

checkAnimal(c);

}

static void checkAnimal( ________________ pets) {

System.out.print("animals checked here");

}

}

Options :

a) List

b) List

c) List

d) List

e) List

f) All of the above

Answer :

A , C and E are the correct answers.

Keyword "super " – allows the type followed by keyword and its super type(parent ).

Keyword "extends" – allows the type followed by keyword and its sub type(child).

Wild card ? – allows everything.



8) what is the output of the following code?

import java.util.*;

class Example {

public static void main(String [] args) {

Set values = new TreeSet();

values.add("yet");

values.add("get");

values.add("bet");

displayValues(values);

}

static void displayValues(Set values) {

values.add("wet");

for(Object v : values)

System.out.print(v + " ");

}

}

Options :

a) Compiles and gives output "yet get bet wet".

b) Compiles and gives output "bet get wet yet".

c) Compilation fails

d) Compiles with warning and Exception thrown at runtime.

e) Compiles without warning and Exception thrown at runtime.

Answer :

C is the correct answer.

When we use wildcard(?) to catch the collection , then modifications are not allowed in that collection. Here values.add("wet") will throw error at compilation time.


9) Choose the valid ways to create an object for the following class.

class GenTest {

T num;

public T checkNumber(T n) {

return n;

}

}

Options :

a) Compilation fails.

b) GenTest gt = new GenTest();

c) GenTest gt = new GenTest();

d) GenTest gt = new GenTest();

e) None of the above.

Answer :

A is the correct answer.

Since is an invalid syntax. If super keyword is replaced by extends, then B and C will be the valid answers.



10) Choose the valid constructors for the following class.

class Generics{}

Options :

a) public Generics(){}

b) public Generics(){}

c) public Generics(T t){}

d) public Generics(){}

e) All the above.

Answer :

A,C and D are correct answers.

B is incorrect because of improper syntax.

No comments: