Archive for September, 2008

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.

Wicket 8: Comfortable instance variables

12 September 2008

This is part 8 of a series about Wicket.

In JSP/Servlets you should not use instance variables; in Wicket it’s a comfort.

Let’s consider this servlet.

public class Servlet1 extends HttpServlet {
   private Integer number = 0;
   public void doGet(HttpServletRequest request,
      HttpServletResponse response)
      throws ServletException, IOException {
      number = number + 1;
      response.getWriter().
      println("Instance variable: " + number);
   }
}

If you open two different browsers and hit the refresh button several times, you’ll see that the number is incremented with every request. This happens because a servlet gets initialized once and serves the various requests with different threads.

Let’s see this servlet as well:

public class Servlet1 extends HttpServlet {
   private Integer number = new Random().nextInt();
   public void doGet(HttpServletRequest request,
      HttpServletResponse response)
      throws ServletException, IOException {
      response.getWriter().
      println("Instance variable: " + number);
   }
}

Similarly, in this second servlet the number shown is always the same as if the state was static and final.

Fortunately in Wicket every WebPage is a plain Java object.

For example consider Page.html

<html>
   <body>
      <h3 wicket:id="number"></h3>
   </body>
</html>
</pre>

and Page.java

public class Page extends WebPage {
   private Integer number = 0;
   public Page() {
      number = number + 1;
      add(new Label("number", "Instance variable: " + number));
   }
}

The result is what we really expect. How about this.

public class Page extends WebPage {
   private Integer number = new Random().nextInt();
   public Page() {
      add(new Label("number", "Instance variable: " + number));
   }
}

Just deploy it and hit refresh a few times: A new number is shown every time!

Review

Instance variables of a web page are a great convenience, in contrast to what happens with a servlet.

WebPage objects are plain Java objects.

SCJP 6 Mock exam for Threads

8 September 2008

Make sure to study the API of Runnable, Thread and Object before moving on.

  1. Runnable is an interface. (1 correct answer)
    1. true
    2. false
  2. Runnable belongs to the java.lang package. (1 correct answer)
    1. true
    2. false
  3. How many methods does the Runnable interface define? (1 correct answer)
    1. One: run()
    2. Two: run() and start()
    3. Three: run(), start() and join()
  4. Runnable defines a method run. What’s the signature? (1 correct answer)
    1. public void run()
    2. public void run() throws InterruptedException
    3. public void run(Runnable r)
    4. public void run(Runnable r) throws InterruptedException
  5. Class Thread implements the Runnable interface. (1 correct answer)
    1. true
    2. false
  6. Consider these methods.
          ⇒ run()
          ⇒ start()
          ⇒ join()
          ⇒ sleep()
          ⇒ yield()
          ⇒ currentThread()
    How many of them are defined in the Thread class? (1 correct answer)

    1. One.
    2. Two.
    3. Three.
    4. All.
  7. Consider these methods.
          ⇒ run()
          ⇒ start()
          ⇒ join()
          ⇒ sleep()
          ⇒ yield()
          ⇒ currentThread()
    How many of them are static? (1 correct answer)

    1. One.
    2. Two.
    3. Three.
    4. All.
  8. Consider these methods.
          ⇒ run()
          ⇒ start()
          ⇒ join()
          ⇒ sleep()
          ⇒ yield()
          ⇒ currentThread()
    How many of them declare a checked exception? (1 correct answer)

    1. One.
    2. Two.
    3. Three.
    4. All.
  9. What’s the signature of the method start()? (1 correct answer)
    1. public void start()
    2. public void start() throws InterruptedException
    3. public void start() throws IllegalThreadStateException
  10. What’s the signature of the method yield()? (1 correct answer)
    1. public void yield()
    2. public void yield() throws InterruptedException
    3. public static void yield()
    4. public static void yield() throws InterruptedException
  11. There are 2 overloaded methods of Thread with the name sleep. (1 correct answer)
    1. true
    2. false
  12. There are 3 overloaded methods of Thread with the name join. (1 correct answer)
    1. true
    2. false
  13. The Thread class has three public static and final integer fields: MIN_PRIORITY, NORM_PRIORITY, MAX_PRIORITY. (1 correct answer)
    1. true
    2. false
  14. Consider these methods.
          ⇒ wait()
          ⇒ notify()
          ⇒ notifyAll()
    How many of them are defined in the Object class? (1 correct answer)

    1. One.
    2. All.
    3. None.
  15. Consider these methods.
          ⇒ wait()
          ⇒ notify()
          ⇒ notifyAll()
    How many of them are static? (1 correct answer)

    1. One.
    2. All.
    3. None.
  16. Consider these methods.
          ⇒ wait()
          ⇒ notify()
          ⇒ notifyAll()
    How many of them declare a checked exception? (1 correct answer)

    1. One.
    2. All.
    3. None.
  17. There are 3 overloaded methods of Object with the name wait. (1 correct answer)
    1. true
    2. false
  18. Will this code compile successfully? (1 correct answer)
    class Test extends Runnable {
    
    }
    
    1. Yes.
    2. No.
  19. Will this code compile successfully? (1 correct answer)
    class Test implements Runnable {
    
    }
    
    1. Yes.
    2. No.
  20. Will this code compile successfully? (1 correct answer)
    class Test implements Runnable {
        void run() {
        }
    }
    
    1. Yes.
    2. No.
  21. Will this code compile successfully? (1 correct answer)
    class Test implements Runnable {
        public void run() {
        }
        void run(Runnable r) {
        }
        void run(String... s) {
        }
    }
    
    1. Yes.
    2. No.
  22. Will this code compile successfully? (1 correct answer)
    class Test extends Thread {
    
    }
    
    1. Yes.
    2. No.
  23. Will this code compile successfully? (1 correct answer)
    class Test extends Thread {
        public Test() {
            super();
        }
    }
    
    1. Yes.
    2. No.
  24. Will this code compile successfully? (1 correct answer)
    class Test extends Thread {
        public Test() {
            super("Nikos");
        }
    }
    
    1. Yes.
    2. No.
  25. Will this code compile successfully? (1 correct answer)
    class Test extends Thread {
        public Test(String name) {
            super(name);
        }
    }
    
    1. Yes.
    2. No.
  26. Will this code compile successfully? (1 correct answer)
    class Test extends Thread {
        public Test(Runnable job, String name) {
            super(job);
        }
    }
    
    1. Yes.
    2. No.
  27. Will this code compile successfully? (1 correct answer)
    class Test extends Thread {
        public Test(Runnable job, String name) {
            super(job, name);
        }
    
    }
    
    1. Yes.
    2. No.
  28. Will this code compile successfully? (1 correct answer)
    import java.util.NavigableSet;
    class Test extends Thread {
        public void run(NavigableSet<Thread> set) {
        }
    }
    
    1. Yes.
    2. No.
  29. Will this code compile successfully? (1 correct answer)
    class Test extends Thread {
        public void run() throws InterruptedException {
        }
    }
    
    1. Yes.
    2. No.
  30. Will this code compile successfully? (1 correct answer)
    class Test extends Thread {
        public void join() throws InterruptedException {
        }
    }
    
    1. Yes.
    2. No.
  31. What happens when this code gets compiled and executed? (1 correct answer)
    public class Test extends Thread {
        public void run() {
            System.out.println("run 1");
        }
        public void run(String s) {
            System.out.println("run 2");
        }
        public static void main(String[] args) {
            new Test().start();
        }
    }
    
    1. It prints “run 1″.
    2. It prints “run 2″.
    3. Compilation fails.
  32. What happens when this code gets compiled and executed? (1 correct answer)
    public class Test extends Thread {
        public void run() {
            System.out.println("run 1");
        }
        public void run(String s) {
            System.out.println("run 2");
        }
        public static void main(String[] args) {
            new Test().start("hi");
        }
    }
    
    1. It prints “run 1″.
    2. It prints “run 2″.
    3. Compilation fails.
  33. What happens when this code gets compiled and executed? (1 correct answer)
    public class Test {
        public static void main(String[] args) {
            String name = Thread.currentThread().getName();
            System.out.println(name);
        }
    }
    
    1. It prints nothing.
    2. It prints “main”.
    3. It prints “Thread-0″.
    4. Compilation fails because an InterruptedException is not handled.
  34. What happens when this code gets compiled and executed? (1 correct answer)
    public class Test extends Thread {
        public static void main(String[] args) {
            String name = Thread.currentThread().getName();
            System.out.println(name);
        }
    }
    
    1. It prints nothing.
    2. It prints “main”.
    3. It prints “Thread-0″.
    4. Compilation fails because an InterruptedException is not handled.
    5. Compilation fails because Test does not implement the run() method.
  35. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            new Thread().run();
        }
    }
    
    1. Compilation fails.
    2. It compiles and runs fine.
    3. An IllegalThreadStateException is thrown at runtime.
  36. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            Thread thread = new Thread();
            thread.run();
            thread.run();
        }
    }
    
    1. Compilation fails.
    2. It compiles and runs fine.
    3. An IllegalThreadStateException is thrown at runtime.
  37. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            new Thread().start();
        }
    }
    
    1. Compilation fails.
    2. It compiles and runs fine.
    3. An IllegalThreadStateException is thrown at runtime.
  38. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            Thread thread = new Thread();
            thread.start();
            thread.start();
        }
    }
    
    1. Compilation fails.
    2. It compiles and runs fine.
    3. An IllegalThreadStateException is thrown at runtime.
  39. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            Thread thread = new Thread();
            thread.start();
            thread = new Thread();
            thread.start();
        }
    }
    
    1. Compilation fails.
    2. It compiles and runs fine.
    3. An IllegalThreadStateException is thrown at runtime.
  40. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            Thread thread = new Thread();
            thread.start();
            Thread.yield();
            thread.start();
        }
    }
    
    1. Compilation fails.
    2. It compiles and runs fine.
    3. An IllegalThreadStateException is thrown at runtime.
  41. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            Thread thread = new Thread();
            thread.run();
            thread.start();
        }
    }
    
    1. Compilation fails.
    2. It compiles and runs fine.
    3. An IllegalThreadStateException is thrown at runtime.
  42. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            System.out.print("A ");
            new Thread(new Thread(new Thread())).start();
            System.out.println("B");
        }
    }
    
    1. It prints “A B”.
    2. Compilation fails.
    3. It prints “A ” and an exception is thrown.
  43. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            System.out.print("A ");
            new Thread(new Thread(new Thread())).run();
            System.out.println("B");
        }
    }
    
    1. It prints “A B”.
    2. Compilation fails.
    3. It prints “A ” and an exception is thrown.
  44. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
    public static void main(String[] args) {
        Thread thread = new Thread() {
            public void run() {
                System.out.println("Hello!");
            }
        };
        thread.start();
    }
    }
    
    1. It prints “Hello!”.
    2. Compilation fails.
  45. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
    public static void main(String[] args) {
        new Thread() {
            public void run() {
                System.out.println("Hello!");
            }
        }.start();
    }
    }
    
    1. It prints “Hello!”.
    2. Compilation fails.
  46. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
    public static void main(String[] args) {
        new Runnable() {
            public void run() {
                System.out.println("Hello!");
            }
        }.start();
    }
    }
    
    1. It prints “Hello!”.
    2. Compilation fails.
  47. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            public void run() {
                System.out.println("Hello!");
            }
        }).start();
    }
    }
    
    1. It prints “Hello!”.
    2. Compilation fails.
  48. These classes are defined in the same file. What is the output? (1 correct answer)
    class Job implements Runnable {
        public void run() {
            System.out.println("Working...");
        }
    }
    public class My {
        public static void main(String[] args) {
            Thread thread1 = new Thread(new Job());
            Thread thread2 = new Thread(new Job());
            thread1.start();
            thread2.start();
        }
    }
    
    1. Compilation fails.
    2. It prints “Working…” three times.
    3. It prints “Working…” one time and then an exception is thrown.
  49. These classes are defined in the same file. What is the output? (1 correct answer)
    class Job implements Runnable {
        public void run() {
            System.out.println("Working...");
        }
    }
    public class My {
        public static void main(String[] args) {
            Job job = new Job();
            Thread thread1 = new Thread(job);
            Thread thread2 = new Thread(job);
            thread1.start();
            thread2.start();
        }
    }
    
    1. Compilation fails.
    2. It prints “Working…” three times.
    3. It prints “Working…” one time and then an exception is thrown.
  50. What is the output? (1 correct answer)
    public class Test extends Thread {
        public void run() {
            System.out.println(isAlive());
        }
        public static void main(String[] args) {
            Test test = new Test();
            System.out.println(test.isAlive());
            test.start();
        }
    }
    
    1. false false
    2. false true
    3. true false
    4. true true
  51. What is the output? (1 correct answer)
    public class Test extends Thread {
        public void run() {
            System.out.println(isAlive());
        }
        public static void main(String[] args) {
            Test test = new Test();
            System.out.println(test.isAlive());
            test.run();
        }
    }
    
    1. false false
    2. false true
    3. true false
    4. true true
  52. What is the output? (1 correct answer)
    public class Test extends Thread {
        public Test() {
            System.out.println(isAlive());
        }
        public static void main(String[] args) {
            new Test();
        }
    }
    
    1. false
    2. true
  53. What is the output? (1 correct answer)
    public class Test extends Thread {
        public Test() {
            System.out.println(isAlive());
        }
        public static void main(String[] args) {
            new Test().run();
        }
    }
    
    1. false
    2. true
  54. What is the output? (1 correct answer)
    public class Test extends Thread {
        public Test() {
            System.out.println(isAlive());
        }
        public static void main(String[] args) {
            new Test().start();
        }
    }
    
    1. false
    2. true
  55. Is this class thread-safe? In other words, is it carefully designed to protect its state from concurrent access? (1 correct answer)
    public class Account {
        private Integer number = 0;
        synchronized public void setNumber(Integer number) {
            this.number = number;
        }
        synchronized public Integer getNumber() {
            return number;
        }
    }
    
    1. Yes.
    2. No.
  56. Is this class thread-safe? In other words, is it carefully designed to protect its state from concurrent access? (1 correct answer)
    public class Account {
        private Integer number = 0;
        public void setNumber(Integer number) {
            synchronized (this) {
                this.number = number;
            }
        }
        synchronized public Integer getNumber() {
            return number;
        }
    }
    
    1. Yes.
    2. No.
  57. Is this class thread-safe? In other words, is it carefully designed to protect its state from concurrent access? (1 correct answer)
    public class Account {
        private Integer number = 0;
        public void setNumber(Integer number) {
            synchronized (this) {
                this.number = number;
            }
        }
        public Integer getNumber() {
            synchronized (this) {
                return number;
            }
        }
    }
    
    1. Yes.
    2. No.
  58. These classes are defined in a single file. There’s a value object Account that carefully protects it’s state from concurrent access, a Client class of type Thread that puts some money in an account, and a main method that simply starts two clients.
    class Account {
        private Integer number = 0;
        public synchronized void setNumber(Integer number) {
            this.number = number;
        }
        public synchronized Integer getNumber() {
            return number;
        }
    }
    
    class Client extends Thread {
        Account account;
        public Client(Account account) {
            this.account = account;
        }
        public void run() {
            for (int i = 1; i <= 1000; i++) {
                account.setNumber(account.getNumber() + 1);
    
            }
        }
    }
    
    public class Run {
        public static void main(String[] args) throws Exception {
            Account account = new Account();
            Client one = new Client(account);
            Client two = new Client(account);
            one.start();
            two.start();
            one.join();
            two.join();
            // here
        }
    }
    

    Just before the main method exits, the account’s number field is guaranteed to have value 2000.

    1. true
    2. false
  59. These classes are defined in a single file.
    class Account {
        private Integer number = 0;
        public synchronized void increase() {
            number++;
        }
        public synchronized Integer getNumber() {
            return number;
        }
    }
    
    class Client extends Thread {
        Account account;
        public Client(Account account) {
            this.account = account;
        }
        public void run() {
            for (int i = 1; i <= 1000; i++) {
                account.increase();
            }
        }
    }
    
    public class Run {
        public static void main(String[] args) throws Exception {
            Account account = new Account();
            Client one = new Client(account);
            Client two = new Client(account);
            one.start();
            two.start();
            one.join();
            two.join();
            // here
        }
    }
    

    Just before the main method exits, the account’s number field is guaranteed to have value 2000.

    1. true
    2. false
  60. These classes are defined in a single file.
    class Account {
        private Integer number = 0;
        public void increase() {
            number++;
        }
        public synchronized Integer getNumber() {
            return number;
        }
    }
    
    class Client extends Thread {
        Account account;
        public Client(Account account) {
            this.account = account;
        }
        public void run() {
            for (int i = 1; i <= 1000; i++) {
                account.increase();
            }
        }
    }
    
    public class Run {
        public static void main(String[] args) throws Exception {
            Account account = new Account();
            Client one = new Client(account);
            Client two = new Client(account);
            one.start();
            two.start();
            one.join();
            two.join();
            // here
        }
    }
    

    Just before the main method exits, the account’s number field is guaranteed to have value 2000.

    1. true
    2. false
  61. What happens when this code gets compiled and executed? (1 correct answer)
    public class Test  {
        public static void main(String[] args) {
            String message = "Hello!";
            message.wait(); // 1
            System.out.println(message);
        }
    }
    
    1. Compilation fails.
    2. It prints “Hello!”.
    3. An IllegalMonitorStateException is thrown at runtime.
    4. Nothing gets printed, as the main thread just waits at line 1.
  62. What happens when this code gets compiled and executed? (1 correct answer)
    public class Test  {
        public static void main(String[] args) {
            String message = "Hello!";
            try {
                message.wait();
            } catch (InterruptedException e) {
            }
            System.out.println(message);
        }
    }
    
    1. Compilation fails.
    2. It prints “Hello!”.
    3. An IllegalMonitorStateException is thrown at runtime.
    4. Nothing gets printed, as the main thread just waits at line 1.
  63. What happens when this code gets compiled and executed? (1 correct answer)
    public class Test  {
    public static void main(String[] args) {
        String message = "Hello!";
        synchronized (args) {
            try {
                message.wait();
            } catch (InterruptedException e) {
            }
        }
        System.out.println(message);
    }
    }
    
    1. Compilation fails.
    2. It prints “Hello!”.
    3. An IllegalMonitorStateException is thrown at runtime.
    4. Nothing gets printed, as the main thread just waits at line 1.
  64. What happens when this code gets compiled and executed? (1 correct answer)
    public class Test  {
    public static void main(String[] args) {
        String message = "Hello!";
        synchronized (message) {
            try {
                message.wait();
            } catch (InterruptedException e) {
            }
        }
        System.out.println(message);
    }
    }
    
    1. Compilation fails.
    2. It prints “Hello!”.
    3. An IllegalMonitorStateException is thrown at runtime.
    4. Nothing gets printed, as the main thread just waits at line 1.
  65. These classes are defined in the same file. What is the output? (1 correct answer)
    class Job extends Thread {
        private Integer number = 0;
        public void run() {
            for (int i = 1; i < 1000000; i++) {
                number++;
            }
        }
        public Integer getNumber() {
            return number;
        }
    }
    public class Test {
        public static void main(String[] args) {
            Job thread = new Job();
            thread.start();
            System.out.println(thread.getNumber());
        }
    }
    
    1. It prints 0.
    2. It prints 999999.
    3. The output is not guaranteed to be any of the above.
  66. These classes are defined in the same file. What is the output? (1 correct answer)
    class Job extends Thread {
        private Integer number = 0;
        public void run() {
            for (int i = 1; i < 1000000; i++) {
                number++;
            }
        }
        public Integer getNumber() {
            return number;
        }
    }
    public class Test {
        public static void main(String[] args)
        throws InterruptedException {
            Job thread = new Job();
            thread.start();
            synchronized (thread) {
                thread.wait();
            }
            System.out.println(thread.getNumber());
        }
    }
    
    1. It prints 0.
    2. It prints 999999.
    3. The output is not guaranteed to be any of the above.
  67. These classes are defined in the same file. What is the output? (1 correct answer)
    class Job extends Thread {
    private Integer number = 0;
        public void run() {
        synchronized (this) {
            for (int i = 1; i < 1000000; i++) {
                number++;
            }
            notify();
        }
        }
        public Integer getNumber() {
            return number;
        }
    }
    public class Test {
        public static void main(String[] args) throws Exception {
            Job thread = new Job();
            thread.start();
            synchronized (thread) {
    
                thread.wait();
            }
            System.out.println(thread.getNumber());
        }
    }
    
    1. It prints 0.
    2. It prints 999999.
    3. The output is not guaranteed to be any of the above.
  68. These classes are defined in the same file. What is the output? (1 correct answer)
    class Job extends Thread {
        private Integer number = 0;
        public void run() {
            for (int i = 1; i < 1000000; i++) {
                number++;
            }
        }
        public Integer getNumber() {
            return number;
        }
    }
    
    public class Test {
        public static void main(String[] args) throws Exception {
            Job thread = new Job();
            thread.start();
            thread.join();
            System.out.println(thread.getNumber());
        }
    }
    
    1. It prints 0.
    2. It prints 999999.
    3. The output is not guaranteed to be any of the above.
  69. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            System.out.print("A ");
            notifyAll();
            System.out.println("B");
        }
    }
    
    1. It prints “A B”.
    2. Compilation fails.
    3. It prints “A ” and an exception is thrown.
  70. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            System.out.print("A ");
            new Object().notifyAll();
            System.out.println("B");
        }
    }
    
    1. It prints “A B”.
    2. Compilation fails.
    3. It prints “A ” and an exception is thrown.
  71. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            System.out.print("A ");
            synchronized (new Object()) {
                new Object().notifyAll();
            }
            System.out.println("B");
        }
    }
    
    1. It prints “A B”.
    2. Compilation fails.
    3. It prints “A ” and an exception is thrown.
  72. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            System.out.print("A ");
            final Object test = new Object();
            synchronized (test) {
                test.notifyAll();
            }
            System.out.println("B");
        }
    }
    
    1. It prints “A B”.
    2. Compilation fails.
    3. It prints “A ” and an exception is thrown.

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

References

A friendly community of people preparing for SCJP can be found at the JavaRanch SCJP forum.

Please contact me for any suggestion.

Thanks.

Wicket 7: Two elements with the same value

6 September 2008

This is part 7 of a series about Wicket.

Let’s type something in a field, press submit and see what was typed in the same page.

Consider SamePage.html. First we need an input field to type in, accompanied with a submit button, surrounded by a form of course.

<form>
   <input type="text"/><input type="submit" value="OK"/>
</form>

We also need a span to display what we have just typed.

<form>
   <input type="text"/><input type="submit" value="OK"/>
</form>
<span style="color:coral"></span>

Let’s add wicket ids to <form>, <input> and <span>.

<form wicket:id="form">
   <input type="text" wicket:id="input"/><input type="submit" value="OK"/>
</form>
<span style="color:coral" wicket:id="output"></span>

So the form has id “form”, the input field “input” and the span “output”.

Here’s the complete SamePage.html

<html>
   <body>
      <form wicket:id="form">
         <input type="text" wicket:id="input"/><input type="submit" value="OK"/>
      </form>
      <span style="color:coral" wicket:id="output"></span>
   </body>
</html>

Now let’s build the associated Java class that IS-A WebPage. All the action happens in its constructor.

Here’s the form with id “form”.

final Form form = new Form("form");

Here’s the text field with id “input”.

final TextField input = new TextField("input");

And that’s the span (label) with id “output”.

final Label label = new Label("output");

Now, we follow the html markup: The form contains the input.

form.add(input);

The page contains the form,

add(form);

and the label.

add(label);

Now, to make input and label have the same value we simply provide a common Model.

final Model model = new Model();
final TextField input = new TextField("input", model);
final Label label = new Label("output", model);

This is the complete code of SamePage.java

public class SamePage extends WebPage {
   public SamePage() {
      final Model model = new Model();
      final Form form = new Form("form");
      final TextField input = new TextField("input", model);
      final Label label = new Label("output", model);
      form.add(input);
      add(form);
      add(label);
   }
}

Of course you may shorten the code by half with chaining, but this does not help debugging and maintenance.

public class SamePage extends WebPage {
   public SamePage() {
      final Model model = new Model();
      add(new Form("form").add(new TextField("input", model)));
      add(new Label("output", model));
   }
}

Let’s type something and submit.

A simple form

A simple form

The page refreshes as expected.

The span below the field repeats its value.

The span below the field repeats its value.

Review

  • Just provide the same Model to components that should have the same value.
  • You may take a look at TextField, Label and Form.

Wicket 6: Hit counter

5 September 2008

This is part 6 of a series about Wicket.

Here’s the simplest way to keep track of the hits of a page.

This is Hello.html

<html>
  <body>
    <h2 wicket:id="message"></h2>
  </body>
</html>

And this is Hello.java

public class Hello extends WebPage {
   public Hello() {
      add(new Label("message", "Hello!"));
   }
}

Every page is a real object of type WebPage. So we may use a static field.

public class Hello extends WebPage {
   private static Integer counter = 1;
   public Hello() {
      add(new Label("message", "hits: " + counter++));
   }
}

Deploy it and hit the refresh button several times!

Close the browser window and open a new one. Is the value of hits preserved? Why?

Review

A simple solution implemented using simple Java code.