Thursday, January 18, 2007

SCJP 5.0 Mock Exams - Generics


SCJP 5.0 Mock Exams - Generics


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

vals.add("one");

vals.add(1);

vals.add("two");

System.out.println(vals);

}

}


Options :

  1. Does not Compile

  2. Compiles with warning and prints output [one, 1, two]

  3. Compiles without warning and prints output [one, 1, two]

  4. Compiles with warning and throws exception at runtime

  5. 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<String>();

Since TreeSet<String> 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).


  1. 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 :

  1. List<Object> datas = new LinkedList<Object>();

String data = datas.get(0);

  1. List<Object> datas = new LinkedList<Object>();

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

  1. List<String> datas = new LinkedList<String>();

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

  1. List<String> datas = new LinkedList<String>();

String data = datas.get(0);

  1. all the above


Answer :

B,C, and D are the correct answers.

A is wrong because datas.get(0) will return a Object which cannot be directly assigned to a String without casting.


  1. 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<String> samples) {

for(String sample : samples) {

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

}

}

}


Options :


  1. Prints 100 200 300

  2. Compile time error

  3. Compiles without warning

  4. Compiles with warning

  5. Runtime Exception


Answer :

D, E are correct answers.

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

E) 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.


  1. Consider the following code, select the valid options given below.

class Fruit {}

class Apple extends Fruit {}

class Orange extends Fruit {}


Options :

  1. List<? extends Fruit> stmt = new ArrayList<Fruit>();

  2. List<? super Apple> stmt = new ArrayList<Fruit>();

  3. List<? extends Fruit> stmt = new ArrayList<Apple>();

  4. List<? super Orange> stmt = new ArrayList<Orange>();

  5. All the above

  6. 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).


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

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

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

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

5) List<Color> newClr = alterColor(colors);

6) System.out.println(newClr);

}


static void alterColor(List clrs) {

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

}

}


Options :

  1. Compile time error due to lines 3 and 4.

  2. Compile time error due to line 5.

  3. Compile time error due to line 7.

  4. Compiles with warning and produces some output.

  5. Compiles without warning and produces some output.

  6. 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.


  1. 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<String, Bird> birds = new HashMap<String, Bird>();

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 :

  1. Compiles and prints output “bird hen duck”.

  2. Compiles and prints output “bird duck hen”.

  3. Compiles and prints some output order cannot be determined.

  4. Run time Exception.

  5. 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.


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

List<Dog> d = new ArrayList<Dog>();

List<Cat> c = new ArrayList<Cat>();

checkAnimal(a);

checkAnimal(d);

checkAnimal(c);

}

static void checkAnimal( ________________ pets) {

System.out.print(“animals checked here”);

}

}


Options :

  1. List<? extends Animal>

  2. List<? super Animal>

  3. List<? extends Eat>

  4. List<? super Eat>

  5. List<?>

  6. 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.


  1. what is the output of the following code?

import java.util.*;

class Example {

public static void main(String [] args) {

Set<String> values = new TreeSet<String>();

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 :

  1. Compiles and gives output “yet get bet wet”.

  2. Compiles and gives output “bet get wet yet”.

  3. Compilation fails

  4. Compiles with warning and Exception thrown at runtime.

  5. 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.


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

class GenTest<T super Number> {

T num;

public T checkNumber(T n) {

return n;

}

}


Options :

  1. Compilation fails.

  2. GenTest<Number> gt = new GenTest<Number>();


  1. GenTest<Integer> gt = new GenTest<Integer>();


  1. GenTest<Object> gt = new GenTest<Object>();


  1. None of the above.



Answer :

A is the correct answer.

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


  1. Choose the valid constructors for the following class.

class Generics<T>{}


Options :

  1. public Generics(){}

  2. public Generics<T>(){}

  3. public <T> Generics(T t){}

  4. public <T> Generics(){}

  5. All the above.


Answer :

A,C and D are correct answers.

B is incorrect because of improper syntax.













1 comment:

Unknown said...

Hello sir,
There are mistakes in questions and your explanations.
1).In 1st ques. warning is not due to un-safe assignment,it because we are trying to add something to a reference that is not type safe.
2).In 2nd ques. only option B is correct,C & D will not even complie because with C & D you are adding Object type to list declared of String type.
3).In Ques 3 warning is also because we are adding something in unsafe list.
4).The explanation for ques 5 is completely wrong.It wont even compile because in line 5 you are assigning void type to List type therefore it will not compile and according to your explanation warning will be produced due to unsafe method call-wrong.Warning will be produced because we are adding something in collection in alterColor.If we dont perform add operation in alterColor method it will not produce warning.
Hope you agree with errors pointed by me.Thanx!