Questions 1-30 are about Console, 31-50 about NavigableSet and 51-80 about NavigableMap.
- Console is a class. (1 correct answer)
- true
- false
- Console belongs to the java.io package. (1 correct answer)
- true
- false
- All the following methods are defined in Console.
⇒ readLine()
⇒ readPassword()
⇒ flush()
⇒ reader()
⇒ writer()
⇒ printf()
⇒ format()- True.
- False.
- Consider these methods.
⇒ readLine()
⇒ readPassword()
⇒ flush()
⇒ reader()
⇒ writer()
⇒ printf()
⇒ format()
How many of them are static? (1 correct answer)- Two.
- Four.
- All.
- None.
- Consider these methods.
⇒ readLine()
⇒ readPassword()
⇒ flush()
⇒ reader()
⇒ writer()
⇒ printf()
⇒ format()
How many of them declare a checked exception? (1 correct answer)- Two.
- Four.
- All.
- None.
- What’s the signature of the method readPassword()? (1 correct answer)
- public char[] readPassword()
- public byte[] readPassword()
- public String readPassword()
- What’s the signature of the method format()? (1 correct answer)
- public void format(String, Object…)
- public String format(String, Object…)
- public Console format(String, Object…)
- There are 2 overloaded methods of Console with the name readLine. (1 correct answer)
- true
- false
- There are 2 overloaded methods of Console with the name getPassword. (1 correct answer)
- true
- false
- There’s maximum one object of type Console available per JVM. (1 correct answer)
- True.
- False.
- Will the following code compile successfully? (1 correct answer)
import java.io.Console; public class MyConsole implements Console { }- Yes.
- No.
- Will the following code compile successfully? (1 correct answer)
import java.io.Console; public class MyConsole extends Console { }- Yes.
- No.
- 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(); } }- Yes.
- No.
- 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(); } }- Yes.
- No.
- 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(); } }- Yes.
- No.
- 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!"); } }- Yes.
- No.
- 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!"); } }- Yes.
- No.
- 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 } }- A runtime exception is thrown at line 1
- A runtime exception is thrown at line 2
- No exception is thrown at runtime.
- 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()); } } }- Compilation fails.
- An exception is thrown at runtime.
- The console prints exactly what the user typed before pressing ENTER.
- None of the above.
- 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!"); } } }- Compilation fails.
- An exception is thrown at runtime.
- The console prints “Hello!”.
- None of the above.
- 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); } } }- Compilation fails.
- An exception is thrown at runtime.
- If the user enters “Nikos”, the console prints “Hello Nikos!”
- 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); } } }- Compilation fails.
- An exception is thrown at runtime.
- If the user enters “Nikos”, the console prints “Hello Nikos!”
- 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); } } }- Compilation fails.
- An exception is thrown at runtime.
- If the user enters “Nikos”, the console prints “Hello Nikos!”
- 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); } }- Compilation fails.
- An exception is thrown at runtime.
- If the user enters “Nikos”, the console prints “Hello Nikos!”
- 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!"); } }- Compilation fails.
- An exception is thrown at runtime.
- The console prints “Hello!”.
- 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!"); } }- Compilation fails.
- An exception is thrown at runtime.
- The console prints “Hello!”.
- 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(); } }- Compilation fails.
- An exception is thrown at runtime.
- The console prints “Hello from console!”.
- 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); } }- Compilation fails.
- An exception is thrown at runtime.
- The console prints “Hello!”.
- 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); } }- Compilation fails.
- An exception is thrown at runtime.
- The console prints “null”.
- 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!"); } }- Compilation fails.
- An exception is thrown at runtime.
- The console prints “Hello from console!”.
- NavigableSet is an interface. (1 correct answer)
- true
- false
- NavigableSet extends SortedSet. (1 correct answer)
- true
- false
- In Java 1.5 TreeSet implements SortedSet, whereas in Java 1.6 TreeSet implements NavigableSet. (1 correct answer)
- true
- false
- All these methods belong to NavigableSet. (1 correct answer)
lower()
higher()
floor()
ceiling()
pollFirst()
pollLast()- true
- false
- How many of the following methods declare a checked exception? (1 correct answer)
lower()
higher()
floor()
ceiling()
pollFirst()
pollLast()- None.
- Two.
- Four.
- All.
- How many of the following methods may throw an exception at runtime? (1 correct answer)
lower()
higher()
floor()
ceiling()
pollFirst()
pollLast()- None.
- Two.
- Four.
- All.
- What’s the signature of the method lower()? (1 correct answer)
- E lower(E)
- boolean lower(E)
- E lower(NavigableSet<E>)
- What’s the signature of the method pollFirst()? (1 correct answer)
- E pollFirst()
- E pollFirst(E)
- E pollFirst(NavigableSet<E>)
- In the NavigableSet interface there are 2 overloaded methods with the name subSet. (1 correct answer)
- true
- false
- In the NavigableSet interface there are 2 overloaded methods with the name headSet. (1 correct answer)
- true
- false
- 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) ); }- It prints “null -12 -12 24″.
- It prints “-12 -12 24 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.floor(-12), set.floor(0), set.floor(24), set.floor(100) ); }- It prints “null -12 -12 24″.
- It prints “-12 -12 24 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.higher(-12), set.higher(0), set.higher(24), set.higher(100) ); }- It prints “24 24 null null”.
- It prints “-12 24 24 null”.
- 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) ); }- It prints “24 24 null null”.
- It prints “-12 24 24 null”.
- 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) ); }- It prints “-12 11 0 100″.
- It prints “-12 11 0 145″.
- It prints “-28 24 0 100″.
- It prints “-28 24 0 145″.
- 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()); }- An exception is thrown at runtime.
- Compilation fails.
- It prints “0″.
- 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()); }- An exception is thrown at runtime.
- Compilation fails.
- It prints “0″.
- 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()); }- An exception is thrown at runtime.
- Compilation fails.
- It prints “4″.
- It prints “2″.
- 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()); }- An exception is thrown at runtime.
- Compilation fails.
- It prints “4″.
- It prints “2″.
- 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()); } }- It prints “1 2 4 “.
- It prints “4 2 1 “.
- Compilation fails.
- NavigableMap IS-A Map. (1 correct answer)
- true
- false
- NavigableMap IS-A SortedMap. (1 correct answer)
- true
- false
- In Java 1.5 TreeMap implements SortedMap, whereas in Java 1.6 TreeMap implements NavigableMap. (1 correct answer)
- true
- false
- All these methods belong to NavigableMap. (1 correct answer)
⇒ lowerKey()
⇒ higherKey()
⇒ floorKey()
⇒ ceilingKey()- true
- false
- All these methods belong to NavigableMap. (1 correct answer)
⇒ lowerEntry()
⇒ higherEntry()
⇒ floorEntry()
⇒ ceilingEntry()- true
- false
- All these methods belong to NavigableMap. (1 correct answer)
⇒ pollFirstEntry()
⇒ pollLastEntry()
⇒ firstEntry()
⇒ lastEntry()- true
- false
- All these methods belong to NavigableMap. (1 correct answer)
⇒ pollFirstKey()
⇒ pollLastKey()
⇒ firstKey()
⇒ lastKey()- true
- false
- Assume NavigableMap<K,V>. What’s the signature of the method floorEntry()? (1 correct answer)
- K floorEntry(K)
- V floorEntry(K)
- Map.Entry<K,V> floorEntry(K)
- Assume NavigableMap<K,V>. What’s the signature of the method higherKey()? (1 correct answer)
- K higherKey(K)
- Map.Entry<K,V> higherKey(K)
- K higherKey(NavigableMap<K,V>)
- Assume NavigableMap<K,V>. What’s the signature of the method pollFirstEntry()? (1 correct answer)
- Map.Entry<K,V> pollFirstEntry()
- Map.Entry<K,V> pollFirstEntry(K)
- Map.Entry<K,V> pollFirstEntry(NavigableMap<K,V>)
- How many of these methods of NavigableMap may throw an exception at runtime? (1 correct answer)
⇒ pollFirstEntry()
⇒ pollLastEntry()
⇒ firstEntry()
⇒ lastEntry()
⇒ firstKey()
⇒ lastKey()- None.
- Two.
- Four.
- All.
- How many of these methods of NavigableMap declare a checked exception? (1 correct answer)
⇒ pollFirstEntry()
⇒ pollLastEntry()
⇒ firstEntry()
⇒ lastEntry()
⇒ firstKey()
⇒ lastKey()- None.
- Two.
- Four.
- All.
- In NavigableMap there are 2 overloaded methods with the name subMap. (1 correct answer)
- true
- false
- In NavigableMap there are 2 overloaded methods with the name headMap. (1 correct answer)
- true
- false
- 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) ); }- It prints “null -1 5 7″.
- It prints “null 5 5 7″.
- 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) ); }- It prints “null -1 5 7″.
- It prints “null 5 5 7″.
- 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) ); }- It prints “-1 7 7 null”.
- It prints “-1 5 7 null”.
- 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) ); }- It prints “-1 7 7 null”.
- It prints “-1 5 7 null”.
- 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) ); }- It prints “null -1=A 5=D 7=O”.
- It prints “null 5=D 5=D 7=O”.
- 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) ); }- It prints “null -1=A 5=D 7=O”.
- It prints “null 5=D 5=D 7=O”.
- 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) ); }- It prints “-1=A 7=O 7=O null”.
- It prints “-1=A 5=D 7=O null”.
- 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) ); }- It prints “-1=A 7=O 7=O null”.
- It prints “-1=A 5=D 7=O null”.
- 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) ); }- It prints “-1=2 5=a 34=J -1″.
- It prints “-1=a 5=a 34=J -1″.
- It prints “-1=2 5=a 34=J 5″.
- It prints “-1=a 5=a 34=J 5″.
- 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()); }- An exception is thrown at runtime.
- Compilation fails.
- It prints “0″.
- 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()); }- An exception is thrown at runtime.
- Compilation fails.
- It prints “0″.
- 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()); }- An exception is thrown at runtime.
- Compilation fails.
- It prints “0″.
- 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()); }- It prints “2″.
- It prints “3″.
- Compilation fails.
- An exception is thrown at runtime.
- 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()); }- It prints “2″.
- It prints “3″.
- Compilation fails.
- An exception is thrown at runtime.
- 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()); }- It prints “2 2″.
- It prints “3 2″.
- It prints “3 3″.
- It prints “4 2″.
- It prints “4 3″.
- Compilation fails.
- An exception is thrown at runtime.
- 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()); }- It prints “2 2″.
- It prints “3 2″.
- It prints “3 3″.
- It prints “4 2″.
- It prints “4 3″.
- Compilation fails.
- 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
- a
- a
- a
- d
- d
- a
- c
- a
- b
- a
- b
- b
- b
- b
- a
- b
- a
- a
- d
- c
- c
- b
- a
- c
- a
- c
- c
- c
- b
- c
- a
- a
- a
- a
- a
- c
- a
- a
- a
- a
- a
- b
- a
- b
- b
- c
- a
- b
- c
- b
- a
- a
- a
- a
- a
- a
- b
- c
- a
- a
- b
- a
- a
- a
- a
- b
- a
- b
- a
- b
- a
- b
- a
- c
- c
- a
- c
- a
- d
- 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.


