SCJP Mock exam for Generics
59 Votes
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.
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
No hay comentarios:
Publicar un comentario