SCJP Mock exam for Collections


Step-by-step questions for Collections, in order to help you prepare for the Sun Certified Java Programmer for Java SE 6.

Questions 1-40 are about sets, 41-85 about maps, 86-110 about queues, 111-130 about lists.

questions
1-16 SortedSet
17-30 NavigableSet
31-40 LinkedHashSet, HashSet, TreeSet
41-56 SortedMap
57-67 LinkedHashMap, HashMap, TreeMap
68-85 NavigableMap
86-110 PriorityQueue, Queue
111-130 List, LinkedList, ArrayList

  1. SortedSet IS-A Set. (1 correct answer)
    1. true
    2. false

  2. SortedSet belongs to the java.util package. (1 correct answer)
    1. true
    2. false

  3. How many of the following methods of SortedSet may throw an exception at runtime? (1 correct answer)
        ⇒ first()
        ⇒ last()
        ⇒ headSet()
        ⇒ tailSet()
        ⇒ subSet()
        ⇒ comparator()

    1. None.
    2. Two.
    3. Three.
    4. Five.

  4. What’s the signature of the method headSet()? (1 correct answer)
    1. SortedSet<E> headSet(E)
    2. SortedSet<E> headSet(E, E)
    3. SortedSet<E> headSet(E, boolean)

  5. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        SortedSet<Integer> set = new TreeSet<Integer>();
        set.last();
    }
    
    1. A NoSuchElementException is thrown.
    2. It compiles and runs fine.
    3. Compilation fails.

  6. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        SortedSet<Integer> set = new TreeSet<Integer>();
        set.add(7);
        set.add(-12);
        SortedSet<Integer> sub = set.subSet(-100, 100);
        System.out.format("%d %d", set.size(), sub.size());
    }
    
    1. It prints “2 2”.
    2. It prints “2 0”.

  7. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        SortedSet<Integer> set = new TreeSet<Integer>();
        set.add(7);
        set.add(-12);
        SortedSet<Integer> sub = set.subSet(-100, 100);
        sub.add(9);
        System.out.format("%d %d", set.size(), sub.size());
    }
    
    1. An IllegalArgumentException is thrown.
    2. It prints “2 3”.
    3. It prints “2 1”.
    4. It prints “3 3”.

  8. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        SortedSet<Integer> set = new TreeSet<Integer>();
        set.add(7);
        set.add(-12);
        SortedSet<Integer> sub = set.subSet(-100, 100);
        sub.add(-100);
        System.out.format("%d %d", set.size(), sub.size());
    }
    
    1. An IllegalArgumentException is thrown.
    2. It prints “2 3”.
    3. It prints “2 1”.
    4. It prints “3 3”.

  9. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        SortedSet<Integer> set = new TreeSet<Integer>();
        set.add(7);
        set.add(-12);
        SortedSet<Integer> sub = set.subSet(-100, 100);
        sub.add(100);
        System.out.format("%d %d", set.size(), sub.size());
    }
    
    1. An IllegalArgumentException is thrown.
    2. It prints “2 3”.
    3. It prints “2 1”.
    4. It prints “3 3”.

  10. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        SortedSet<Integer> set = new TreeSet<Integer>();
        set.add(7);
        set.add(-12);
        SortedSet<Integer> sub = set.subSet(-100, 100);
        sub.add(99);
        System.out.format("%d %d", set.size(), sub.size());
    }
    
    1. An IllegalArgumentException is thrown.
    2. It prints “2 3”.
    3. It prints “2 1”.
    4. It prints “3 3”.

  11. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        SortedSet<Integer> set = new TreeSet<Integer>();
        set.add(7);
        set.add(-12);
        SortedSet<Integer> sub = set.subSet(-100, 100);
        sub.add(7);
        System.out.format("%d %d", set.size(), sub.size());
    }
    
    1. An IllegalArgumentException is thrown.
    2. It prints “2 1”.
    3. It prints “2 2”.

  12. What happens when this code gets compiled and executed? (1 correct answer)
    public static void main(String[] args) {
        SortedSet set = new TreeSet();
        set.add(7);
        set.add(-12);
        SortedSet sub = set.subSet("Hello!", 100);
        System.out.format("%d %d", set.size(), sub.size());
    }
    
    1. An IllegalArgumentException is thrown at runtime.
    2. A ClassCastException is thrown at runtime.
    3. Compilation fails.
    4. It prints “2 0”.
    5. It prints “2 2”.

  13. What happens when this code gets compiled and executed? (1 correct answer)
    public static void main(String[] args) {
        SortedSet<Integer> set = new TreeSet<Integer>();
        set.add("Hello!");
    }
    
    1. A ClassCastException is thrown at runtime.
    2. Compilation fails.
    3. None of the above.

  14. What happens when this code gets compiled and executed? (1 correct answer)
    public static void main(String[] args) {
        SortedSet set = new TreeSet();
        set.add("Hello!");
    }
    
    1. A ClassCastException is thrown at runtime.
    2. Compilation fails.
    3. None of the above.

  15. What happens when this code gets compiled and executed? (1 correct answer)
    public static void main(String[] args) {
        SortedSet set = new TreeSet();
        set.add("Hello!");
        set.add(1);
    }
    
    1. A ClassCastException is thrown at runtime.
    2. Compilation fails.
    3. None of the above.

  16. What happens when this code gets compiled and executed? (1 correct answer)
    public static void main(String[] args) {
        SortedSet set = new TreeSet();
        set.add(null);
        set.add(1);
    }
    
    1. An NullPointerException is thrown at runtime.
    2. A ClassCastException is thrown at runtime.
    3. Compilation fails.
    4. None of the above.

  17. NavigableSet extends SortedSet. (1 correct answer)
    1. true
    2. false

  18. How many of the following methods of NavigableSet may throw an exception at runtime? (1 correct answer)
        ⇒ lower()
        ⇒ higher()
        ⇒ floor()
        ⇒ ceiling()
        ⇒ pollFirst()
        ⇒ pollLast()

    1. None.
    2. Two.
    3. Four.
    4. All.

  19. What’s the signature of the method lower()? (1 correct answer)
    1. E lower(E)
    2. boolean lower(E)
    3. E lower(NavigableSet<E>)

  20. In the NavigableSet interface there are 2 overloaded methods with the name subSet. (1 correct answer)
    1. true
    2. false

  21. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableSet<Integer> set = new TreeSet<Integer>();
        set.add(-12);
        set.add(24);
        System.out.format("%d %d %d %d",
            set.lower(-12),
            set.lower(0),
            set.lower(24),
            set.lower(100)
        );
    }
    
    1. An exception is thrown at runtime.
    2. It prints “null -12 -12 24”.
    3. It prints “-12 -12 24 24”.

  22. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableSet<Integer> set = new TreeSet<Integer>();
        set.add(-12);
        set.add(24);
        System.out.format("%d %d %d %d",
            set.floor(-12),
            set.floor(0),
            set.floor(24),
            set.floor(100)
        );
    }
    
    1. An exception is thrown at runtime.
    2. It prints “null -12 -12 24”.
    3. It prints “-12 -12 24 24”.

  23. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableSet<Integer> set = new TreeSet<Integer>();
        set.add(-12);
        set.add(24);
        System.out.format("%d %d %d %d",
            set.higher(-12),
            set.higher(0),
            set.higher(24),
            set.higher(100)
        );
    }
    
    1. An exception is thrown at runtime.
    2. It prints “24 24 null null”.
    3. It prints “-12 24 24 null”.

  24. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableSet<Integer> set = new TreeSet<Integer>();
        set.add(-12);
        set.add(24);
        System.out.format("%d %d %d %d",
            set.ceiling(-12),
            set.ceiling(0),
            set.ceiling(24),
            set.ceiling(100)
        );
    }
    
    1. An exception is thrown at runtime.
    2. It prints “24 24 null null”.
    3. It prints “-12 24 24 null”.

  25. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableSet<Integer> set = new TreeSet<Integer>();
        set.add(-12);
        set.add(24);
        set.add(-28);
        set.add(-0);
        set.add(0);
        set.add(+0);
        set.add(11);
        set.add(145);
        System.out.format("%d %d %d %d",
            set.higher(-28),
            set.lower(24),
            set.floor(-0),
            set.ceiling(100)
        );
    }
    
    1. An exception is thrown at runtime.
    2. It prints “-12 11 0 100”.
    3. It prints “-12 11 0 145”.
    4. It prints “-28 24 0 100”.
    5. It prints “-28 24 0 145”.

  26. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableSet<Integer> set = new TreeSet<Integer>();
        set.pollFirst();
        System.out.println(set.size());
    }
    
    1. An exception is thrown at runtime.
    2. Compilation fails.
    3. It prints “0”.

  27. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableSet<Integer> set = new TreeSet<Integer>();
        set.first();
        System.out.println(set.size());
    }
    
    1. An exception is thrown at runtime.
    2. Compilation fails.
    3. It prints “0”.

  28. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableSet<Integer> set = new TreeSet<Integer>();
        set.add(1);
        set.add(2);
        set.add(4);
        NavigableSet<Integer> sub = set.headSet(4);
        System.out.println(sub.last());
    }
    
    1. An exception is thrown at runtime.
    2. Compilation fails.
    3. It prints “4”.
    4. It prints “2”.

  29. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableSet<Integer> set = new TreeSet<Integer>();
        set.add(1);
        set.add(2);
        set.add(4);
        NavigableSet<Integer> sub = set.headSet(4, true);
        System.out.println(sub.last());
    }
    
    1. An exception is thrown at runtime.
    2. Compilation fails.
    3. It prints “4”.
    4. It prints “2”.

  30. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableSet<Integer> set = new TreeSet<Integer>();
        set.add(1);
        set.add(2);
        set.add(4);
        for (Iterator iterator = set.descendingSet().iterator();
            iterator.hasNext();) {
            System.out.format("%d ", iterator.next());
        }
    }
    
    1. It prints “1 2 4 “.
    2. It prints “4 2 1 “.
    3. Compilation fails.

  31. What is the output of this code? (1 correct answer)
    import java.util.*;
    
    public class TestSet {
        static void add(Set<? super String> set) {
            set.add(null);
            System.out.println(set.size());
        }
        public static void main(String[] args) {
            Set<String> set = new HashSet<String>();
            add(set);
        }
    }
    
    1. It prints “0”.
    2. It prints “1”.
    3. An exception is thrown at runtime.

  32. What is the output of this code? (1 correct answer)
    import java.util.*;
    
    public class TestSet {
        static void add(Set<? super String> set) {
            set.add(null);
            set.add(null);
            System.out.println(set.size());
        }
        public static void main(String[] args) {
            Set<String> set = new HashSet<String>();
            add(set);
        }
    }
    
    1. It prints “0”.
    2. It prints “1”.
    3. It prints “2”.
    4. An exception is thrown at runtime.

  33. What is the output of this code? (1 correct answer)
    import java.util.*;
    
    public class TestSet {
        static void add(Set<? super String> set) {
            set.add("Hi");
            set.add("Hi");
            System.out.println(set.size());
        }
        public static void main(String[] args) {
            Set<String> set = new HashSet<String>();
            add(set);
        }
    }
    
    1. It prints “0”.
    2. It prints “1”.
    3. It prints “2”.
    4. An exception is thrown at runtime.

  34. What is the output of this code? (1 correct answer)
    import java.util.*;
    
    public class TestSet {
        static void add(Set set) {
            set.add("Hi");
            set.add(1);
            System.out.println(set.size());
        }
        public static void main(String[] args) {
            Set<String> set = new HashSet<String>();
            add(set);
        }
    }
    
    1. It prints “0”.
    2. It prints “1”.
    3. It prints “2”.
    4. An exception is thrown at runtime.

  35. What is the output of this code? (1 correct answer)
    import java.util.*;
    
    public class TestSet {
        static void add(Set<? super String> set) {
            set.add(null);
            System.out.println(set.size());
        }
        public static void main(String[] args) {
            Set<String> set = new TreeSet<String>();
            add(set);
        }
    }
    
    1. It prints “0”.
    2. It prints “1”.
    3. An exception is thrown at runtime.

  36. What is the output of this code? (1 correct answer)
    import java.util.*;
    
    public class TestSet {
        static void add(Set<? super String> set) {
            set.add(null);
            set.add(null);
            System.out.println(set.size());
        }
        public static void main(String[] args) {
            Set<String> set = new TreeSet<String>();
            add(set);
        }
    }
    
    1. It prints “0”.
    2. It prints “1”.
    3. It prints “2”.
    4. An exception is thrown at runtime.

  37. What is the output of this code? (1 correct answer)
    import java.util.*;
    
    public class TestSet {
        static void add(Set<? super String> set) {
            set.add("Hi");
            set.add("Hi");
            System.out.println(set.size());
        }
        public static void main(String[] args) {
            Set<String> set = new TreeSet<String>();
            add(set);
        }
    }
    
    1. It prints “0”.
    2. It prints “1”.
    3. It prints “2”.
    4. An exception is thrown at runtime.

  38. What is the output of this code? (1 correct answer)
    import java.util.*;
    
    public class TestSet {
        static void add(Set set) {
            set.add("Hi");
            set.add(1);
            System.out.println(set.size());
        }
        public static void main(String[] args) {
            Set<String> set = new TreeSet<String>();
            add(set);
        }
    }
    
    1. It prints “0”.
    2. It prints “1”.
    3. It prints “2”.
    4. An exception is thrown at runtime.

  39. What is the output of this code? (1 correct answer)
    import java.util.*;
    
    public class TestSet {
        static void print(Set<? extends String> set) {
            for (String element : set) {
                System.out.format("%s ", element);
            }
        }
        public static void main(String[] args) {
            Set<String> set = new LinkedHashSet<String>();
            set.add("A");
            set.add("J");
            set.add("A");
            set.add("X");
            print(set);
        }
    }
    
    1. It prints “A J A X “.
    2. It prints “A J X “.
    3. The output cannot be determined.

  40. What is the output of this code? (1 correct answer)
    import java.util.*;
    
    public class TestSet {
        static void print(Set<? extends String> set) {
            for (String element : set) {
                System.out.format("%s ", element);
            }
        }
        public static void main(String[] args) {
            Set<String> set = new HashSet<String>();
            set.add("A");
            set.add("J");
            set.add("A");
            set.add("X");
            print(set);
        }
    }
    
    1. It prints “A J A X “.
    2. It prints “A J X “.
    3. The output cannot be determined.

  41. SortedMap IS-A Collection. (1 correct answer)
    1. true
    2. false

  42. SortedMap IS-A Map. (1 correct answer)
    1. true
    2. false

  43. All the following methods are defined in SortedMap. (1 correct answer)
        ⇒ firstKey()
        ⇒ lastKey()
        ⇒ headMap()
        ⇒ tailMap()
        ⇒ subMap()
        ⇒ comparator()

    1. true
    2. false

  44. How many of the following methods of SortedMap may throw an exception at runtime? (1 correct answer)
        ⇒ firstKey()
        ⇒ lastKey()
        ⇒ headMap()
        ⇒ tailMap()
        ⇒ subMap()
        ⇒ comparator()

    1. None.
    2. Two.
    3. Three.
    4. Five.

  45. Consider SortedMap<K,V>. What’s the signature of the method firstKey()? (1 correct answer)
    1. K firstKey()
    2. V firstKey()
    3. K firstKey(K)
    4. V firstKey(K)

  46. Consider SortedMap<K,V>. What’s the signature of the method tailMap()? (1 correct answer)
    1. SortedMap<K,V> tailMap(K)
    2. SortedMap<K,V> tailMap(K, V)
    3. SortedMap<K,V> tailMap(K, boolean)

  47. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        SortedMap<String, Integer> map = new TreeMap<String, Integer>();
        map.last();
    }
    
    1. A NoSuchElementException is thrown.
    2. It compiles and runs fine.
    3. Compilation fails.

  48. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        SortedMap<String, Integer> map = new TreeMap<String, Integer>();
        map.lastKey();
    }
    
    1. A NoSuchElementException is thrown.
    2. It compiles and runs fine.
    3. Compilation fails.

  49. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        SortedMap<String, Integer> map = new TreeMap<String, Integer>();
        map.lastEntry();
    }
    
    1. A NoSuchElementException is thrown.
    2. It compiles and runs fine.
    3. Compilation fails.

  50. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        SortedMap<String, Integer> map = new TreeMap<String, Integer>();
        map.put("B", 1);
        System.out.println(map.put("B", 2));
    }
    
    1. It prints “B”.
    2. It prints “null”.
    3. It prints “1”.
    4. It prints “2”.

  51. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        SortedMap<String, Integer> map = new TreeMap<String, Integer>();
        map.put("B", 1);
        map.put("B", 2);
        map.put("a", 4);
        System.out.format("%s %d",
            map.lastKey(),
            map.size());
    }
    
    1. It prints “a 2”.
    2. It prints “a 3”.
    3. It prints “B 2”.
    4. It prints “B 3”.

  52. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        SortedMap<String, Integer> map = new TreeMap<String, Integer>();
        map.put("K", 1);
        map.put("B", 2);
        map.put("F", 3);
        System.out.println(map.tailMap("C").size());
    }
    
    1. It prints “0”.
    2. It prints “1”.
    3. It prints “2”.
    4. It prints “3”.

  53. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        SortedMap<String, Integer> map = new TreeMap<String, Integer>();
        map.put("K", 1);
        map.put("B", 2);
        map.put("F", 3);
        System.out.println(map.tailMap("C").put("D", 1));
    }
    
    1. An IllegalArgumentException is thrown.
    2. It prints “null”.
    3. It prints “D”.
    4. It prints “F”.

  54. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        SortedMap<String, Integer> map = new TreeMap<String, Integer>();
        map.put("K", 1);
        map.put("B", 2);
        map.put("F", 3);
        System.out.println(map.tailMap("c").size());
    }
    
    1. An IllegalArgumentException is thrown.
    2. It prints “0”.
    3. It prints “1”.
    4. It prints “2”.
    5. It prints “3”.

  55. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        SortedMap<String, Integer> map = new TreeMap<String, Integer>();
        map.put("K", 1);
        map.put("B", 2);
        map.put("F", 3);
        System.out.println(map.tailMap("c").put("c", 1));
    }
    
    1. An IllegalArgumentException is thrown.
    2. It prints “null”.
    3. It prints “c”.
    4. It prints “F”.

  56. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        SortedMap<String, Integer> map = new TreeMap<String, Integer>();
        map.put("K", 1);
        map.put("B", 2);
        map.put("F", 3);
        System.out.println(map.tailMap("c").put("A", 1));
    }
    
    1. An IllegalArgumentException is thrown.
    2. It prints “null”.
    3. It prints “c”.
    4. It prints “F”.

  57. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        Map map = new TreeMap();
        map.put("K", 1);
        map.put("L", "L");
    }
    
    1. An exception is thrown at runtime.
    2. Compilation fails.
    3. None of the above.

  58. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        Map map = new TreeMap();
        map.put("K", 1);
        map.put(2, "L");
    }
    
    1. An exception is thrown at runtime.
    2. Compilation fails.
    3. None of the above.

  59. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        Map map = new LinkedHashMap();
        map.put("K", 1);
        map.put(2, "L");
    }
    
    1. An exception is thrown at runtime.
    2. Compilation fails.
    3. None of the above.

  60. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
        Map map = new LinkedHashMap();
        map.put("K", 1);
        map.put(2, "L");
        for (Object element : map.keySet()) {
            System.out.print(element);
        }
    }
    
    1. An exception is thrown at runtime.
    2. Compilation fails.
    3. None of the above.

  61. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
    	new LinkedHashMap<String, Integer>().put(null, null);
    	new HashMap<Object, Number>().put(null, null);
    	new TreeMap<String, Queue<String>>().put(null, null);
    }
    
    1. An exception is thrown at runtime.
    2. It compiles and runs fine.
    3. Compilation fails.

  62. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
    	new Hashtable().put(1, null);
    }
    
    1. An exception is thrown at runtime.
    2. It compiles and runs fine.
    3. Compilation fails.

  63. What happens when this code gets executed? (1 correct answer)
    public static void main(String[] args) {
    	new Hashtable().put(null, 1);
    }
    
    1. An exception is thrown at runtime.
    2. It compiles and runs fine.
    3. Compilation fails.

  64. What happens when this code gets compiled and executed? (1 correct answer)
    public static void main(String[] args) {
        Map<String, Integer> map = new LinkedHashMap<String, Integer>();
        map.put("J", 1);
        map.put("A", 2);
        map.put("V", 3);
        map.put("A", 4);
        for (Object element : map.keySet()) {
            System.out.format("%s ", element);
        }
    }
    
    1. It prints “J A V A”
    2. It prints “J A V”
    3. It prints “A J V”
    4. The output order cannot be guaranteed.

  65. What happens when this code gets compiled and executed? (1 correct answer)
    public static void main(String[] args) {
        Map<String, Integer> map = new TreeMap<String, Integer>();
        map.put("J", 1);
        map.put("A", 2);
        map.put("V", 3);
        map.put("A", 4);
        for (Object element : map.keySet()) {
            System.out.format("%s ", element);
        }
    }
    
    1. It prints “J A V A”
    2. It prints “J A V”
    3. It prints “A J V”
    4. The output order cannot be guaranteed.

  66. What happens when this code gets compiled and executed? (1 correct answer)
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("T", 1);
        map.put("M", 2);
        map.keySet().add("A");
        System.out.println(map.size());
    }
    
    1. It prints “2”
    2. It prints “3”
    3. An exception is thrown at runtime.

  67. What happens when this code gets compiled and executed? (1 correct answer)
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("T", 1);
        map.put("M", 2);
        map.keySet().remove("T");
        System.out.println(map.size());
    }
    
    1. It prints “1”
    2. It prints “2”
    3. An exception is thrown at runtime.

  68. All the following methods belong to NavigableMap. (1 correct answer)
        ⇒ lowerEntry()
        ⇒ higherEntry()
        ⇒ floorEntry()
        ⇒ ceilingEntry()

    1. true
    2. false

  69. All the following methods belong to NavigableMap. (1 correct answer)
        ⇒ pollFirstEntry()
        ⇒ pollLastEntry()
        ⇒ firstEntry()
        ⇒ lastEntry()

    1. true
    2. false

  70. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = 
        new TreeMap<Integer, String>();
        map.put(5, "D");
        map.put(-1, "A");
        map.put(7, "O");
        System.out.format("%d %d %d %d",
            map.lowerKey(-100),
            map.lowerKey(5),
            map.lowerKey(6),
            map.lowerKey(100)
    	);
    }
    
    1. It prints “null -1 5 7”.
    2. It prints “null 5 5 7”.

  71. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = 
        new TreeMap<Integer, String>();
        map.put(5, "D");
        map.put(-1, "A");
        map.put(7, "O");
        System.out.format("%d %d %d %d",
            map.floorKey(-100),
            map.floorKey(5),
            map.floorKey(6),
            map.floorKey(100)
    	);
    }
    
    1. It prints “null -1 5 7”.
    2. It prints “null 5 5 7”.

  72. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = 
        new TreeMap<Integer, String>();
        map.put(5, "D");
        map.put(-1, "A");
        map.put(7, "O");
        System.out.format("%d %d %d %d",
            map.higherKey(-100),
            map.higherKey(5),
            map.higherKey(6),
            map.higherKey(100)
    	);
    }
    
    1. It prints “-1 7 7 null”.
    2. It prints “-1 5 7 null”.

  73. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = 
        new TreeMap<Integer, String>();
        map.put(5, "D");
        map.put(-1, "A");
        map.put(7, "O");
        System.out.format("%d %d %d %d",
            map.ceilingKey(-100),
            map.ceilingKey(5),
            map.ceilingKey(6),
            map.ceilingKey(100)
    	);
    }
    
    1. It prints “-1 7 7 null”.
    2. It prints “-1 5 7 null”.

  74. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = 
        new TreeMap<Integer, String>();
        map.put(5, "D");
        map.put(-1, "A");
        map.put(7, "O");
        System.out.format("%s %s %s %s",
            map.lowerEntry(-100),
            map.lowerEntry(5),
            map.lowerEntry(6),
            map.lowerEntry(100)
    	);
    }
    
    1. It prints “null -1=A 5=D 7=O”.
    2. It prints “null 5=D 5=D 7=O”.

  75. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = 
        new TreeMap<Integer, String>();
        map.put(5, "D");
        map.put(-1, "A");
        map.put(7, "O");
        System.out.format("%s %s %s %s",
            map.floorEntry(-100),
            map.floorEntry(5),
            map.floorEntry(6),
            map.floorEntry(100)
    	);
    }
    
    1. It prints “null -1=A 5=D 7=O”.
    2. It prints “null 5=D 5=D 7=O”.

  76. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = 
        new TreeMap<Integer, String>();
        map.put(5, "D");
        map.put(-1, "A");
        map.put(7, "O");
        System.out.format("%s %s %s %s",
            map.higherEntry(-100),
            map.higherEntry(5),
            map.higherEntry(6),
            map.higherEntry(100)
    	);
    }
    
    1. It prints “-1=A 7=O 7=O null”.
    2. It prints “-1=A 5=D 7=O null”.

  77. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = 
        new TreeMap<Integer, String>();
        map.put(5, "D");
        map.put(-1, "A");
        map.put(7, "O");
        System.out.format("%s %s %s %s",
            map.ceilingEntry(-100),
            map.ceilingEntry(5),
            map.ceilingEntry(6),
            map.ceilingEntry(100)
    	);
    }
    
    1. It prints “-1=A 7=O 7=O null”.
    2. It prints “-1=A 5=D 7=O null”.

  78. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = 
        new TreeMap<Integer, String>();
        map.put(34, "J");
        map.put(-1, "a");
        map.put(70, "v");
        map.put(5, "a");
        map.put(-1, "2");
        System.out.format("%s %s %s %s",
            map.ceilingEntry(-100),
            map.floorEntry(5),
            map.higherEntry(6),
            map.lowerKey(5)
        );
    }
    
    1. It prints “-1=2 5=a 34=J -1”.
    2. It prints “-1=a 5=a 34=J -1”.
    3. It prints “-1=2 5=a 34=J 5”.
    4. It prints “-1=a 5=a 34=J 5”.

  79. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = 
        new TreeMap<Integer, String>();
        map.lastEntry();
        System.out.println(map.size());
    }
    
    1. An exception is thrown at runtime.
    2. Compilation fails.
    3. It prints “0”.

  80. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = 
        new TreeMap<Integer, String>();
        map.pollFirstEntry();
        System.out.println(map.size());
    }
    
    1. An exception is thrown at runtime.
    2. Compilation fails.
    3. It prints “0”.

  81. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = 
        new TreeMap<Integer, String>();
        map.firstKey();
        System.out.println(map.size());
    }
    
    1. An exception is thrown at runtime.
    2. Compilation fails.
    3. It prints “0”.

  82. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = 
        new TreeMap<Integer, String>();
        map.put(1, "A");
        map.put(2, "B");
        map.put(3, "C");
        NavigableMap<Integer, String> sub =
        map.subMap(0, 3);
        System.out.println(sub.lastKey());
    }
    
    1. It prints “2”.
    2. It prints “3”.
    3. Compilation fails.
    4. An exception is thrown at runtime.

  83. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = 
        new TreeMap<Integer, String>();
        map.put(1, "A");
        map.put(2, "B");
        map.put(3, "C");
        NavigableMap<Integer, String> sub =
        map.subMap(0, false, 3, false);
        System.out.println(sub.lastKey());
    }
    
    1. It prints “2”.
    2. It prints “3”.
    3. Compilation fails.
    4. An exception is thrown at runtime.

  84. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = 
        new TreeMap<Integer, String>();
        map.put(1, "A");
        map.put(2, "B");
        map.put(3, "C");
        NavigableMap<Integer, String> sub =
        map.subMap(0, false, 3, false);
        map.put(4, "D");
        System.out.format("%d %d", map.size(), sub.size());
    }
    
    1. It prints “2 2”.
    2. It prints “3 2”.
    3. It prints “3 3”.
    4. It prints “4 2”.
    5. It prints “4 3”.
    6. Compilation fails.
    7. An exception is thrown at runtime.

  85. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        NavigableMap<Integer, String> map = 
        new TreeMap<Integer, String>();
        map.put(1, "A");
        map.put(2, "B");
        map.put(3, "C");
        NavigableMap<Integer, String> sub =
        map.subMap(0, false, 3, false);
        sub.put(4, "D");
        System.out.format("%d %d", map.size(), sub.size());
    }
    
    1. It prints “2 2”.
    2. It prints “3 2”.
    3. It prints “3 3”.
    4. It prints “4 2”.
    5. It prints “4 3”.
    6. Compilation fails.
    7. An exception is thrown at runtime.

  86. Queue is an interface. (1 correct answer)
    1. true
    2. false

  87. PriorityQueue IS-A Queue. (1 correct answer)
    1. true
    2. false

  88. PriorityQueue IS-A AbstractQueue. (1 correct answer)
    1. true
    2. false

  89. All the following are methods of Queue. (1 correct answer)
        ⇒ offer()
        ⇒ poll()
        ⇒ peek()
        ⇒ add()
        ⇒ remove()
        ⇒ element()

    1. true
    2. false

  90. How many of the following methods of Queue may throw an exception at runtime? (1 correct answer)
        ⇒ offer()
        ⇒ poll()
        ⇒ peek()

    1. One.
    2. Two.
    3. Three.

  91. How many of the following methods of Queue may throw an exception at runtime? (1 correct answer)
        ⇒ add()
        ⇒ remove()
        ⇒ element()

    1. One.
    2. Two.
    3. Three.

  92. Consider Queue<E>. What’s the signature of the method offer()? (1 correct answer)
    1. E offer(E)
    2. void offer(E)
    3. boolean offer(E)

  93. Consider Queue<E>. What’s the signature of the method peek()? (1 correct answer)
    1. E peek()
    2. E peek(E)
    3. boolean peek(E)

  94. In the Queue interface there are 2 overloaded methods with the name remove. (1 correct answer)
    1. true
    2. false

  95. In the Queue interface both add() and offer() add an element to the queue. (1 correct answer)
    1. true
    2. false

  96. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        Queue<String> queue = new PriorityQueue<String>();
        System.out.println(queue.add(null));
    }
    
    1. It prints “true”.
    2. It prints “false”.
    3. An exception is thrown at runtime.

  97. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        Queue<String> queue = new PriorityQueue<String>();
        System.out.println(queue.offer(null));
    }
    
    1. It prints “true”.
    2. It prints “false”.
    3. An exception is thrown at runtime.

  98. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        Queue<String> queue = new PriorityQueue<String>();
        System.out.println(queue.peek());
    }
    
    1. It prints “null”.
    2. An exception is thrown at runtime.

  99. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        Queue<String> queue = new PriorityQueue<String>();
        System.out.println(queue.poll());
    }
    
    1. It prints “null”.
    2. An exception is thrown at runtime.

  100. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        Queue<String> queue = new PriorityQueue<String>();
        System.out.println(queue.element());
    }
    
    1. It prints “null”.
    2. An exception is thrown at runtime.

  101. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        Queue<String> queue = new PriorityQueue<String>();
        System.out.println(queue.remove());
    }
    
    1. It prints “null”.
    2. An exception is thrown at runtime.

  102. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        Queue<String> queue = new PriorityQueue<String>();
        System.out.format("%b %b %d",
            queue.add("A"),
            queue.add("A"),
            queue.size());
    }
    
    1. It prints “true true 2”.
    2. It prints “true false 1”.
    3. An exception is thrown at runtime.

  103. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        Queue<String> queue = new PriorityQueue<String>();
        queue.add("E");
        queue.add("J");
        queue.add("B");
        queue.add("3");
        System.out.println(queue.element());
    }
    
    1. It prints “B”.
    2. It prints “J”.
    3. It prints “3”.

  104. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        Queue<String> queue = new PriorityQueue<String>(
        Collections.reverseOrder());
        queue.add("E");
        queue.add("J");
        queue.add("B");
        queue.add("3");
        System.out.println(queue.element());
    }
    
    1. It prints “B”.
    2. It prints “J”.
    3. It prints “3”.
    4. Compilation fails.

  105. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        Queue<String> queue = new PriorityQueue<String>(
        4, Collections.reverseOrder());
        queue.add("E");
        queue.add("J");
        queue.add("B");
        queue.add("3");
        System.out.println(queue.element());
    }
    
    1. It prints “B”.
    2. It prints “J”.
    3. It prints “3”.
    4. Compilation fails.

  106. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        Queue<String> queue = new PriorityQueue<String>();
        queue.add("J");
        queue.add("A");
        queue.add("V");
        queue.add("A");
        Arrays.sort(queue.toArray());
        for (String element : queue) {
            System.out.format("%s ", element);
        }
    }
    
    1. It prints “J A V A”.
    2. It prints “A A J V”.
    3. The output order is not guaranteed.

  107. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        Queue<String> queue = new PriorityQueue<String>();
        queue.add("J");
        queue.add("A");
        queue.add("V");
        queue.add("A");
        String[] array = queue.toArray(new String[0]);
        Arrays.sort(array);
        for (String element : array) {
            System.out.format("%s ", element);
        }
    }
    
    1. It prints “J A V A”.
    2. It prints “A A J V”.
    3. The output order is not guaranteed.

  108. What happens when this code is executed? (1 correct answer)
    public static void main(String[] args) {
        Queue queue = new PriorityQueue();
        queue.add(null);
    }
    
    1. An exception is thrown at runtime.
    2. It runs just fine.

  109. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        Queue queue = new PriorityQueue();
        queue.add(1);
        queue.add("J");
        System.out.print(queue.element());
    }
    
    1. It prints “J”.
    2. It prints “1”.
    3. An exception is thrown at runtime.

  110. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {		
        Queue<String> queue = new PriorityQueue<String>();
        queue.add("Z");
        queue.add("T");
        queue.add("F");
        queue.add("A");
        queue.add("A");
        queue.add("C");
        queue.offer("G");
        queue.poll();
        queue.peek();
        queue.remove();
        queue.element();
        System.out.format("%s %d",
            queue.poll(),
            queue.size()
        );
    }
    
    1. It prints “A 6”.
    2. It prints “A 5”.
    3. It prints “C 5”.
    4. It prints “C 4”.
    5. It prints “F 4”.
    6. It prints “F 3”.

  111. List is an interface. (1 correct answer)
    1. true
    2. false

  112. LinkedList IS-A List. Also, LinkedList IS-A Queue since Java 1.5. (1 correct answer)
    1. true
    2. false

  113. In the List interface there are two overloaded methods with the name remove.
    1. true
    2. false

  114. In the List interface there are two overloaded methods with the name addAll.
    1. true
    2. false

  115. Consider List<E>. What is the signature of the method lastIndexOf?
    1. E lastIndexOf(int)
    2. int lastIndexOf(E)
    3. int lastIndexOf(Object)

  116. Consider List<E>. What is the signature of the method set?
    1. E set(int, E)
    2. int set(int, E)
    3. boolean set(int, E)

  117. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        List<Integer> sub = list.subList(0, 1);
        System.out.println(sub.size());
    }
    
    1. It prints “0”.
    2. It prints “1”.
    3. Compilation fails.
    4. An exception is thrown at runtime.

  118. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        List<Integer> sub = list.subList(0, 0);
        System.out.println(sub.size());
    }
    
    1. It prints “0”.
    2. It prints “1”.
    3. Compilation fails.
    4. An exception is thrown at runtime.

  119. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(1);
        list.add(2);
        for (int element : list) {
            System.out.format("%d ", element);
        }
    }
    
    1. It prints “1 2 “.
    2. Compilation fails.

  120. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        for (int element : list) {
            System.out.format("%d ", element);
        }
    }
    
    1. It prints “1 2 “.
    2. Compilation fails.

  121. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        for (Object element : list) {
            System.out.format("%d ", element);
        }
    }
    
    1. It prints “1 2 “.
    2. Compilation fails.

  122. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(1);
        list.add(2);
        for (Object element : list) {
            System.out.format("%d ", element);
        }
    }
    
    1. It prints “1 2 “.
    2. Compilation fails.

  123. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        List<String> list = new LinkedList<String>();
        list.add("X");
        list.add("M");
        list.add("L");
        for (String element : list) {
            System.out.format("%s ", element);
        }
    }
    
    1. It prints “X M L “.
    2. Compilation fails.

  124. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        List<String> list = new LinkedList<String>();
        list.add("X");
        list.add("M");
        list.add("L");
        System.out.println(list.peek());
    }
    
    1. It prints “X”.
    2. It prints “L”.
    3. Compilation fails.

  125. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<String>();
        list.add("X");
        list.add("M");
        list.add("L");
        System.out.println(list.peek());
    }
    
    1. It prints “X”.
    2. It prints “L”.
    3. Compilation fails.

  126. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        List<Number> list = new ArrayList<Number>();
        list.add(7);
        list.add(8);
        list.add(9);
        list.remove(7);
        System.out.println(list.size());
    }
    
    1. It prints “2”.
    2. It prints “3”.
    3. An exception is thrown at runtime.

  127. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        List<Number> list = new ArrayList<Number>();
        list.add(7);
        list.add(8);
        list.add(9);
        list.remove(Integer.valueOf(7));
        System.out.println(list.size());
    }
    
    1. It prints “2”.
    2. It prints “3”.
    3. An exception is thrown at runtime.

  128. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        List<Number> list = new ArrayList<Number>();
        list.add(7);
        list.add(8);
        list.add(7);
        Number index = list.get(7);
        System.out.println(index);
    }
    
    1. It prints “0”.
    2. It prints “2”.
    3. An exception is thrown at runtime.

  129. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        List<Number> list = new ArrayList<Number>();
        list.add(7);
        list.add(8);
        list.add(7);
        Number index = list.get(Integer.valueOf(7));
        System.out.println(index);
    }
    
    1. It prints “0”.
    2. It prints “2”.
    3. An exception is thrown at runtime.

  130. What is the output of this code? (1 correct answer)
    public static void main(String[] args) {
        List<Number> list = new ArrayList<Number>();
        System.out.format("%b %b %b %d",
            list.add(7),
            list.add(null),
            list.add(7),
            list.size());
    }
    
    1. It prints “true false false 1”.
    2. It prints “true true false 2”.
    3. It prints “true false true 2”.
    4. It prints “true true true 3”.
    5. An exception is thrown at runtime.

© 2008 Nikos Pougounias. This is a free contribution to the Java community. Please distribute it for free. https://nikojava.wordpress.com

Answers

  1. a
  2. a
  3. d
  4. a
  5. a
  6. a
  7. d
  8. d
  9. a
  10. d
  11. c
  12. b
  13. b
  14. c
  15. a
  16. a
  17. a
  18. c
  19. a
  20. a
  21. b
  22. c
  23. b
  24. c
  25. b
  26. c
  27. a
  28. b
  29. c
  30. b
  31. b
  32. b
  33. b
  34. c
  35. b
  36. d
  37. b
  38. d
  39. b
  40. c
  41. b
  42. a
  43. a
  44. d
  45. a
  46. a
  47. c
  48. a
  49. c
  50. c
  51. a
  52. c
  53. b
  54. b
  55. b
  56. a
  57. c
  58. a
  59. c
  60. c
  61. b
  62. a
  63. a
  64. b
  65. c
  66. c
  67. a
  68. a
  69. a
  70. a
  71. b
  72. a
  73. b
  74. a
  75. b
  76. a
  77. b
  78. a
  79. c
  80. c
  81. a
  82. c
  83. a
  84. d
  85. g
  86. a
  87. a
  88. a
  89. a
  90. a
  91. c
  92. c
  93. a
  94. a
  95. a
  96. c
  97. c
  98. a
  99. a
  100. b
  101. b
  102. a
  103. c
  104. d
  105. b
  106. c
  107. b
  108. a
  109. c
  110. d
  111. a
  112. a
  113. a
  114. a
  115. c
  116. a
  117. d
  118. a
  119. b
  120. a
  121. a
  122. a
  123. a
  124. c
  125. a
  126. c
  127. a
  128. c
  129. c
  130. d

18 Responses to SCJP Mock exam for Collections

  1. Srilatha says:

    Hi, Could you please verify the answer of 54 once? I guess the answer should be b.

  2. Nikos says:

    Hi, you are right! The answer is b. Thank you!

  3. G says:

    Hi, I think the answer for 106) is b

  4. […] Free SCJP Mock exam for Collections […]

  5. Aruna says:

    Hi,,its an Excellent Practice Material.Thanks a lot.keep it up.

  6. Pedro Parra says:

    The answer for the question 25 should be c, -12 11 0 145

  7. Alex Serna says:

    Thank you. Excellent Job!!!

  8. Azat says:

    Thanks,

    I only tried first 40. But at question 25, I tested,
    answer should be “c” not “b”.

  9. java pocetnica says:

    Hi,
    Great Job, tnx very much🙂
    I hope I will pass exam

  10. arpit says:

    thanks for providing such a best Mock……

  11. rkb says:

    Errata:
    25-c, 94-b (maybe; ref: API docs),

  12. balaji says:

    Niko, You are genius. You know how the brain thinks. The design of the flow of questions are really good.

  13. Hiral Jhaveri says:

    Many of the code in questions are cut off from side and not fully visible. I would be glad if you fix it.
    Thank you🙂

  14. devi says:

    can some one let me know why the compilation is failing here?

    public static void main(String[] args) {

    NavigableSet set = new TreeSet();
    set.add(1);
    set.add(2);
    set.add(4);

    NavigableSet s = set.navigableHeadSet(4);

    }

  15. Gvesh says:

    35 ans must be c -NullPointerException

  16. Robin Kumar says:

    answer of 61 must be ‘a’. NullPointerException

  17. 25 is C
    61 is A
    106 is B

  18. Narayana says:

    Hi Devi,

    Actually I didn’t understand from where you got this method?
    set.navigableHeadSet(4);

    If you want to return a headset related to NavigableSet, then you can go for set.headSet(4,true) which returns a NavigableSet to you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: