martes, 28 de julio de 2015

SCJP Mock exam for Generics

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.
questionstopic
1-24assignment and hierarchy
25-40add and get elements from a generic collection
41-50overloading and overriding
51-60various details
61-72declaration of generic classes
  1. Will this code compile successfully? (1 correct answer)
    List<Number> list1 = null;
    List<Integer> list2 = null;
    list1 = list2;
    
    1. Yes.
    2. No.
  2. Will this code compile successfully? (1 correct answer)
    List<Number> list1 = null;
    List<Integer> list2 = null;
    list2 = list1;
    
    1. Yes.
    2. No.
  3. Will this code compile successfully? (1 correct answer)
    List<? extends Number> list1 = null;
    List<Integer> list2 = null;
    list1 = list2;
    
    1. Yes.
    2. No.
  4. Will this code compile successfully? (1 correct answer)
    List<? extends Number> list1 = null;
    List<Integer> list2 = null;
    list2 = list1;
    
    1. Yes.
    2. No.
  5. Will this code compile successfully? (1 correct answer)
    List<Number> list1 = null;
    List<? super Integer> list2 = null;
    list1 = list2;
    
    1. Yes.
    2. No.
  6. Will this code compile successfully? (1 correct answer)
    List<Number> list1 = null;
    List<? super Integer> list2 = null;
    list2 = list1;
    
    1. Yes.
    2. No.
  7. Will this code compile successfully? (1 correct answer)
    SortedSet<? super Number> set1 = null;
    SortedSet<Integer> set2 = null;
    set1 = set2;
    
    1. Yes.
    2. No.
  8. Will this code compile successfully? (1 correct answer)
    SortedSet<? super Number> set1 = null;
    SortedSet<Integer> set2 = null;
    set2 = set1;
    
    1. Yes.
    2. No.
  9. Will this code compile successfully? (1 correct answer)
    SortedSet<Number> set1 = null;
    SortedSet<? extends Integer> set2 = null;
    set1 = set2;
    
    1. Yes.
    2. No.
  10. Will this code compile successfully? (1 correct answer)
    SortedSet<Number> set1 = null;
    SortedSet<? extends Integer> set2 = null;
    set2 = set1;
    
    1. Yes.
    2. No.
  11. Will this code compile successfully? (1 correct answer)
    Queue<? extends Number> q1 = null;
    Queue<? super Integer> q2 = null;
    q1 = q2;
    
    1. Yes.
    2. No.
  12. Will this code compile successfully? (1 correct answer)
    Queue<? extends Number> q1 = null;
    Queue<? super Integer> q2 = null;
    q2 = q1;
    
    1. Yes.
    2. No.
  13. Will this code compile successfully? (1 correct answer)
    Queue<? super Number> q1 = null;
    Queue<? extends Integer> q2 = null;
    q1 = q2;
    
    1. Yes.
    2. No.
  14. Will this code compile successfully? (1 correct answer)
    Queue<? super Number> q1 = null;
    Queue<? extends Integer> q2 = null;
    q2 = q1;
    
    1. Yes.
    2. No.
  15. Will this code compile successfully? (1 correct answer)
    Queue<?> q1 = null;
    Queue<Integer> q2 = null;
    q1 = q2;
    
    1. Yes.
    2. No.
  16. Will this code compile successfully? (1 correct answer)
    LinkedList<?> list1 = null;
    LinkedList<Integer> list2 = null;
    list2 = list1;
    
    1. Yes.
    2. No.
  17. Will this code compile successfully? (1 correct answer)
    LinkedList<?> list1 = null;
    LinkedList<? extends Integer> list2 = null;
    list1 = list2;
    
    1. Yes.
    2. No.
  18. Will this code compile successfully? (1 correct answer)
    LinkedList<?> list1 = null;
    LinkedList<? extends Integer> list2 = null;
    list2 = list1;
    
    1. Yes.
    2. No.
  19. Will this code compile successfully? (1 correct answer)
    LinkedList<?> list1 = null;
    LinkedList<? super Integer> list2 = null;
    list1 = list2;
    
    1. Yes.
    2. No.
  20. Will this code compile successfully? (1 correct answer)
    LinkedList<?> list1 = null;
    LinkedList<? super Integer> list2 = null;
    list2 = list1;
    
    1. Yes.
    2. No.
  21. Will this code compile successfully? (1 correct answer)
    PriorityQueue queue1 = null;
    PriorityQueue<Integer> queue2 = null;
    queue1 = queue2;
    
    1. Yes, without warnings.
    2. Yes, with a warning.
    3. No.
  22. Will this code compile successfully? (1 correct answer)
    PriorityQueue queue1 = null;
    PriorityQueue<Integer> queue2 = null;
    queue2 = queue1;
    
    1. Yes, without warnings.
    2. Yes, with a warning.
    3. No.
  23. Will this code compile successfully? (1 correct answer)
    PriorityQueue<?> queue1 = null;
    PriorityQueue queue2 = null;
    queue1 = queue2;
    
    1. Yes, without warnings.
    2. Yes, with a warning.
    3. No.
  24. Will this code compile successfully? (1 correct answer)
    PriorityQueue<?> queue1 = null;
    PriorityQueue queue2 = null;
    queue1 = queue2;
    
    1. Yes, without warnings.
    2. Yes, with a warning.
    3. No.
  25. Will this code compile successfully? (1 correct answer)
    Set<Integer> set = new TreeSet<Integer>();
    set.add(10);
    
    1. Yes.
    2. No.
  26. Will this code compile successfully? (1 correct answer)
    Set<Integer> set = new TreeSet<Integer>();  
    set.add((int)1.0f);
    
    1. Yes.
    2. No.
  27. Will this code compile successfully? (1 correct answer)
    Set<Integer> set = new TreeSet<Integer>();
    set.add(10L);
    
    1. Yes.
    2. No.
  28. Will this code compile successfully? (1 correct answer)
    Set<Integer> set = new TreeSet<Integer>();
    int number = (short)10;
    set.add(number);
    
    1. Yes.
    2. No.
  29. Will this code compile successfully? (1 correct answer)
    Set<Number> set = new TreeSet<Integer>();
    set.add(10L);
    
    1. Yes.
    2. No.
  30. Will this code compile successfully? (1 correct answer)
    NavigableSet<?> set = new TreeSet<Object>();
    set.add(new Object());
    
    1. Yes.
    2. No.
  31. Will this code compile successfully? (1 correct answer)
    NavigableSet<? super Object> set = new TreeSet<Object>();
    set.add(new Object());
    
    1. Yes.
    2. No.
  32. Will this code compile successfully? (1 correct answer)
    NavigableSet<? extends Object> set = new TreeSet<Object>();
    set.add(new Object());
    
    1. Yes.
    2. No.
  33. Will this code compile successfully? (1 correct answer)
    NavigableSet<? extends String> set = new TreeSet<String>();
    set.add("string
    1. Yes.
    2. No.
  34. Will this code compile successfully? (1 correct answer)
    NavigableSet<? super String> set = new TreeSet<String>();
    set.add("string
    1. Yes.
    2. No.
  35. Will this code compile successfully? (1 correct answer)
    NavigableSet<? super String> set = new TreeSet<String>();
    set.add(new Object());
    
    1. Yes.
    2. No.
  36. Will this code compile successfully? (1 correct answer)
    List<? extends Integer> list = new ArrayList<Integer>();
    for (Integer element : list) {
     System.out.println(element);
    }
    
    1. Yes.
    2. No.
  37. Will this code compile successfully? (1 correct answer)
    List<? extends Integer> list = new ArrayList<Integer>();
    Integer first = list.get(0);
    
    1. Yes.
    2. No.
  38. Will this code compile successfully? (1 correct answer)
    List<? super Integer> list = new ArrayList<Integer>();
    for (Integer element : list) {
     System.out.println(element);
    }
    
    1. Yes.
    2. No.
  39. Will this code compile successfully? (1 correct answer)
    List<? super Integer> list = new ArrayList<Integer>();
    Integer first = list.get(0);
    
    1. Yes.
    2. No.
  40. Will this code compile successfully? (1 correct answer)
    List<? super Integer> list = new ArrayList<Integer>();
    Object first = list.get(0);
    
    1. Yes.
    2. No.
  41. Will this code compile successfully? (1 correct answer)
    import java.util.*;
    
    class Test {
        void say(Set<Double> set) {
        }
        void say(SortedSet<Double> set) {
        }
    }
    
    1. Yes.
    2. No.
  42. Will this code compile successfully? (1 correct answer)
    import java.util.*;
    
    class Test {
        void say(Set<Double> set) {
        }
        void say(Set<Boolean> set) {
        }
    }
    
    1. Yes.
    2. No.
  43. Will this code compile successfully? (1 correct answer)
    import java.util.*;
    
    class Test {
        void say(Set<Double> set) {
        }
        void say(Set<Double>... set) {
        }
    }
    
    1. Yes.
    2. No.
  44. 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>());
    }
    
    1. It prints “child”.
    2. It prints “parent”.
    3. Compilation fails.
  45. 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>>());
    }
    
    1. It prints “child”.
    2. It prints “parent”.
    3. Compilation fails.
  46. 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>());
    }
    
    1. It prints “child”.
    2. It prints “parent”.
    3. Compilation fails.
  47. 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>());
    }
    
    1. It prints “child”.
    2. It prints “parent”.
    3. Compilation fails.
  48. 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}
    }
    
    1. Yes.
    2. No.
  49. 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}
    }
    
    1. Yes.
    2. No.
  50. 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}
    }
    
    1. Yes.
    2. No.
  51. Will this code compile successfully? (1 correct answer)
    Object set = new TreeSet<Integer>();
    boolean flag = set instanceof NavigableSet<Integer>;
    
    1. Yes.
    2. No.
  52. 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
    
    1. It prints “yes”.
    2. It prints nothing.
  53. Will this code compile successfully? (1 correct answer)
    Class c = ArrayList<Integer>.class;
    
    1. Yes.
    2. No.
  54. Will this code compile successfully? (1 correct answer)
    Class c = new ArrayList<Integer>().getClass();
    
    1. Yes.
    2. No.
  55. Will this code compile successfully? (1 correct answer)
    new ArrayList<?>();
    
    1. Yes.
    2. No.
  56. Will this code compile successfully? (1 correct answer)
    new TreeMap<String, ? super Integer>();
    
    1. Yes.
    2. No.
  57. Will this code compile successfully? (1 correct answer)
    new ArrayList<Set<?>>();
    
    1. Yes.
    2. No.
  58. Will this code compile successfully? (1 correct answer)
    class Test extends ArrayList<? extends Number> {
    }
    
    1. Yes.
    2. No.
  59. Will this code compile successfully? (1 correct answer)
    class Test implements Comparable<?> {
        public int compareTo(Comparable<?> object) {
            return 0;
        }
    }
    
    1. Yes.
    2. No.
  60. Will this code compile successfully? (1 correct answer)
    class Test implements Comparable<Comparable<?>> {
        public int compareTo(Comparable<?> object) {
            return 0;
        }
    }
    
    1. Yes.
    2. No.
  61. Will this code compile successfully? (1 correct answer)
    class Test {
    <T> T getFirst(List<T> list) {
        return list.get(0);
    }
    }
    
    1. Yes.
    2. No.
  62. Will this code compile successfully? (1 correct answer)
    class Test {
    static <T> T getFirst(List<T> list) {
        return list.get(0);
    }
    }
    
    1. Yes.
    2. No.
  63. Will this code compile successfully? (1 correct answer)
    class Test <T> {
    T getFirst(List<T> list) {
        return list.get(0);
    }
    }
    
    1. Yes.
    2. No.
  64. Will this code compile successfully? (1 correct answer)
    class Test <T> {
    static T getFirst(List<T> list) {
        return list.get(0);
    }
    }
    
    1. Yes.
    2. No.
  65. Will this code compile successfully? (1 correct answer)
    class Test <T> {
    T instance;
    Test(T instance) {
        this.instance = instance;
    }
    }
    
    1. Yes.
    2. No.
  66. Will this code compile successfully? (1 correct answer)
    class Test <T> {
    Test(T my) {
        boolean b = (my instanceof T);
    }
    }
    
    1. Yes.
    2. No.
  67. Will this code compile successfully? (1 correct answer)
    class Test <T> {
    void test(T method) {
        Object my = (T)method;
    }
    }
    
    1. Yes.
    2. No.
  68. Will this code compile successfully? (1 correct answer)
    class Test <T> {
    void test() {
        NavigableMap map = new TreeMap<String, T>();
    }
    }
    
    1. Yes.
    2. No.
  69. Will this code compile successfully? (1 correct answer)
    class Test <T> {
    private T[] array = null;
    }
    
    1. Yes.
    2. No.
  70. Will this code compile successfully? (1 correct answer)
    class Test <T> {
    private T[] array = new T[7];
    }
    
    1. Yes.
    2. No.
  71. Will this code compile successfully? (1 correct answer)
    class Test<String> {
        String my = "Hello!";
    }
    
    1. Yes.
    2. No.
  72. 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());
        }
    }
    
    1. Yes.
    2. No.
© 2008 Nikos Pougounias. This is a free contribution to the Javacommunity. Please distribute it for free. https://nikojava.wordpress.com

Answers

  1. b
  2. b
  3. a
  4. b
  5. b
  6. a
  7. b
  8. b
  9. b
  10. b
  11. b
  12. b
  13. b
  14. b
  15. a
  16. b
  17. a
  18. b
  19. a
  20. b
  21. a
  22. b
  23. a
  24. a
  25. a
  26. a
  27. b
  28. a
  29. b
  30. b
  31. a
  32. b
  33. b
  34. a
  35. b
  36. a
  37. a
  38. b
  39. b
  40. a
  41. a
  42. b
  43. a
  44. a
  45. a
  46. a
  47. c
  48. b
  49. b
  50. b
  51. b
  52. a
  53. b
  54. a
  55. b
  56. b
  57. a
  58. b
  59. b
  60. a
  61. a
  62. a
  63. a
  64. b
  65. a
  66. b
  67. a
  68. a
  69. a
  70. b
  71. b
  72. a

No hay comentarios:

Publicar un comentario

Blogger Widgets