A collection of structured questions for Generics, under the scope of Sun Certified Java Programmer for Java SE 6.
If you are busy, just go through questions 1-40.
| questions | topic |
|---|---|
| 1-24 | assignment and hierarchy |
| 25-40 | add and get elements from a generic collection |
| 41-50 | overloading and overriding |
| 51-60 | various details |
| 61-72 | declaration of generic classes |
- Will this code compile successfully? (1 correct answer)
List<Number> list1 = null; List<Integer> list2 = null; list1 = list2;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
List<Number> list1 = null; List<Integer> list2 = null; list2 = list1;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
List<? extends Number> list1 = null; List<Integer> list2 = null; list1 = list2;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
List<? extends Number> list1 = null; List<Integer> list2 = null; list2 = list1;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
List<Number> list1 = null; List<? super Integer> list2 = null; list1 = list2;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
List<Number> list1 = null; List<? super Integer> list2 = null; list2 = list1;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
SortedSet<? super Number> set1 = null; SortedSet<Integer> set2 = null; set1 = set2;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
SortedSet<? super Number> set1 = null; SortedSet<Integer> set2 = null; set2 = set1;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
SortedSet<Number> set1 = null; SortedSet<? extends Integer> set2 = null; set1 = set2;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
SortedSet<Number> set1 = null; SortedSet<? extends Integer> set2 = null; set2 = set1;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
Queue<? extends Number> q1 = null; Queue<? super Integer> q2 = null; q1 = q2;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
Queue<? extends Number> q1 = null; Queue<? super Integer> q2 = null; q2 = q1;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
Queue<? super Number> q1 = null; Queue<? extends Integer> q2 = null; q1 = q2;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
Queue<? super Number> q1 = null; Queue<? extends Integer> q2 = null; q2 = q1;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
Queue<?> q1 = null; Queue<Integer> q2 = null; q1 = q2;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
LinkedList<?> list1 = null; LinkedList<Integer> list2 = null; list2 = list1;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
LinkedList<?> list1 = null; LinkedList<? extends Integer> list2 = null; list1 = list2;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
LinkedList<?> list1 = null; LinkedList<? extends Integer> list2 = null; list2 = list1;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
LinkedList<?> list1 = null; LinkedList<? super Integer> list2 = null; list1 = list2;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
LinkedList<?> list1 = null; LinkedList<? super Integer> list2 = null; list2 = list1;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
PriorityQueue queue1 = null; PriorityQueue<Integer> queue2 = null; queue1 = queue2;
- Yes, without warnings.
- Yes, with a warning.
- No.
- Will this code compile successfully? (1 correct answer)
PriorityQueue queue1 = null; PriorityQueue<Integer> queue2 = null; queue2 = queue1;
- Yes, without warnings.
- Yes, with a warning.
- No.
- Will this code compile successfully? (1 correct answer)
PriorityQueue<?> queue1 = null; PriorityQueue queue2 = null; queue1 = queue2;
- Yes, without warnings.
- Yes, with a warning.
- No.
- Will this code compile successfully? (1 correct answer)
PriorityQueue<?> queue1 = null; PriorityQueue queue2 = null; queue1 = queue2;
- Yes, without warnings.
- Yes, with a warning.
- No.
- Will this code compile successfully? (1 correct answer)
Set<Integer> set = new TreeSet<Integer>(); set.add(10);
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
Set<Integer> set = new TreeSet<Integer>(); set.add((int)1.0f);
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
Set<Integer> set = new TreeSet<Integer>(); set.add(10L);
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
Set<Integer> set = new TreeSet<Integer>(); int number = (short)10; set.add(number);
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
Set<Number> set = new TreeSet<Integer>(); set.add(10L);
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
NavigableSet<?> set = new TreeSet<Object>(); set.add(new Object());
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
NavigableSet<? super Object> set = new TreeSet<Object>(); set.add(new Object());
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
NavigableSet<? extends Object> set = new TreeSet<Object>(); set.add(new Object());
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
NavigableSet<? extends String> set = new TreeSet<String>(); set.add("string");- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
NavigableSet<? super String> set = new TreeSet<String>(); set.add("string");- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
NavigableSet<? super String> set = new TreeSet<String>(); set.add(new Object());
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
List<? extends Integer> list = new ArrayList<Integer>(); for (Integer element : list) { System.out.println(element); }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
List<? extends Integer> list = new ArrayList<Integer>(); Integer first = list.get(0);
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
List<? super Integer> list = new ArrayList<Integer>(); for (Integer element : list) { System.out.println(element); }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
List<? super Integer> list = new ArrayList<Integer>(); Integer first = list.get(0);
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
List<? super Integer> list = new ArrayList<Integer>(); Object first = list.get(0);
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
import java.util.*; class Test { void say(Set<Double> set) { } void say(SortedSet<Double> set) { } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
import java.util.*; class Test { void say(Set<Double> set) { } void say(Set<Boolean> set) { } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
import java.util.*; class Test { void say(Set<Double> set) { } void say(Set<Double>... set) { } }- Yes.
- No.
- Consider these classes.
import java.util.*; class Parent { void say(List<String> list) { System.out.println("parent"); } } class Child extends Parent { void say(List list) { System.out.println("child"); } }What happens when this code is compiled and executed? (1 correct answer)
public static void main(String[] java) { Child c = new Child(); c.say(new LinkedList<String>()); }- It prints “child”.
- It prints “parent”.
- Compilation fails.
- Consider these classes.
import java.util.*; class Parent { void say(List<String> list) { System.out.println("parent"); } } class Child extends Parent { void say(List list) { System.out.println("child"); } }What happens when this code is compiled and executed? (1 correct answer)
public static void main(String[] java) { Child c = new Child(); c.say(new LinkedList<List<Boolean>>()); }- It prints “child”.
- It prints “parent”.
- Compilation fails.
- Consider these classes.
import java.util.*; class Parent { void say(List<String> list) { System.out.println("parent"); } } class Child extends Parent { void say(List list) { System.out.println("child"); } }What happens when this code is compiled and executed? (1 correct answer)
public static void main(String[] java) { Parent c = new Child(); c.say(new LinkedList<String>()); }- It prints “child”.
- It prints “parent”.
- Compilation fails.
- Consider these classes.
import java.util.*; class Parent { void say(List<String> list) { System.out.println("parent"); } } class Child extends Parent { void say(List list) { System.out.println("child"); } }What happens when this code is compiled and executed? (1 correct answer)
public static void main(String[] java) { Parent c = new Child(); c.say(new LinkedList<Long>()); }- It prints “child”.
- It prints “parent”.
- Compilation fails.
- Will this code compile successfully? (1 correct answer)
import java.util.*; class Parent { void say(List<String> list) { System.out.println("parent"); } } class Child extends Parent { void say(List<Integer> list) { System.out.println("child"); } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
import java.util.*; class Parent { void say(List<? extends Number> list) { System.out.println("parent"); } } class Child extends Parent { void say(List<Integer> list) { System.out.println("child"); } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
import java.util.*; class Parent { void say(List list) { System.out.println("parent"); } } class Child extends Parent { void say(List<Integer> list) { System.out.println("child"); } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
Object set = new TreeSet<Integer>(); boolean flag = set instanceof NavigableSet<Integer>;
- Yes.
- No.
- What is the output of the following code? (1 correct answer)
List<? extends String> list1 = new ArrayList<String>(); List<? super String> list2 = new ArrayList<String>(); List<Integer> list3 = new ArrayList<Integer>(); List list4 = new ArrayList(); if (list1 instanceof List && list2 instanceof List && list3 instanceof List && list4 instanceof List) { System.out.println("yes"); }- It prints “yes”.
- It prints nothing.
- Will this code compile successfully? (1 correct answer)
Class c = ArrayList<Integer>.class;
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
Class c = new ArrayList<Integer>().getClass();
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
new ArrayList<?>();
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
new TreeMap<String, ? super Integer>();
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
new ArrayList<Set<?>>();
- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test extends ArrayList<? extends Number> { }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test implements Comparable<?> { public int compareTo(Comparable<?> object) { return 0; } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test implements Comparable<Comparable<?>> { public int compareTo(Comparable<?> object) { return 0; } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test { <T> T getFirst(List<T> list) { return list.get(0); } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test { static <T> T getFirst(List<T> list) { return list.get(0); } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test <T> { T getFirst(List<T> list) { return list.get(0); } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test <T> { static T getFirst(List<T> list) { return list.get(0); } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test <T> { T instance; Test(T instance) { this.instance = instance; } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test <T> { Test(T my) { boolean b = (my instanceof T); } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test <T> { void test(T method) { Object my = (T)method; } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test <T> { void test() { NavigableMap map = new TreeMap<String, T>(); } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test <T> { private T[] array = null; }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test <T> { private T[] array = new T[7]; }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test<String> { String my = "Hello!"; }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test<String> { String my; public Test(String my) { this.my = my; } public String get() { return my; } } public class RunTest { public static void main(String[] args) { Integer i = new Test<Integer>(1).get(); System.out.println(i.getClass()); } }- Yes.
- No.
© 2008 Nikos Pougounias. This is a free contribution to the Java community. Please distribute it for free. http://nikojava.wordpress.com
Answers
- b
- b
- a
- b
- b
- a
- b
- b
- b
- b
- b
- b
- b
- b
- a
- b
- a
- b
- a
- b
- a
- b
- a
- a
- a
- a
- b
- a
- b
- b
- a
- b
- b
- a
- b
- a
- a
- b
- b
- a
- a
- b
- a
- a
- a
- a
- c
- b
- b
- b
- b
- a
- b
- a
- b
- b
- a
- b
- b
- a
- a
- a
- a
- b
- a
- b
- a
- a
- a
- b
- b
- a
[...] java blog quick & easy Java tutorials « SCJP Mock exam for Generics JBoss integration with JDeveloper 11g [...]
Hii Niko This an Excellent Practice Stuff.it’s helped me a lot in grasping the Concepts of Generics fully.
Thnaks A Lot.
Thx !
nice one
kudos to you
Hi Niko,
this is really excellent stuff. i liked it and i really appreciate your effort.
Thanks Buddy!..
Hi, thanks for this!
Questions 23 and 24 and respective answers are identical. Is it intended?
hi Niko,,,
thanks a lot …. i really enjoyed with these questions …
well done
[...] This is from the practice exam for Generics on > NikoJava [...]
Hi everyone
In question 72 the correct answer is b), i don’t know the exact reason; I supposed is a name colide because i tried to compile and the result is:
non-static class String cannot be referenced from a static context
Thanks for your time
Hi Nikos,
Thanks for this wonderful effort.
To add cream on top of this, if you could display the rules/logics/traps that you are testing in your questions, it would help us a lot in revising the concepts.
48, 49 and 50 the answer is a! They compile just fine!
and about question 72, it compiles fine, so, it’s correct.
Hello Guys
Finally cracked this all confusing generic collection type assignement. If you use this logic you can crack nicko questions on generics (1-24) without any problem as well understanding the concepts of generics assignments.
Solutions:
Look at the assignments L1 = L2
Read like this
I accept L1 = You have L2
Question No Left hand side (I accept) Right Hand side is I have Can I accept Answer
1 I accept Number I have Integer No No
2 I accept Integer I have Number No No
3 I accept subclasses of Number I have Integer Yes Yes
4 I accept Integer I have any subclasses of Number No No
5 I accept Number I have any super class of Integer (Number/Object) No No
6 Any super classes of Integer (Number or Object) I have Number Yes Yes
7 Any super classes of Number(Object) I have Integer No No
8 Integer I have any super classes of Number(Object) No No
9 Number Any subclasses of Integer No No
10 Any subclass on Integer Number No No
11 Any subclass of Number Super classes of Integer(Object/Number) No No
12 Any super class of Integer(Number/Object) Subclasses of Number No No
13 Only super classes of Number(Object) Integer No No
14 Subclasses of Integer Any Super class of Number (Object) No No
15 Anything Integer Yes Yes
16 Integer Anything No No
17 Anything Subclasses of Integer Yes Yes
18 Subclasses of Integer Anything No No
19 Anything Super class of Integer(Number/Object) Yes Yes
20 Any super class of Integer(Number/Object) Anything No No
21 PriorityQueue(non param read as anything) Integer Yes Yes
22 Integer Anything No No
23 Anything Anything Yes Yes
24 Anything Anything Yes Yes
48,49,50
are compiler errors
please if you could some explanation kind of thing or reasons for the answers,it will be great for some weird questions here.
Q64: it compiles fine
Hi Nick,
This is simply great. You must have spent a lot of time over this to bring the questions in more organised fashion. Great work.
Hi Niko,
One small question. The generics are used only during the compile time and not during the run time. Am i right? Ex – List will be just List during the runtime.
Hi Nikos Pougounias.
I am vipul Kumar. Thanks for providing the stuff on generics. I am preparing for scjp 1.6. The questions you have kept awared me of some topics i am not covered..
Thanks once again,
Vipul Kumar
whats wrong with 30th question??
Can anyone xplain why it can’t compile successfully
plz reply asap
List list = new ArrayList();
for (Integer element : list) {
System.out.println(element);
}
this should not compile. right??
but in 36th question the answer is YES..
can anyone explain please..
This is good stuff.
Nice concepts.
Nick
This is a super stuff
Reblogged this on Srikanth's Blog.
Mohamed Farouk thanks a lot for clearing the concept