Archive for the ‘SCJP 6’ Category

Free SCJP Mock exams

10 October 2008

These practice exams focus on specific objectives of the Sun Certified Java Programmer certification. They should be useful at the final stages of your preparation.

© 2008-2009 Nikos Pougounias. This is a free contribution to the Java and JavaRanch communities. Please distribute it for free.

SCJP Mock exam for Generics

9 October 2008

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
  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 Java community. Please distribute it for free. http://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

SCJP Mock exam for Collections

6 October 2008

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. http://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

SCJP Mock exam for Overriding & Overloading

2 October 2008
  1. These classes are defined in separate files. Will this code compile successfully? (1 correct answer)
    package dir1;
    class Parent {
        public java.util.Set<String> set;
    }
    
    package dir2;
    class Child extends dir1.Parent {
        void test() {
            set.add("Hello");
        }
    }
    
    1. Yes.
    2. No.

  2. These classes are defined in separate files. Will this code compile successfully? (1 correct answer)
    package dir1;
    class Parent {
        protected java.util.Set<String> set;
    }
    
    package dir2;
    class Child extends dir1.Parent {
        void test() {
            set.add("Hello");
        }
    }
    
    1. Yes.
    2. No.

  3. These classes are defined in separate files. Will this code compile successfully? (1 correct answer)
    package dir1;
    public class Parent {
        public java.util.Set<String> set;
    }
    
    package dir2;
    public class Child extends dir1.Parent {
        void test() {
            set.add("Hello");
        }
    }
    
    1. Yes.
    2. No.

  4. These classes are defined in separate files. Will this code compile successfully? (1 correct answer)
    package dir1;
    public class Parent {
        protected java.util.Set<String> set;
    }
    
    package dir2;
    public class Child extends dir1.Parent {
        void test() {
            set.add("Hello");
        }
    }
    
    1. Yes.
    2. No.

  5. These classes are defined in separate files. Will this code compile successfully? (1 correct answer)
    package dir1;
    class Parent {
        java.util.Set<String> set;
    }
    
    package dir2;
    class Child extends dir1.Parent {
        void test() {
            set.add("Hello");
        }
    }
    
    1. Yes.
    2. No.

  6. These classes are defined in separate files. Will this code compile successfully? (1 correct answer)
    package dir1;
    class Parent {
        private java.util.Set<String> set;
    }
    
    package dir2;
    class Child extends dir1.Parent {
        void test() {
            set.add("Hello");
        }
    }
    
    1. Yes.
    2. No.

  7. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
    }
    
    class Child extends Parent {
        String message = "child";
    }
    
    public class Test {
        public static void main(String[] args) {
            System.out.println(new Parent().message);
        }
    }
    
    1. parent
    2. child

  8. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
    }
    
    class Child extends Parent {
        String message = "child";
    }
    
    public class Test {
        public static void main(String[] args) {
            System.out.println(new Child().message);
        }
    }
    
    1. parent
    2. child

  9. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
    }
    
    class Child extends Parent {
        String message = "child";
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent yo = new Child();
            System.out.println(yo.message);
        }
    }
    
    1. parent
    2. child

  10. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
        void say() {
            System.out.println(message);
        }
    }
    
    class Child extends Parent {
        String message = "child";
    }
    
    public class Test {
        public static void main(String[] args) {
            new Parent().say();
        }
    }
    
    1. parent
    2. child

  11. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
        void say() {
            System.out.println(message);
        }
    }
    
    class Child extends Parent {
        String message = "child";
    }
    
    public class Test {
        public static void main(String[] args) {
            new Child().say();
        }
    }
    
    1. parent
    2. child

  12. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
        void say() {
            System.out.println(message);
        }
    }
    
    class Child extends Parent {
        String message = "child";
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent yo = new Child();
            yo.say();
        }
    }
    
    1. parent
    2. child

  13. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
        void say() {
            System.out.println(message);
        }
    }
    
    class Child extends Parent {
        String message = "child";
        void say() {
            System.out.println(message);
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            new Parent().say();
        }
    }
    
    1. parent
    2. child

  14. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
        void say() {
            System.out.println(message);
        }
    }
    
    class Child extends Parent {
        String message = "child";
        void say() {
            System.out.println(message);
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            new Child().say();
        }
    }
    
    1. parent
    2. child

  15. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
        void say() {
            System.out.println(message);
        }
    }
    
    class Child extends Parent {
        String message = "child";
        void say() {
            System.out.println(message);
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent yo = new Child();
            yo.say();
        }
    }
    
    1. parent
    2. child

  16. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        static void say() {
        }
    }
    
    class Child extends Parent {
        void say() {
        }
    }
    
    1. Yes.
    2. No.

  17. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() {
        }
    }
    
    class Child extends Parent {
        static void say() {
        }
    }
    
    1. Yes.
    2. No.

  18. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() throws Exception {
        }
    }
    
    class Child extends Parent {
        void say() {
        }
    }
    
    1. Yes.
    2. No.

  19. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() throws Exception {
        }
    }
    
    class Child extends Parent {
        void say() throws Exception {
        }
    }
    
    1. Yes.
    2. No.

  20. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    import java.io.*;
    
    class Parent {
        void say() throws Exception {
        }
    }
    
    class Child extends Parent {
        void say() throws IOException, FileNotFoundException {
        }
    }
    
    1. Yes.
    2. No.

  21. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() throws Exception {
        }
    }
    
    class Child extends Parent {
        void say() throws RuntimeException {
        }
    }
    
    1. Yes.
    2. No.

  22. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() throws PovertyException {
        }
    }
    
    class Child extends Parent {
        void say() throws NoFoodException {
        }
    }
    
    class PovertyException extends Exception {
    }
    
    class NoFoodException extends PovertyException {
    }
    
    1. Yes.
    2. No.

  23. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() throws PovertyException {
        }
    }
    
    class Child extends Parent {
        void say() throws NoFoodException, IOException {
        }
    }
    
    class PovertyException extends Exception {
    }
    
    class NoFoodException extends PovertyException {
    }
    
    1. Yes.
    2. No.

  24. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() throws NoFoodException {
        }
    }
    
    class Child extends Parent {
        void say() throws PovertyException {
        }
    }
    
    class PovertyException extends Exception {
    }
    
    class NoFoodException extends PovertyException {
    }
    
    1. Yes.
    2. No.

  25. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() throws Exception {
        }
    }
    
    class Child extends Parent {
        void say() throws ArrayIndexOutOfBoundsException,
        ClassCastException, NullPointerException {
        }
    }
    
    1. Yes.
    2. No.

  26. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() {
        }
    }
    
    class Child extends Parent {
        void say() throws ArrayIndexOutOfBoundsException,
        ClassCastException, NullPointerException {
        }
    }
    
    1. Yes.
    2. No.

  27. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        <T> void say() {
        }
    }
    
    class Child extends Parent {
        void say() {
        }
    }
    
    1. Yes.
    2. No.

  28. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() {
        }
    }
    
    class Child extends Parent {
        <T> void say() {
        }
    }
    
    1. Yes.
    2. No.

  29. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say(Number number) {
        }
    }
    
    class Child extends Parent {
        <T extends Number> void say(T number) {
        }
    }
    
    1. Yes.
    2. No.

  30. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        Number get() {
            return 1;
        }
    }
    
    class Child extends Parent {
        Integer get() {
            return 2;
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent yo = new Child();
            System.out.println(yo.get());
        }
    }
    
    1. 1
    2. 2
    3. Compilation fails.

  31. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        Integer get() {
            return 1;
        }
    }
    
    class Child extends Parent {
        Number get() {
            return 2;
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent yo = new Child();
            System.out.println(yo.get());
        }
    }
    
    1. 1
    2. 2
    3. Compilation fails.

  32. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        Float get() {
            return 1.0f;
        }
    }
    
    class Child extends Parent {
        Integer get() {
            return 2;
        }
    }
    
    1. Yes.
    2. No.

  33. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        Double get() {
            return 1.0;
        }
    }
    
    class Child extends Parent {
        Integer get() {
            return 2;
        }
    }
    
    1. Yes.
    2. No.

  34. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        Integer get() {
            return 1;
        }
    }
    
    class Child extends Parent {
        Double get() {
            return 2.0;
        }
    }
    
    1. Yes.
    2. No.

  35. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        void show(Parent parent) {
            System.out.println("parent");
        }
    }
    
    class Child extends Parent {
        void show(Child child) {
            System.out.println("child");
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent parent = new Parent();
            Child child = new Child();
            child.show(child);
    	}
    }
    
    1. parent
    2. child
    3. Compilation fails.

  36. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        void show(Parent parent) {
            System.out.println("parent");
        }
    }
    
    class Child extends Parent {
        void show(Child child) {
            System.out.println("child");
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent parent = new Parent();
            Child child = new Child();
            child.show(parent);
    	}
    }
    
    1. parent
    2. child
    3. Compilation fails.

  37. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        void show(Parent parent) {
            System.out.println("parent");
        }
    }
    
    class Child extends Parent {
        void show(Child child) {
            System.out.println("child");
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent parent = new Child();
            Child child = new Child();
            child.show(parent);
    	}
    }
    
    1. parent
    2. child
    3. Compilation fails.

  38. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        static Integer number;
    }
    
    class Child extends Parent {
        Integer number;
    }
    
    1. Yes.
    2. No.

  39. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        Integer number;
    }
    
    class Child extends Parent {
        static Integer number;
    }
    
    1. Yes.
    2. No.

  40. These classes are defined in the same file and compilation succeeds. What is the output? (1 correct answer)
    class Parent {
        Integer a = 1;
        static Integer b = 2;
    }
    
    class Child extends Parent {
        static Integer a = 41;
        Integer b = 42;
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent parent = new Parent();
            Child child = new Child();
            Parent yo = new Child();
            System.out.format("%d %d %d %d %d %d ",
            parent.a,
            parent.b,
            child.a,
            child.b,
            yo.a,
            yo.b);
        }
    }
    
    1. 1  2  41  42  1  2
    2. 1  2  41  42  41  42
    3. 1  2  41  42  1  42
    4. 1  2  41  42  41  2

  41. These classes are defined in the same file and compilation succeeds. What is the output? (1 correct answer)
    class Parent {
        final Integer a = 1;
        final static Integer b = 2;
    }
    
    class Child extends Parent {
        final static Integer a = 41;
        final Integer b = 42;
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent parent = new Parent();
            Child child = new Child();
            Parent yo = new Child();
            System.out.format("%d %d %d %d %d %d ",
            parent.a,
            parent.b,
            child.a,
            child.b,
            yo.a,
            yo.b);
        }
    }
    
    1. 1  2  41  42  1  2
    2. 1  2  41  42  41  42
    3. 1  2  41  42  1  42
    4. 1  2  41  42  41  2

  42. These classes are defined in the same file. What is the output? (1 correct answer)
    class Grandparent {
        String name = "granparent";
        void act() {
            System.out.println(name);
        }
    }
    
    class Parent extends Grandparent {
        String name = "parent";
    }
    
    class Child extends Parent {
        String name = "child";
        void act() {
            System.out.println(name);
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Grandparent yo = new Child();
            yo.act();
        }
    }
    
    1. grandparent
    2. parent
    3. child
    4. Compilation fails

  43. These classes are defined in the same file. What is the output? (1 correct answer)
    class Grandparent {
        String name = "granparent";
        void act() {
            System.out.println(name);
        }
    }
    
    class Parent extends Grandparent {
        String name = "parent";
    }
    
    class Child extends Parent {
        String name = "child";
        void act() {
            System.out.println(name);
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent yo = new Child();
            yo.act();
        }
    }
    
    1. grandparent
    2. parent
    3. child
    4. Compilation fails

  44. These classes are defined in the same file. What is the output? (1 correct answer)
    class Grandparent {
        String name = "granparent";
        void act() {
            System.out.println(name);
        }
    }
    
    class Parent extends Grandparent {
        String name = "parent";
    }
    
    class Child extends Parent {
        String name = "child";
        void act() {
            System.out.println(name);
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent yo = new Parent();
            yo.act();
        }
    }
    
    1. grandparent
    2. parent
    3. child
    4. Compilation fails

  45. These classes are defined in the same file. What is the output? (1 correct answer)
    class Grandparent {
        String name = "granparent";
        void act() {
            System.out.println(name);
        }
    }
    
    class Parent extends Grandparent {
        String name = "parent";
    }
    
    class Child extends Parent {
        String name = "child";
        void act() {
            System.out.println(name);
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Grandparent yo = new Parent();
            yo.act();
        }
    }
    
    1. grandparent
    2. parent
    3. child
    4. Compilation fails

  46. Will this code compile successfully? (1 correct answer)
    class Overload {
        public void method() {
        }
        static void method() {
        }
    }
    
    1. Yes.
    2. No.

  47. Will this code compile successfully? (1 correct answer)
    class Overload {
        public void method() {
        }
        static void method() throws Exception {
        }
    }
    
    1. Yes.
    2. No.

  48. Will this code compile successfully? (1 correct answer)
    class Overload {
        public void method() {
        }
        static Object method() {
            return null;
        }
    }
    
    1. Yes.
    2. No.

  49. Will this code compile successfully? (1 correct answer)
    import java.util.List;
    class Overload {
        public void method(List<String> names) {
        }
        private static Object method(String... names) {
            return null;
        }
    }
    
    1. Yes.
    2. No.

  50. Will this code compile successfully? (1 correct answer)
    class Overload {
        public void method(String[] names) {
        }
        final Object method(String... names) {
            return null;
        }
    }
    
    1. Yes.
    2. No.

  51. Will this code compile successfully? (1 correct answer)
    class Overload {
        public void method(String[] names) {
        }
        final Object method(String name, String... names) {
            return null;
        }
    }
    
    1. Yes.
    2. No.

  52. Will this code compile successfully? (1 correct answer)
    class Overload {
        public void method(String name, String[] names) {
        }
        final Object method(String... names) {
            return null;
        }
    }
    
    1. Yes.
    2. No.

  53. Will this code compile successfully? (1 correct answer)
    class Overload {
        public void method(String name, String[] names) {
        }
        final Object method(String name, String... names) {
            return null;
        }
    }
    
    1. Yes.
    2. No.

  54. Will this code compile successfully? (1 correct answer)
    class Overload {
        public void method(String name, String[] names) {
        }
        final Object method(String... names, String name) {
            return null;
        }
    }
    
    1. Yes.
    2. No.

  55. Will this code compile successfully? (1 correct answer)
    import java.util.*;
    class Overload {
        void method(NavigableSet<Integer> set) {
        }
        void method(NavigableSet<String> set) {
        }
    }
    
    1. Yes.
    2. No.

  56. Will this code compile successfully? (1 correct answer)
    import java.util.*;
    class Overload {
        void method(SortedSet<String> set) {
        }
        void method(NavigableSet<String> set) {
        }
    }
    
    1. Yes.
    2. No.

  57. Will this code compile successfully? (1 correct answer)
    import java.util.*;
    class Overload {
        void method(Set<String>... set) {
        }
        void method(Set<String> set) {
        }
        void method(NavigableSet<String> set) {
        }
    }
    
    1. Yes.
    2. No.

  58. Will this code compile successfully? (1 correct answer)
    import java.util.NavigableSet;
    
    interface Interface {
        void method(NavigableSet<String> set) throws Exception;
    }
    
    public class Overload implements Interface {
        void method() {
        }
        void method(NavigableSet<String> set) throws Exception {
        }
    }
    
    1. Yes.
    2. No.

  59. These classes are defined in the same file. What is the output of this code? (1 correct answer)
    class Parent {
        int value;
        void validate() {
            value = value + 10;
        }
    }
    
    class Child extends Parent {
        void validate() {
            super.validate();
            value = value - 2;
        }
    }
    
    public class Run {
        public static void main(String[] args) {
            Child child = new Child();
            child.validate();
            System.out.println(child.value);
        }
    }
    
    1. It prints “8″.
    2. It prints “-2″.
    3. None of the above.

  60. These classes are defined in the same file. What is the output of this code? (1 correct answer)
    class Parent {
        int value;
        void validate() {
            value = value + 10;
        }
    }
    
    class Child extends Parent {
        int value;
        void validate() {
            super.validate();
            value = value - 2;
        }
    }
    
    public class Run {
        public static void main(String[] args) {
            Child child = new Child();
            child.validate();
            System.out.println(child.value);
        }
    }
    
    1. It prints “8″.
    2. It prints “-2″.
    3. None of the above.

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

Answers

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

An index of all the SCJP Mock exams may be found here.

SCJP Mock exam for new Java 6 features

13 September 2008

Questions 1-30 are about Console, 31-50 about NavigableSet and 51-80 about NavigableMap.

  1. Console is a class. (1 correct answer)
    1. true
    2. false
  2. Console belongs to the java.io package. (1 correct answer)
    1. true
    2. false
  3. All the following methods are defined in Console.
        ⇒ readLine()
        ⇒ readPassword()
        ⇒ flush()
        ⇒ reader()
        ⇒ writer()
        ⇒ printf()
        ⇒ format()

    1. True.
    2. False.
  4. Consider these methods.
        ⇒ readLine()
        ⇒ readPassword()
        ⇒ flush()
        ⇒ reader()
        ⇒ writer()
        ⇒ printf()
        ⇒ format()
    How many of them are static? (1 correct answer)

    1. Two.
    2. Four.
    3. All.
    4. None.
  5. Consider these methods.
        ⇒ readLine()
        ⇒ readPassword()
        ⇒ flush()
        ⇒ reader()
        ⇒ writer()
        ⇒ printf()
        ⇒ format()
    How many of them declare a checked exception? (1 correct answer)

    1. Two.
    2. Four.
    3. All.
    4. None.
  6. What’s the signature of the method readPassword()? (1 correct answer)
    1. public char[] readPassword()
    2. public byte[] readPassword()
    3. public String readPassword()
  7. What’s the signature of the method format()? (1 correct answer)
    1. public void format(String, Object…)
    2. public String format(String, Object…)
    3. public Console format(String, Object…)
  8. There are 2 overloaded methods of Console with the name readLine. (1 correct answer)
    1. true
    2. false
  9. There are 2 overloaded methods of Console with the name getPassword. (1 correct answer)
    1. true
    2. false
  10. There’s maximum one object of type Console available per JVM. (1 correct answer)
    1. True.
    2. False.
  11. Will the following code compile successfully? (1 correct answer)
    import java.io.Console;
    
    public class MyConsole implements Console {
    
    }
    
    1. Yes.
    2. No.
  12. Will the following code compile successfully? (1 correct answer)
    import java.io.Console;
    
    public class MyConsole extends Console {
    
    }
    
    1. Yes.
    2. No.
  13. Will the following code compile successfully? (1 correct answer)
    import java.io.Console;
    
    public class Test {
        public static void main(String[] args) {
            Console console = new Console();
        }
    }
    
    1. Yes.
    2. No.
  14. Will the following code compile successfully? (1 correct answer)
    import java.io.Console;
    
    public class Test {
        public static void main(String[] args) {
            Console console = System.getConsole();
        }
    }
    
    1. Yes.
    2. No.
  15. Will the following code compile successfully? (1 correct answer)
    import java.io.Console;
    
    public class Test {
        public static void main(String[] args) {
            Console console = System.console();
        }
    }
    
    1. Yes.
    2. No.
  16. Will the following code compile successfully? (1 correct answer)
    import java.io.Console;
    
    public class Test {
        public static void main(String[] args) {
            Console console = System.console();
            console.println("Hello from your console!");
        }
    }
    
    1. Yes.
    2. No.
  17. Will the following code compile successfully? (1 correct answer)
    import java.io.Console;
    
    public class Test {
        public static void main(String[] args) {
            Console console = System.console();
            console.format("Hello from your console!");
        }
    }
    
    1. Yes.
    2. No.
  18. For this question only, consider that the underlying system does NOT provide a console. What happens when the following code is executed? (1 correct answer)
    import java.io.Console;
    
    public class Test {
        public static void main(String[] args) {
            Console console = System.console(); // 1
            console.format("Hello from your console!"); // 2
        }
    }
    
    1. A runtime exception is thrown at line 1
    2. A runtime exception is thrown at line 2
    3. No exception is thrown at runtime.
  19. What happens when this code is compiled and executed? (1 correct answer)
    import java.io.Console;
    
    public class Test {
    public static void main(String[] args) {
        Console console = System.console();
            console.readLine("Please enter your name: ");
            console.format(console.toString());
        }
    }
    }
    
    1. Compilation fails.
    2. An exception is thrown at runtime.
    3. The console prints exactly what the user typed before pressing ENTER.
    4. None of the above.
  20. What happens when this code is compiled and executed? (1 correct answer)
    import java.io.Console;
    
    public class Test {
    public static void main(String[] args) {
        Console console = System.console();
        if (console != null) {
            console.writer().close();
            console.printf("Hello!");
        }
    }
    }
    
    1. Compilation fails.
    2. An exception is thrown at runtime.
    3. The console prints “Hello!”.
    4. None of the above.
  21. What happens when this code is compiled and executed? (1 correct answer)
    import java.io.Console;
    
    public class Test {
    public static void main(String[] args) {
        Console console = System.console();
        if (console != null) {
        String name = console.
        readLine("Please enter your name: ");
        console.format("Hello %s!", name);
        }
    }
    }
    
    1. Compilation fails.
    2. An exception is thrown at runtime.
    3. If the user enters “Nikos”, the console prints “Hello Nikos!”
  22. What happens when this code is compiled and executed? (1 correct answer)
    import java.io.Console;
    
    public class Test {
    public static void main(String[] args) {
        Console console = System.console();
        if (console != null) {
        String name = console.
        readLine("Please enter your name: ");
        console.format("Hello %z!", name);
        }
    }
    }
    
    1. Compilation fails.
    2. An exception is thrown at runtime.
    3. If the user enters “Nikos”, the console prints “Hello Nikos!”
  23. What happens when this code is compiled and executed? (1 correct answer)
    import java.io.Console;
    
    public class Test {
    public static void main(String[] args) {
        Console console = System.console();
        if (console != null) {
        console.reader().close();
        String name = console.
        readLine("Please enter your name: ");
        console.format("Hello %s!", name);
        }
    }
    }
    
    1. Compilation fails.
    2. An exception is thrown at runtime.
    3. If the user enters “Nikos”, the console prints “Hello Nikos!”
  24. What happens when this code is compiled and executed? (1 correct answer)
    import java.io.Console;
    import java.io.IOException;
    
    public class Test {
    public static void main(String[] args) {
        Console console = System.console();
        try {
            console.reader().close();
        } catch (IOException e) {
        }
        String name = console.
        readLine("Please enter your name: ");
        console.format("Hello %s!", name);
    }
    }
    
    1. Compilation fails.
    2. An exception is thrown at runtime.
    3. If the user enters “Nikos”, the console prints “Hello Nikos!”
  25. What happens when this code is compiled and executed? (1 correct answer)
    import java.io.Console;
    
    public class Test {
    public static void main(String[] args) {
        Console console = System.console();
        console.println("Hello!");
    }
    }
    
    1. Compilation fails.
    2. An exception is thrown at runtime.
    3. The console prints “Hello!”.
  26. What happens when this code is compiled and executed? (1 correct answer)
    import java.io.Console;
    
    public class Test {
    public static void main(String[] args) {
        Console console = System.console();
        console.writer().println("Hello!");
    }
    }
    
    1. Compilation fails.
    2. An exception is thrown at runtime.
    3. The console prints “Hello!”.
  27. What happens when this code is compiled and executed? (1 correct answer)
    import java.io.Console;
    
    public class Test {
    public static void main(String[] args) {
        Console console = System.console();
        console.writer().append("Hello ")
        .append("from ").append("console!");
        console.flush();
    }
    }
    
    1. Compilation fails.
    2. An exception is thrown at runtime.
    3. The console prints “Hello from console!”.
  28. What happens when this code is compiled and executed? (1 correct answer)
    import java.io.Console;
    
    public class Test {
    public static void main(String[] args) {
        Console console = System.console();
        console.format("Hello!", null);
    }
    }
    
    1. Compilation fails.
    2. An exception is thrown at runtime.
    3. The console prints “Hello!”.
  29. What happens when this code is compiled and executed? (1 correct answer)
    import java.io.Console;
    
    public class Test {
    public static void main(String[] args) {
        Console console = System.console();
        console.format(null);
    }
    }
    
    1. Compilation fails.
    2. An exception is thrown at runtime.
    3. The console prints “null”.
  30. What happens when this code is compiled and executed? (1 correct answer)
    import java.io.Console;
    
    public class Test {
    public static void main(String[] args) {
        Console console = System.console();
        console.format("Hello ").printf("from ")
        .format("console!");
    }
    }
    
    1. Compilation fails.
    2. An exception is thrown at runtime.
    3. The console prints “Hello from console!”.
  31. NavigableSet is an interface. (1 correct answer)
    1. true
    2. false
  32. NavigableSet extends SortedSet. (1 correct answer)
    1. true
    2. false
  33. In Java 1.5 TreeSet implements SortedSet, whereas in Java 1.6 TreeSet implements NavigableSet. (1 correct answer)
    1. true
    2. false
  34. All these methods belong to NavigableSet. (1 correct answer)
        lower()
        higher()
        floor()
        ceiling()
        pollFirst()
        pollLast()

    1. true
    2. false
  35. How many of the following methods declare a checked exception? (1 correct answer)
        lower()
        higher()
        floor()
        ceiling()
        pollFirst()
        pollLast()

    1. None.
    2. Two.
    3. Four.
    4. All.
  36. How many of the following methods may throw an exception at runtime? (1 correct answer)
        lower()
        higher()
        floor()
        ceiling()
        pollFirst()
        pollLast()

    1. None.
    2. Two.
    3. Four.
    4. All.
  37. What’s the signature of the method lower()? (1 correct answer)
    1. E lower(E)
    2. boolean lower(E)
    3. E lower(NavigableSet<E>)
  38. What’s the signature of the method pollFirst()? (1 correct answer)
    1. E pollFirst()
    2. E pollFirst(E)
    3. E pollFirst(NavigableSet<E>)
  39. In the NavigableSet interface there are 2 overloaded methods with the name subSet. (1 correct answer)
    1. true
    2. false
  40. In the NavigableSet interface there are 2 overloaded methods with the name headSet. (1 correct answer)
    1. true
    2. false
  41. 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. It prints “null -12 -12 24″.
    2. It prints “-12 -12 24 24″.
  42. 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. It prints “null -12 -12 24″.
    2. It prints “-12 -12 24 24″.
  43. 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. It prints “24 24 null null”.
    2. It prints “-12 24 24 null”.
  44. 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. It prints “24 24 null null”.
    2. It prints “-12 24 24 null”.
  45. 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. It prints “-12 11 0 100″.
    2. It prints “-12 11 0 145″.
    3. It prints “-28 24 0 100″.
    4. It prints “-28 24 0 145″.
  46. 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″.
  47. 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″.
  48. 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″.
  49. 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″.
  50. 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.
  51. NavigableMap IS-A Map. (1 correct answer)
    1. true
    2. false
  52. NavigableMap IS-A SortedMap. (1 correct answer)
    1. true
    2. false
  53. In Java 1.5 TreeMap implements SortedMap, whereas in Java 1.6 TreeMap implements NavigableMap. (1 correct answer)
    1. true
    2. false
  54. All these methods belong to NavigableMap. (1 correct answer)
        ⇒ lowerKey()
        ⇒ higherKey()
        ⇒ floorKey()
        ⇒ ceilingKey()

    1. true
    2. false
  55. All these methods belong to NavigableMap. (1 correct answer)
        ⇒ lowerEntry()
        ⇒ higherEntry()
        ⇒ floorEntry()
        ⇒ ceilingEntry()

    1. true
    2. false
  56. All these methods belong to NavigableMap. (1 correct answer)
        ⇒ pollFirstEntry()
        ⇒ pollLastEntry()
        ⇒ firstEntry()
        ⇒ lastEntry()

    1. true
    2. false
  57. All these methods belong to NavigableMap. (1 correct answer)
        ⇒ pollFirstKey()
        ⇒ pollLastKey()
        ⇒ firstKey()
        ⇒ lastKey()

    1. true
    2. false
  58. Assume NavigableMap<K,V>. What’s the signature of the method floorEntry()? (1 correct answer)
    1. K floorEntry(K)
    2. V floorEntry(K)
    3. Map.Entry<K,V> floorEntry(K)
  59. Assume NavigableMap<K,V>. What’s the signature of the method higherKey()? (1 correct answer)
    1. K higherKey(K)
    2. Map.Entry<K,V> higherKey(K)
    3. K higherKey(NavigableMap<K,V>)
  60. Assume NavigableMap<K,V>. What’s the signature of the method pollFirstEntry()? (1 correct answer)
    1. Map.Entry<K,V> pollFirstEntry()
    2. Map.Entry<K,V> pollFirstEntry(K)
    3. Map.Entry<K,V> pollFirstEntry(NavigableMap<K,V>)
  61. How many of these methods of NavigableMap may throw an exception at runtime? (1 correct answer)
        ⇒ pollFirstEntry()
        ⇒ pollLastEntry()
        ⇒ firstEntry()
        ⇒ lastEntry()
        ⇒ firstKey()
        ⇒ lastKey()

    1. None.
    2. Two.
    3. Four.
    4. All.
  62. How many of these methods of NavigableMap declare a checked exception? (1 correct answer)
        ⇒ pollFirstEntry()
        ⇒ pollLastEntry()
        ⇒ firstEntry()
        ⇒ lastEntry()
        ⇒ firstKey()
        ⇒ lastKey()

    1. None.
    2. Two.
    3. Four.
    4. All.
  63. In NavigableMap there are 2 overloaded methods with the name subMap. (1 correct answer)
    1. true
    2. false
  64. In NavigableMap there are 2 overloaded methods with the name headMap. (1 correct answer)
    1. true
    2. false
  65. 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″.
  66. 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″.
  67. 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”.
  68. 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”.
  69. 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”.
  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("%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”.
  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("%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”.
  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("%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”.
  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(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″.
  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.lastEntry();
        System.out.println(map.size());
    }
    
    1. An exception is thrown at runtime.
    2. Compilation fails.
    3. It prints “0″.
  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.pollFirstEntry();
        System.out.println(map.size());
    }
    
    1. An exception is thrown at runtime.
    2. Compilation fails.
    3. It prints “0″.
  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.firstKey();
        System.out.println(map.size());
    }
    
    1. An exception is thrown at runtime.
    2. Compilation fails.
    3. It prints “0″.
  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(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.
  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(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.
  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.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.
  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.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.

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

Answers

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

References

A friendly community of people preparing for the Sun Certified Java Programmer certification can be found at the JavaRanch SCJP forum.

You may contact me for any suggestion.