OCPJP

168
-80 about NavigableMap . 1. Console is a class. (1 correct answer) a. true b. false 2. Console belongs to the java.io package. (1 correct answer) a. true b. false 3. All the following methods are defined in Console. ⇒ readLine() ⇒ readPassword() ⇒ flush() ⇒ reader() ⇒ writer() ⇒ printf() ⇒ format() a. True. b. False. 4. Consider these methods. ⇒ readLine() ⇒ readPassword() ⇒ flush() ⇒ reader() ⇒ writer() ⇒ printf() ⇒ format() How many of them are static? (1 correct answer) a. Two. b. Four. c. All. d. None. 5. Consider these methods. ⇒ readLine() ⇒ readPassword() ⇒ flush() ⇒ reader() ⇒ writer() ⇒ printf() ⇒ format() How many of them declare a checked exception? (1 correct answer)

Transcript of OCPJP

-80 about NavigableMap.

1. Console is a class. (1 correct answer) a. trueb. false

2. Console belongs to the java.io package. (1 correct answer) a. trueb. false

3. All the following methods are defined in Console.    ⇒ readLine()    ⇒ readPassword()    ⇒ flush()    ⇒ reader()    ⇒ writer()    ⇒ printf()    ⇒ format()

a. True.b. False.

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

a. Two.b. Four.c. All.d. None.

5. Consider these methods.    ⇒ readLine()    ⇒ readPassword()    ⇒ flush()    ⇒ reader()    ⇒ writer()    ⇒ printf()    ⇒ format()How many of them declare a checked exception? (1 correct answer)

a. Two.b. Four.c. All.d. None.

6. What’s the signature of the method readPassword()? (1 correct answer) a. public char[] readPassword()b. public byte[] readPassword()c. public String readPassword()

7. What’s the signature of the method format()? (1 correct answer)

a. public void format(String, Object…)b. public String format(String, Object…)c. public Console format(String, Object…)

8. There are 2 overloaded methods of Console with the name readLine. (1 correct answer)

a. trueb. false

9. There are 2 overloaded methods of Console with the name getPassword. (1 correct answer)

a. trueb. false

10. There’s maximum one object of type Console available per JVM. (1 correct answer) a. True.b. False.

11. Will the following code compile successfully? (1 correct answer) 12. import java.io.Console;13.14. public class MyConsole implements Console {15.16. }

a. Yes.b. No.

17. Will the following code compile successfully? (1 correct answer) 18. import java.io.Console;19.20. public class MyConsole extends Console {21.22. }

a. Yes.b. No.

23. Will the following code compile successfully? (1 correct answer) 24. import java.io.Console;25.26. public class Test {27.     public static void main(String[] args) {28.         Console console = new Console();29.     }30. }

a. Yes.b. No.

31. Will the following code compile successfully? (1 correct answer) 32. import java.io.Console;33.34. public class Test {35.     public static void main(String[] args) {36.         Console console = System.getConsole();37.     }

38. }a. Yes.b. No.

39. Will the following code compile successfully? (1 correct answer) 40. import java.io.Console;41.42. public class Test {43.     public static void main(String[] args) {44.         Console console = System.console();45.     }46. }

a. Yes.b. No.

47. Will the following code compile successfully? (1 correct answer) 48. import java.io.Console;49.50. public class Test {51.     public static void main(String[] args) {52.         Console console = System.console();53.         console.println("Hello from your

console!");54.     }55. }

a. Yes.b. No.

56. Will the following code compile successfully? (1 correct answer) 57. import java.io.Console;58.59. public class Test {60.     public static void main(String[] args) {61.         Console console = System.console();62.         console.format("Hello from your

console!");63.     }64. }

a. Yes.b. No.

65. 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)

66. import java.io.Console;67.68. public class Test {69.     public static void main(String[] args) {70.         Console console = System.console(); // 171.         console.format("Hello from your

console!"); // 2

72.     }73. }

a. A runtime exception is thrown at line 1b. A runtime exception is thrown at line 2c. No exception is thrown at runtime.

74. What happens when this code is compiled and executed? (1 correct answer) 75. import java.io.Console;76.77. public class Test {78. public static void main(String[] args) {79.     Console console = System.console();80.         console.readLine("Please enter your name:

");81.         console.format(console.toString());82.     }83. }84. }

a. Compilation fails.b. An exception is thrown at runtime.c. The console prints exactly what the user typed before pressing ENTER.d. None of the above.

85. What happens when this code is compiled and executed? (1 correct answer) 86. import java.io.Console;87.88. public class Test {89. public static void main(String[] args) {90.     Console console = System.console();91.     if (console != null) {92.         console.writer().close();93.         console.printf("Hello!");94.     }95. }96. }

a. Compilation fails.b. An exception is thrown at runtime.c. The console prints “Hello!”.d. None of the above.

97. What happens when this code is compiled and executed? (1 correct answer) 98. import java.io.Console;99.100. public class Test {101. public static void main(String[] args) {102.     Console console = System.console();103.     if (console != null) {104.     String name = console.105.     readLine("Please enter your name: ");

106.     console.format("Hello %s!", name);107.     }108. }109. }

a. Compilation fails.b. An exception is thrown at runtime.c. If the user enters “Nikos”, the console prints “Hello Nikos!”

110. What happens when this code is compiled and executed? (1 correct answer) 111. import java.io.Console;112.113. public class Test {114. public static void main(String[] args) {115.     Console console = System.console();116.     if (console != null) {117.     String name = console.118.     readLine("Please enter your name: ");119.     console.format("Hello %z!", name);120.     }121. }122. }

a. Compilation fails.b. An exception is thrown at runtime.c. If the user enters “Nikos”, the console prints “Hello Nikos!”

123. What happens when this code is compiled and executed? (1 correct answer) 124. import java.io.Console;125.126. public class Test {127. public static void main(String[] args) {128.     Console console = System.console();129.     if (console != null) {130.     console.reader().close();131.     String name = console.132.     readLine("Please enter your name: ");133.     console.format("Hello %s!", name);134.     }135. }136. }

a. Compilation fails.b. An exception is thrown at runtime.c. If the user enters “Nikos”, the console prints “Hello Nikos!”

137. What happens when this code is compiled and executed? (1 correct answer) 138. import java.io.Console;139. import java.io.IOException;140.141. public class Test {142. public static void main(String[] args) {

143.     Console console = System.console();144.     try {145.         console.reader().close();146.     } catch (IOException e) {147.     }148.     String name = console.149.     readLine("Please enter your name: ");150.     console.format("Hello %s!", name);151. }152. }

a. Compilation fails.b. An exception is thrown at runtime.c. If the user enters “Nikos”, the console prints “Hello Nikos!”

153. What happens when this code is compiled and executed? (1 correct answer) 154. import java.io.Console;155.156. public class Test {157. public static void main(String[] args) {158.     Console console = System.console();159.     console.println("Hello!");160. }161. }

a. Compilation fails.b. An exception is thrown at runtime.c. The console prints “Hello!”.

162. What happens when this code is compiled and executed? (1 correct answer) 163. import java.io.Console;164.165. public class Test {166. public static void main(String[] args) {167.     Console console = System.console();168.     console.writer().println("Hello!");169. }170. }

a. Compilation fails.b. An exception is thrown at runtime.c. The console prints “Hello!”.

171. What happens when this code is compiled and executed? (1 correct answer) 172. import java.io.Console;173.174. public class Test {175. public static void main(String[] args) {176.     Console console = System.console();177.     console.writer().append("Hello ")178.     .append("from ").append("console!");179.     console.flush();

180. }181. }

a. Compilation fails.b. An exception is thrown at runtime.c. The console prints “Hello from console!”.

182. What happens when this code is compiled and executed? (1 correct answer) 183. import java.io.Console;184.185. public class Test {186. public static void main(String[] args) {187.     Console console = System.console();188.     console.format("Hello!", null);189. }190. }

a. Compilation fails.b. An exception is thrown at runtime.c. The console prints “Hello!”.

191. What happens when this code is compiled and executed? (1 correct answer) 192. import java.io.Console;193.194. public class Test {195. public static void main(String[] args) {196.     Console console = System.console();197.     console.format(null);198. }199. }

a. Compilation fails.b. An exception is thrown at runtime.c. The console prints “null”.

200. What happens when this code is compiled and executed? (1 correct answer) 201. import java.io.Console;202.203. public class Test {204. public static void main(String[] args) {205.     Console console = System.console();206.     console.format("Hello ").printf("from ")207.     .format("console!");208. }209. }

a. Compilation fails.b. An exception is thrown at runtime.c. The console prints “Hello from console!”.

210. NavigableSet is an interface. (1 correct answer) a. trueb. false

211. NavigableSet extends SortedSet. (1 correct answer) a. true

b. false212. In Java 1.5 TreeSet implements SortedSet, whereas in Java 1.6 TreeSet

implements NavigableSet. (1 correct answer) a. trueb. false

213. All these methods belong to NavigableSet. (1 correct answer)    lower()    higher()    floor()    ceiling()    pollFirst()    pollLast()

a. trueb. false

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

a. None.b. Two.c. Four.d. All.

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

a. None.b. Two.c. Four.d. All.

216. What’s the signature of the method lower()? (1 correct answer) a. E lower(E)b. boolean lower(E)c. E lower(NavigableSet<E>)

217. What’s the signature of the method pollFirst()? (1 correct answer) a. E pollFirst()b. E pollFirst(E)c. E pollFirst(NavigableSet<E>)

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

a. trueb. false

219. In the NavigableSet interface there are 2 overloaded methods with the name headSet. (1 correct answer)

a. trueb. false

220. What is the output of this code? (1 correct answer) 221. public static void main(String[] args) {222.     NavigableSet<Integer> set = new

TreeSet<Integer>();223.     set.add(-12);224.     set.add(24);225.     System.out.format("%d %d %d %d",226.         set.lower(-12),227.         set.lower(0),228.         set.lower(24),229.         set.lower(100)230.     );231. }

a. It prints “null -12 -12 24″.b. It prints “-12 -12 24 24″.

232. What is the output of this code? (1 correct answer) 233. public static void main(String[] args) {234.     NavigableSet<Integer> set = new

TreeSet<Integer>();235.     set.add(-12);236.     set.add(24);237.     System.out.format("%d %d %d %d",238.         set.floor(-12),239.         set.floor(0),240.         set.floor(24),241.         set.floor(100)242.     );243. }

a. It prints “null -12 -12 24″.b. It prints “-12 -12 24 24″.

244. What is the output of this code? (1 correct answer) 245. public static void main(String[] args) {246.     NavigableSet<Integer> set = new

TreeSet<Integer>();247.     set.add(-12);248.     set.add(24);249.     System.out.format("%d %d %d %d",250.         set.higher(-12),251.         set.higher(0),252.         set.higher(24),253.         set.higher(100)254.     );

255. }a. It prints “24 24 null null”.b. It prints “-12 24 24 null”.

256. What is the output of this code? (1 correct answer) 257. public static void main(String[] args) {258.     NavigableSet<Integer> set = new

TreeSet<Integer>();259.     set.add(-12);260.     set.add(24);261.     System.out.format("%d %d %d %d",262.         set.ceiling(-12),263.         set.ceiling(0),264.         set.ceiling(24),265.         set.ceiling(100)266.     );267. }

a. It prints “24 24 null null”.b. It prints “-12 24 24 null”.

268. What is the output of this code? (1 correct answer) 269. public static void main(String[] args) {270.     NavigableSet<Integer> set = new

TreeSet<Integer>();271.     set.add(-12);272.     set.add(24);273.     set.add(-28);274.     set.add(-0);275.     set.add(0);276.     set.add(+0);277.     set.add(11);278.     set.add(145);279.     System.out.format("%d %d %d %d",280.         set.higher(-28),281.         set.lower(24),282.         set.floor(-0),283.         set.ceiling(100)284.     );285. }

a. It prints “-12 11 0 100″.b. It prints “-12 11 0 145″.c. It prints “-28 24 0 100″.d. It prints “-28 24 0 145″.

286. What is the output of this code? (1 correct answer) 287. public static void main(String[] args) {288.     NavigableSet<Integer> set = new

TreeSet<Integer>();289.     set.pollFirst();

290.     System.out.println(set.size());291. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “0″.

292. What is the output of this code? (1 correct answer) 293. public static void main(String[] args) {294.     NavigableSet<Integer> set = new

TreeSet<Integer>();295.     set.first();296.     System.out.println(set.size());297. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “0″.

298. What is the output of this code? (1 correct answer) 299. public static void main(String[] args) {300.     NavigableSet<Integer> set = new

TreeSet<Integer>();301.     set.add(1);302.     set.add(2);303.     set.add(4);304.     NavigableSet<Integer> sub =

set.headSet(4);305.     System.out.println(sub.last());306. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “4″.d. It prints “2″.

307. What is the output of this code? (1 correct answer) 308. public static void main(String[] args) {309.     NavigableSet<Integer> set = new

TreeSet<Integer>();310.     set.add(1);311.     set.add(2);312.     set.add(4);313.     NavigableSet<Integer> sub =

set.headSet(4, true);314.     System.out.println(sub.last());315. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “4″.d. It prints “2″.

316. What is the output of this code? (1 correct answer) 317. public static void main(String[] args) {

318.     NavigableSet<Integer> set = new TreeSet<Integer>();

319.     set.add(1);320.     set.add(2);321.     set.add(4);322.     for (Iterator iterator =

set.descendingSet().iterator();323.         iterator.hasNext();) {324.         System.out.format("%d ",

iterator.next());325.     }326. }

a. It prints “1 2 4 “.b. It prints “4 2 1 “.c. Compilation fails.

327. NavigableMap IS-A Map. (1 correct answer) a. trueb. false

328. NavigableMap IS-A SortedMap. (1 correct answer) a. trueb. false

329. In Java 1.5 TreeMap implements SortedMap, whereas in Java 1.6 TreeMap implements NavigableMap. (1 correct answer)

a. trueb. false

330. All these methods belong to NavigableMap. (1 correct answer)    ⇒ lowerKey()    ⇒ higherKey()    ⇒ floorKey()    ⇒ ceilingKey()

a. trueb. false

331. All these methods belong to NavigableMap. (1 correct answer)    ⇒ lowerEntry()    ⇒ higherEntry()    ⇒ floorEntry()    ⇒ ceilingEntry()

a. trueb. false

332. All these methods belong to NavigableMap. (1 correct answer)    ⇒ pollFirstEntry()    ⇒ pollLastEntry()    ⇒ firstEntry()    ⇒ lastEntry()

a. trueb. false

333. All these methods belong to NavigableMap. (1 correct answer)    ⇒ pollFirstKey()

    ⇒ pollLastKey()    ⇒ firstKey()    ⇒ lastKey()

a. trueb. false

334. Assume NavigableMap<K,V>. What’s the signature of the method floorEntry()? (1 correct answer)

a. K floorEntry(K)b. V floorEntry(K)c. Map.Entry<K,V> floorEntry(K)

335. Assume NavigableMap<K,V>. What’s the signature of the method higherKey()? (1 correct answer)

a. K higherKey(K)b. Map.Entry<K,V> higherKey(K)c. K higherKey(NavigableMap<K,V>)

336. Assume NavigableMap<K,V>. What’s the signature of the method pollFirstEntry()? (1 correct answer)

a. Map.Entry<K,V> pollFirstEntry()b. Map.Entry<K,V> pollFirstEntry(K)c. Map.Entry<K,V> pollFirstEntry(NavigableMap<K,V>)

337. How many of these methods of NavigableMap may throw an exception at runtime? (1 correct answer)    ⇒ pollFirstEntry()    ⇒ pollLastEntry()    ⇒ firstEntry()    ⇒ lastEntry()    ⇒ firstKey()    ⇒ lastKey()

a. None.b. Two.c. Four.d. All.

338. How many of these methods of NavigableMap declare a checked exception? (1 correct answer)    ⇒ pollFirstEntry()    ⇒ pollLastEntry()    ⇒ firstEntry()    ⇒ lastEntry()    ⇒ firstKey()    ⇒ lastKey()

a. None.b. Two.c. Four.d. All.

339. In NavigableMap there are 2 overloaded methods with the name subMap. (1 correct answer)

a. trueb. false

340. In NavigableMap there are 2 overloaded methods with the name headMap. (1 correct answer)

a. trueb. false

341. What is the output of this code? (1 correct answer) 342. public static void main(String[] args) {343.     NavigableMap<Integer, String> map =344.     new TreeMap<Integer, String>();345.     map.put(5, "D");346.     map.put(-1, "A");347.     map.put(7, "O");348.     System.out.format("%d %d %d %d",349.         map.lowerKey(-100),350.         map.lowerKey(5),351.         map.lowerKey(6),352.         map.lowerKey(100)353. );354. }

a. It prints “null -1 5 7″.b. It prints “null 5 5 7″.

355. What is the output of this code? (1 correct answer) 356. public static void main(String[] args) {357.     NavigableMap<Integer, String> map =358.     new TreeMap<Integer, String>();359.     map.put(5, "D");360.     map.put(-1, "A");361.     map.put(7, "O");362.     System.out.format("%d %d %d %d",363.         map.floorKey(-100),364.         map.floorKey(5),365.         map.floorKey(6),366.         map.floorKey(100)367. );368. }

a. It prints “null -1 5 7″.b. It prints “null 5 5 7″.

369. What is the output of this code? (1 correct answer) 370. public static void main(String[] args) {371.     NavigableMap<Integer, String> map =372.     new TreeMap<Integer, String>();373.     map.put(5, "D");374.     map.put(-1, "A");375.     map.put(7, "O");376.     System.out.format("%d %d %d %d",377.         map.higherKey(-100),378.         map.higherKey(5),

379.         map.higherKey(6),380.         map.higherKey(100)381. );382. }

a. It prints “-1 7 7 null”.b. It prints “-1 5 7 null”.

383. What is the output of this code? (1 correct answer) 384. public static void main(String[] args) {385.     NavigableMap<Integer, String> map =386.     new TreeMap<Integer, String>();387.     map.put(5, "D");388.     map.put(-1, "A");389.     map.put(7, "O");390.     System.out.format("%d %d %d %d",391.         map.ceilingKey(-100),392.         map.ceilingKey(5),393.         map.ceilingKey(6),394.         map.ceilingKey(100)395. );396. }

a. It prints “-1 7 7 null”.b. It prints “-1 5 7 null”.

397. What is the output of this code? (1 correct answer) 398. public static void main(String[] args) {399.     NavigableMap<Integer, String> map =400.     new TreeMap<Integer, String>();401.     map.put(5, "D");402.     map.put(-1, "A");403.     map.put(7, "O");404.     System.out.format("%s %s %s %s",405.         map.lowerEntry(-100),406.         map.lowerEntry(5),407.         map.lowerEntry(6),408.         map.lowerEntry(100)409. );410. }

a. It prints “null -1=A 5=D 7=O”.b. It prints “null 5=D 5=D 7=O”.

411. What is the output of this code? (1 correct answer) 412. public static void main(String[] args) {413.     NavigableMap<Integer, String> map =414.     new TreeMap<Integer, String>();415.     map.put(5, "D");416.     map.put(-1, "A");417.     map.put(7, "O");418.     System.out.format("%s %s %s %s",

419.         map.floorEntry(-100),420.         map.floorEntry(5),421.         map.floorEntry(6),422.         map.floorEntry(100)423. );424. }

a. It prints “null -1=A 5=D 7=O”.b. It prints “null 5=D 5=D 7=O”.

425. What is the output of this code? (1 correct answer) 426. public static void main(String[] args) {427.     NavigableMap<Integer, String> map =428.     new TreeMap<Integer, String>();429.     map.put(5, "D");430.     map.put(-1, "A");431.     map.put(7, "O");432.     System.out.format("%s %s %s %s",433.         map.higherEntry(-100),434.         map.higherEntry(5),435.         map.higherEntry(6),436.         map.higherEntry(100)437. );438. }

a. It prints “-1=A 7=O 7=O null”.b. It prints “-1=A 5=D 7=O null”.

439. What is the output of this code? (1 correct answer) 440. public static void main(String[] args) {441.     NavigableMap<Integer, String> map =442.     new TreeMap<Integer, String>();443.     map.put(5, "D");444.     map.put(-1, "A");445.     map.put(7, "O");446.     System.out.format("%s %s %s %s",447.         map.ceilingEntry(-100),448.         map.ceilingEntry(5),449.         map.ceilingEntry(6),450.         map.ceilingEntry(100)451. );452. }

a. It prints “-1=A 7=O 7=O null”.b. It prints “-1=A 5=D 7=O null”.

453. What is the output of this code? (1 correct answer) 454. public static void main(String[] args) {455.     NavigableMap<Integer, String> map =456.     new TreeMap<Integer, String>();457.     map.put(34, "J");458.     map.put(-1, "a");

459.     map.put(70, "v");460.     map.put(5, "a");461.     map.put(-1, "2");462.     System.out.format("%s %s %s %s",463.         map.ceilingEntry(-100),464.         map.floorEntry(5),465.         map.higherEntry(6),466.         map.lowerKey(5)467.     );468. }

a. It prints “-1=2 5=a 34=J -1″.b. It prints “-1=a 5=a 34=J -1″.c. It prints “-1=2 5=a 34=J 5″.d. It prints “-1=a 5=a 34=J 5″.

469. What is the output of this code? (1 correct answer) 470. public static void main(String[] args) {471.     NavigableMap<Integer, String> map =472.     new TreeMap<Integer, String>();473.     map.lastEntry();474.     System.out.println(map.size());475. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “0″.

476. What is the output of this code? (1 correct answer) 477. public static void main(String[] args) {478.     NavigableMap<Integer, String> map =479.     new TreeMap<Integer, String>();480.     map.pollFirstEntry();481.     System.out.println(map.size());482. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “0″.

483. What is the output of this code? (1 correct answer) 484. public static void main(String[] args) {485.     NavigableMap<Integer, String> map =486.     new TreeMap<Integer, String>();487.     map.firstKey();488.     System.out.println(map.size());489. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “0″.

490. What is the output of this code? (1 correct answer) 491. public static void main(String[] args) {492.     NavigableMap<Integer, String> map =

493.     new TreeMap<Integer, String>();494.     map.put(1, "A");495.     map.put(2, "B");496.     map.put(3, "C");497.     NavigableMap<Integer, String> sub =498.     map.subMap(0, 3);499.     System.out.println(sub.lastKey());500. }

a. It prints “2″.b. It prints “3″.c. Compilation fails.d. An exception is thrown at runtime.

501. What is the output of this code? (1 correct answer) 502. public static void main(String[] args) {503.     NavigableMap<Integer, String> map =504.     new TreeMap<Integer, String>();505.     map.put(1, "A");506.     map.put(2, "B");507.     map.put(3, "C");508.     NavigableMap<Integer, String> sub =509.     map.subMap(0, false, 3, false);510.     System.out.println(sub.lastKey());511. }

a. It prints “2″.b. It prints “3″.c. Compilation fails.d. An exception is thrown at runtime.

512. What is the output of this code? (1 correct answer) 513. public static void main(String[] args) {514.     NavigableMap<Integer, String> map =515.     new TreeMap<Integer, String>();516.     map.put(1, "A");517.     map.put(2, "B");518.     map.put(3, "C");519.     NavigableMap<Integer, String> sub =520.     map.subMap(0, false, 3, false);521.     map.put(4, "D");522.     System.out.format("%d %d", map.size(),

sub.size());523. }

a. It prints “2 2″.b. It prints “3 2″.c. It prints “3 3″.d. It prints “4 2″.e. It prints “4 3″.f. Compilation fails.g. An exception is thrown at runtime.

524. What is the output of this code? (1 correct answer) 525. public static void main(String[] args) {526.     NavigableMap<Integer, String> map =527.     new TreeMap<Integer, String>();528.     map.put(1, "A");529.     map.put(2, "B");530.     map.put(3, "C");531.     NavigableMap<Integer, String> sub =532.     map.subMap(0, false, 3, false);533.     sub.put(4, "D");534.     System.out.format("%d %d", map.size(),

sub.size());535. }

a. It prints “2 2″.b. It prints “3 2″.c. It prints “3 3″.d. It prints “4 2″.e. It prints “4 3″.f. Compilation fails.g. 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. a2. a3. a4. d5. d6. a7. c8. a9. b10. a11. b12. b13. b14. b15. a16. b17. a18. a19. d20. c21. c22. b23. a

24. c25. a26. c27. c28. c29. b30. c31. a32. a33. a34. a35. a36. c37. a38. a39. a40. a41. a42. b43. a44. b45. b46. c47. a48. b49. c50. b51. a52. a53. a54. a55. a56. a57. b58. c59. a60. a61. b62. a63. a64. a65. a66. b67. a68. b69. a70. b71. a72. b73. a

74. c75. c76. a77. c78. a79. d80. 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.

Share this:

Email Digg Reddit Facebook StumbleUpon Twitter

Like this:

LikeOne blogger likes this post.

This entry was posted on Saturday, September 13th, 2008 at 10:53 am and is filed under SCJP 6, Training. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Post navigation

« Previous Post Next Post »

6 Responses to SCJP Mock exam for new Java 6 features

1. Nikos says:

11 October 2008 at 12:09 am

Thanks to Maggie Zhou for her corrections on questions 45 and 58!

2. Free SCJP Mock exams « Niko’s Java blog says:

6 February 2009 at 12:28 am

[...] Free SCJP Mock exam for new Java 6 features [...]

3. MarcoC says:

7 March 2009 at 9:50 pm

Question 18: for what i know if the JVM has no console System.console() returns null, so the exception is thrown at line 2 (answer b)

Question 19: there’s an extra }, so compilation fails

Many thanks for the exams.

4. lakshmi says:

13 November 2009 at 10:34 am

Hi,

Good job Niko. Nice material has given to us.

Have doubt in question no 26,27 and 28. I am using NetBeans 6.7 IDE tool. I ran it .All these 3 programs giving NullPointerException.

And the 20 was compiled successfully but doent print anything.

Last but not least, How should we give a name to question no 21 and 24?

Please let me know if anyone known about them as soon as poaaible.

5. Nikos says:

17 November 2009 at 8:48 pm

Hi lakshmi, you should not use the NetBeans IDE to run the questions for java.io.Console. You should run them from the command prompt of your operating system.

6. Vibha says:

31 March 2010 at 3:46 am

Hi, in que 9. I think answer is true as there are overloaded readPassword() method in console class. Plz explain if I am wrong.

Leave a Reply

Enter your comment here...

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

Email (required) (Address never made public)

Name (required)

Website

Notify me of follow-up comments via email.

Nikos Pougounias is a senior Analyst-Programmer specialized in Java Enterprise Edition and Object Oriented Design. During the past years, he has been contributing to major portfolios

under the European Commission.

Search

Visitorso 165,272 hits

Top posts (24h)o Free SCJP Mock exams

o SCJP 6 Mock exam for I/O o SCJP Mock exam for Collections

Categorieso Application Server (3) o Database (6) o JavaFX (2) o JDBC (4) o JPA (4) o JSP/Servlets (8) o Quick Tutorials (39) o RMI (1) o Ruby (3) o SCJP 6 (7) o SCWCD (9) o Training (20) o Web Services (3) o Wicket (12) o XML (4)

Archiveso August 2011 o July 2010 o March 2010 o August 2009 o May 2009 o March 2009 o February 2009 o January 2009 o November 2008 o October 2008 o September 2008 o August 2008 o July 2008 o June 2008 o May 2008 o March 2008

Basis data certification Custom Tags Database dom4j Eclipse Glassfish Hibernate J2EE

Nikos' Java blogquick & easy Java tutorials

Home

SCJP Mock exam for new Java 6 features

11 Votes

The new Java 6 features of SCJP exam are presented through 80 original questions.

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

1. Console is a class. (1 correct answer) a. trueb. false

2. Console belongs to the java.io package. (1 correct answer) a. trueb. false

3. All the following methods are defined in Console. ⇒ readLine() ⇒ readPassword() ⇒ flush() ⇒ reader() ⇒ writer() ⇒ printf() ⇒ format()

a. True.b. False.

4. Consider these methods. ⇒ readLine() ⇒ readPassword() ⇒ flush() ⇒ reader()

⇒ writer() ⇒ printf() ⇒ format()How many of them are static? (1 correct answer)

a. Two.b. Four.c. All.d. None.

5. Consider these methods. ⇒ readLine() ⇒ readPassword() ⇒ flush() ⇒ reader() ⇒ writer() ⇒ printf() ⇒ format()How many of them declare a checked exception? (1 correct answer)

a. Two.b. Four.c. All.d. None.

6. What’s the signature of the method readPassword()? (1 correct answer) a. public char[] readPassword()b. public byte[] readPassword()c. public String readPassword()

7. What’s the signature of the method format()? (1 correct answer) a. public void format(String, Object…)b. public String format(String, Object…)c. public Console format(String, Object…)

8. There are 2 overloaded methods of Console with the name readLine. (1 correct answer) a. trueb. false

9. There are 2 overloaded methods of Console with the name getPassword. (1 correct answer) a. trueb. false

10. There’s maximum one object of type Console available per JVM. (1 correct answer) a. True.b. False.

11. Will the following code compile successfully? (1 correct answer) 12. import java.io.Console;13.14. public class MyConsole implements Console {15.16. }

a. Yes.b. No.

17. Will the following code compile successfully? (1 correct answer) 18. import java.io.Console;19.20. public class MyConsole extends Console {

21.22. }

a. Yes.b. No.

23. Will the following code compile successfully? (1 correct answer) 24. import java.io.Console;25.26. public class Test {27.     public static void main(String[] args) {28.         Console console = new Console();29.     }30. }

a. Yes.b. No.

31. Will the following code compile successfully? (1 correct answer) 32. import java.io.Console;33.34. public class Test {35.     public static void main(String[] args) {36.         Console console = System.getConsole();37.     }38. }

a. Yes.b. No.

39. Will the following code compile successfully? (1 correct answer) 40. import java.io.Console;41.42. public class Test {43.     public static void main(String[] args) {44.         Console console = System.console();45.     }46. }

a. Yes.b. No.

47. Will the following code compile successfully? (1 correct answer) 48. import java.io.Console;49.50. public class Test {51.     public static void main(String[] args) {52.         Console console = System.console();53.         console.println("Hello from your

console!");54.     }55. }

a. Yes.b. No.

56. Will the following code compile successfully? (1 correct answer)

57. import java.io.Console;58.59. public class Test {60.     public static void main(String[] args) {61.         Console console = System.console();62.         console.format("Hello from your

console!");63.     }64. }

a. Yes.b. No.

65. 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)

66. import java.io.Console;67.68. public class Test {69.     public static void main(String[] args) {70.         Console console = System.console(); // 171.         console.format("Hello from your

console!"); // 272.     }73. }

a. A runtime exception is thrown at line 1b. A runtime exception is thrown at line 2c. No exception is thrown at runtime.

74. What happens when this code is compiled and executed? (1 correct answer) 75. import java.io.Console;76.77. public class Test {78. public static void main(String[] args) {79.     Console console = System.console();80.         console.readLine("Please enter your name:

");81.         console.format(console.toString());82.     }83. }84. }

a. Compilation fails.b. An exception is thrown at runtime.c. The console prints exactly what the user typed before pressing ENTER.d. None of the above.

85. What happens when this code is compiled and executed? (1 correct answer) 86. import java.io.Console;87.88. public class Test {89. public static void main(String[] args) {90.     Console console = System.console();

91.     if (console != null) {92.         console.writer().close();93.         console.printf("Hello!");94.     }95. }96. }

a. Compilation fails.b. An exception is thrown at runtime.c. The console prints “Hello!”.d. None of the above.

97. What happens when this code is compiled and executed? (1 correct answer) 98. import java.io.Console;99.100. public class Test {101. public static void main(String[] args) {102.     Console console = System.console();103.     if (console != null) {104.     String name = console.105.     readLine("Please enter your name: ");106.     console.format("Hello %s!", name);107.     }108. }109. }

a. Compilation fails.b. An exception is thrown at runtime.c. If the user enters “Nikos”, the console prints “Hello Nikos!”

110. What happens when this code is compiled and executed? (1 correct answer) 111. import java.io.Console;112.113. public class Test {114. public static void main(String[] args) {115.     Console console = System.console();116.     if (console != null) {117.     String name = console.118.     readLine("Please enter your name: ");119.     console.format("Hello %z!", name);120.     }121. }122. }

a. Compilation fails.b. An exception is thrown at runtime.c. If the user enters “Nikos”, the console prints “Hello Nikos!”

123. What happens when this code is compiled and executed? (1 correct answer) 124. import java.io.Console;125.126. public class Test {127. public static void main(String[] args) {

128.     Console console = System.console();129.     if (console != null) {130.     console.reader().close();131.     String name = console.132.     readLine("Please enter your name: ");133.     console.format("Hello %s!", name);134.     }135. }136. }

a. Compilation fails.b. An exception is thrown at runtime.c. If the user enters “Nikos”, the console prints “Hello Nikos!”

137. What happens when this code is compiled and executed? (1 correct answer) 138. import java.io.Console;139. import java.io.IOException;140.141. public class Test {142. public static void main(String[] args) {143.     Console console = System.console();144.     try {145.         console.reader().close();146.     } catch (IOException e) {147.     }148.     String name = console.149.     readLine("Please enter your name: ");150.     console.format("Hello %s!", name);151. }152. }

a. Compilation fails.b. An exception is thrown at runtime.c. If the user enters “Nikos”, the console prints “Hello Nikos!”

153. What happens when this code is compiled and executed? (1 correct answer) 154. import java.io.Console;155.156. public class Test {157. public static void main(String[] args) {158.     Console console = System.console();159.     console.println("Hello!");160. }161. }

a. Compilation fails.b. An exception is thrown at runtime.c. The console prints “Hello!”.

162. What happens when this code is compiled and executed? (1 correct answer) 163. import java.io.Console;164.165. public class Test {

166. public static void main(String[] args) {167.     Console console = System.console();168.     console.writer().println("Hello!");169. }170. }

a. Compilation fails.b. An exception is thrown at runtime.c. The console prints “Hello!”.

171. What happens when this code is compiled and executed? (1 correct answer) 172. import java.io.Console;173.174. public class Test {175. public static void main(String[] args) {176.     Console console = System.console();177.     console.writer().append("Hello ")178.     .append("from ").append("console!");179.     console.flush();180. }181. }

a. Compilation fails.b. An exception is thrown at runtime.c. The console prints “Hello from console!”.

182. What happens when this code is compiled and executed? (1 correct answer) 183. import java.io.Console;184.185. public class Test {186. public static void main(String[] args) {187.     Console console = System.console();188.     console.format("Hello!", null);189. }190. }

a. Compilation fails.b. An exception is thrown at runtime.c. The console prints “Hello!”.

191. What happens when this code is compiled and executed? (1 correct answer) 192. import java.io.Console;193.194. public class Test {195. public static void main(String[] args) {196.     Console console = System.console();197.     console.format(null);198. }199. }

a. Compilation fails.b. An exception is thrown at runtime.c. The console prints “null”.

200. What happens when this code is compiled and executed? (1 correct answer)

201. import java.io.Console;202.203. public class Test {204. public static void main(String[] args) {205.     Console console = System.console();206.     console.format("Hello ").printf("from ")207.     .format("console!");208. }209. }

a. Compilation fails.b. An exception is thrown at runtime.c. The console prints “Hello from console!”.

210. NavigableSet is an interface. (1 correct answer) a. trueb. false

211. NavigableSet extends SortedSet. (1 correct answer) a. trueb. false

212. In Java 1.5 TreeSet implements SortedSet, whereas in Java 1.6 TreeSet implements NavigableSet. (1 correct answer)

a. trueb. false

213. All these methods belong to NavigableSet. (1 correct answer) lower() higher() floor() ceiling() pollFirst() pollLast()

a. trueb. false

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

a. None.b. Two.c. Four.d. All.

215. How many of the following methods may throw an exception at runtime? (1 correct answer) lower() higher() floor() ceiling()

pollFirst() pollLast()

a. None.b. Two.c. Four.d. All.

216. What’s the signature of the method lower()? (1 correct answer) a. E lower(E)b. boolean lower(E)c. E lower(NavigableSet<E>)

217. What’s the signature of the method pollFirst()? (1 correct answer) a. E pollFirst()b. E pollFirst(E)c. E pollFirst(NavigableSet<E>)

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

a. trueb. false

219. In the NavigableSet interface there are 2 overloaded methods with the name headSet. (1 correct answer)

a. trueb. false

220. What is the output of this code? (1 correct answer) 221. public static void main(String[] args) {222.     NavigableSet<Integer> set = new

TreeSet<Integer>();223.     set.add(-12);224.     set.add(24);225.     System.out.format("%d %d %d %d",226.         set.lower(-12),227.         set.lower(0),228.         set.lower(24),229.         set.lower(100)230.     );231. }

a. It prints “null -12 -12 24″.b. It prints “-12 -12 24 24″.

232. What is the output of this code? (1 correct answer) 233. public static void main(String[] args) {234.     NavigableSet<Integer> set = new

TreeSet<Integer>();235.     set.add(-12);236.     set.add(24);237.     System.out.format("%d %d %d %d",238.         set.floor(-12),239.         set.floor(0),240.         set.floor(24),241.         set.floor(100)

242.     );243. }

a. It prints “null -12 -12 24″.b. It prints “-12 -12 24 24″.

244. What is the output of this code? (1 correct answer) 245. public static void main(String[] args) {246.     NavigableSet<Integer> set = new

TreeSet<Integer>();247.     set.add(-12);248.     set.add(24);249.     System.out.format("%d %d %d %d",250.         set.higher(-12),251.         set.higher(0),252.         set.higher(24),253.         set.higher(100)254.     );255. }

a. It prints “24 24 null null”.b. It prints “-12 24 24 null”.

256. What is the output of this code? (1 correct answer) 257. public static void main(String[] args) {258.     NavigableSet<Integer> set = new

TreeSet<Integer>();259.     set.add(-12);260.     set.add(24);261.     System.out.format("%d %d %d %d",262.         set.ceiling(-12),263.         set.ceiling(0),264.         set.ceiling(24),265.         set.ceiling(100)266.     );267. }

a. It prints “24 24 null null”.b. It prints “-12 24 24 null”.

268. What is the output of this code? (1 correct answer) 269. public static void main(String[] args) {270.     NavigableSet<Integer> set = new

TreeSet<Integer>();271.     set.add(-12);272.     set.add(24);273.     set.add(-28);274.     set.add(-0);275.     set.add(0);276.     set.add(+0);277.     set.add(11);278.     set.add(145);

279.     System.out.format("%d %d %d %d",280.         set.higher(-28),281.         set.lower(24),282.         set.floor(-0),283.         set.ceiling(100)284.     );285. }

a. It prints “-12 11 0 100″.b. It prints “-12 11 0 145″.c. It prints “-28 24 0 100″.d. It prints “-28 24 0 145″.

286. What is the output of this code? (1 correct answer) 287. public static void main(String[] args) {288.     NavigableSet<Integer> set = new

TreeSet<Integer>();289.     set.pollFirst();290.     System.out.println(set.size());291. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “0″.

292. What is the output of this code? (1 correct answer) 293. public static void main(String[] args) {294.     NavigableSet<Integer> set = new

TreeSet<Integer>();295.     set.first();296.     System.out.println(set.size());297. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “0″.

298. What is the output of this code? (1 correct answer) 299. public static void main(String[] args) {300.     NavigableSet<Integer> set = new

TreeSet<Integer>();301.     set.add(1);302.     set.add(2);303.     set.add(4);304.     NavigableSet<Integer> sub =

set.headSet(4);305.     System.out.println(sub.last());306. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “4″.d. It prints “2″.

307. What is the output of this code? (1 correct answer)

308. public static void main(String[] args) {309.     NavigableSet<Integer> set = new

TreeSet<Integer>();310.     set.add(1);311.     set.add(2);312.     set.add(4);313.     NavigableSet<Integer> sub =

set.headSet(4, true);314.     System.out.println(sub.last());315. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “4″.d. It prints “2″.

316. What is the output of this code? (1 correct answer) 317. public static void main(String[] args) {318.     NavigableSet<Integer> set = new

TreeSet<Integer>();319.     set.add(1);320.     set.add(2);321.     set.add(4);322.     for (Iterator iterator =

set.descendingSet().iterator();323.         iterator.hasNext();) {324.         System.out.format("%d ",

iterator.next());325.     }326. }

a. It prints “1 2 4 “.b. It prints “4 2 1 “.c. Compilation fails.

327. NavigableMap IS-A Map. (1 correct answer) a. trueb. false

328. NavigableMap IS-A SortedMap. (1 correct answer) a. trueb. false

329. In Java 1.5 TreeMap implements SortedMap, whereas in Java 1.6 TreeMap implements NavigableMap. (1 correct answer)

a. trueb. false

330. All these methods belong to NavigableMap. (1 correct answer) ⇒ lowerKey() ⇒ higherKey() ⇒ floorKey() ⇒ ceilingKey()

a. trueb. false

331. All these methods belong to NavigableMap. (1 correct answer) ⇒ lowerEntry() ⇒ higherEntry() ⇒ floorEntry() ⇒ ceilingEntry()

a. trueb. false

332. All these methods belong to NavigableMap. (1 correct answer) ⇒ pollFirstEntry() ⇒ pollLastEntry() ⇒ firstEntry() ⇒ lastEntry()

a. trueb. false

333. All these methods belong to NavigableMap. (1 correct answer) ⇒ pollFirstKey() ⇒ pollLastKey() ⇒ firstKey() ⇒ lastKey()

a. trueb. false

334. Assume NavigableMap<K,V>. What’s the signature of the method floorEntry()? (1 correct answer)

a. K floorEntry(K)b. V floorEntry(K)c. Map.Entry<K,V> floorEntry(K)

335. Assume NavigableMap<K,V>. What’s the signature of the method higherKey()? (1 correct answer)

a. K higherKey(K)b. Map.Entry<K,V> higherKey(K)c. K higherKey(NavigableMap<K,V>)

336. Assume NavigableMap<K,V>. What’s the signature of the method pollFirstEntry()? (1 correct answer)

a. Map.Entry<K,V> pollFirstEntry()b. Map.Entry<K,V> pollFirstEntry(K)c. Map.Entry<K,V> pollFirstEntry(NavigableMap<K,V>)

337. How many of these methods of NavigableMap may throw an exception at runtime? (1 correct answer) ⇒ pollFirstEntry() ⇒ pollLastEntry() ⇒ firstEntry() ⇒ lastEntry() ⇒ firstKey() ⇒ lastKey()

a. None.b. Two.c. Four.d. All.

338. How many of these methods of NavigableMap declare a checked exception? (1 correct answer) ⇒ pollFirstEntry()

⇒ pollLastEntry() ⇒ firstEntry() ⇒ lastEntry() ⇒ firstKey() ⇒ lastKey()

a. None.b. Two.c. Four.d. All.

339. In NavigableMap there are 2 overloaded methods with the name subMap. (1 correct answer)

a. trueb. false

340. In NavigableMap there are 2 overloaded methods with the name headMap. (1 correct answer)

a. trueb. false

341. What is the output of this code? (1 correct answer) 342. public static void main(String[] args) {343.     NavigableMap<Integer, String> map =344.     new TreeMap<Integer, String>();345.     map.put(5, "D");346.     map.put(-1, "A");347.     map.put(7, "O");348.     System.out.format("%d %d %d %d",349.         map.lowerKey(-100),350.         map.lowerKey(5),351.         map.lowerKey(6),352.         map.lowerKey(100)353. );354. }

a. It prints “null -1 5 7″.b. It prints “null 5 5 7″.

355. What is the output of this code? (1 correct answer) 356. public static void main(String[] args) {357.     NavigableMap<Integer, String> map =358.     new TreeMap<Integer, String>();359.     map.put(5, "D");360.     map.put(-1, "A");361.     map.put(7, "O");362.     System.out.format("%d %d %d %d",363.         map.floorKey(-100),364.         map.floorKey(5),365.         map.floorKey(6),366.         map.floorKey(100)367. );368. }

a. It prints “null -1 5 7″.

b. It prints “null 5 5 7″.369. What is the output of this code? (1 correct answer) 370. public static void main(String[] args) {371.     NavigableMap<Integer, String> map =372.     new TreeMap<Integer, String>();373.     map.put(5, "D");374.     map.put(-1, "A");375.     map.put(7, "O");376.     System.out.format("%d %d %d %d",377.         map.higherKey(-100),378.         map.higherKey(5),379.         map.higherKey(6),380.         map.higherKey(100)381. );382. }

a. It prints “-1 7 7 null”.b. It prints “-1 5 7 null”.

383. What is the output of this code? (1 correct answer) 384. public static void main(String[] args) {385.     NavigableMap<Integer, String> map =386.     new TreeMap<Integer, String>();387.     map.put(5, "D");388.     map.put(-1, "A");389.     map.put(7, "O");390.     System.out.format("%d %d %d %d",391.         map.ceilingKey(-100),392.         map.ceilingKey(5),393.         map.ceilingKey(6),394.         map.ceilingKey(100)395. );396. }

a. It prints “-1 7 7 null”.b. It prints “-1 5 7 null”.

397. What is the output of this code? (1 correct answer) 398. public static void main(String[] args) {399.     NavigableMap<Integer, String> map =400.     new TreeMap<Integer, String>();401.     map.put(5, "D");402.     map.put(-1, "A");403.     map.put(7, "O");404.     System.out.format("%s %s %s %s",405.         map.lowerEntry(-100),406.         map.lowerEntry(5),407.         map.lowerEntry(6),408.         map.lowerEntry(100)409. );

410. }a. It prints “null -1=A 5=D 7=O”.b. It prints “null 5=D 5=D 7=O”.

411. What is the output of this code? (1 correct answer) 412. public static void main(String[] args) {413.     NavigableMap<Integer, String> map =414.     new TreeMap<Integer, String>();415.     map.put(5, "D");416.     map.put(-1, "A");417.     map.put(7, "O");418.     System.out.format("%s %s %s %s",419.         map.floorEntry(-100),420.         map.floorEntry(5),421.         map.floorEntry(6),422.         map.floorEntry(100)423. );424. }

a. It prints “null -1=A 5=D 7=O”.b. It prints “null 5=D 5=D 7=O”.

425. What is the output of this code? (1 correct answer) 426. public static void main(String[] args) {427.     NavigableMap<Integer, String> map =428.     new TreeMap<Integer, String>();429.     map.put(5, "D");430.     map.put(-1, "A");431.     map.put(7, "O");432.     System.out.format("%s %s %s %s",433.         map.higherEntry(-100),434.         map.higherEntry(5),435.         map.higherEntry(6),436.         map.higherEntry(100)437. );438. }

a. It prints “-1=A 7=O 7=O null”.b. It prints “-1=A 5=D 7=O null”.

439. What is the output of this code? (1 correct answer) 440. public static void main(String[] args) {441.     NavigableMap<Integer, String> map =442.     new TreeMap<Integer, String>();443.     map.put(5, "D");444.     map.put(-1, "A");445.     map.put(7, "O");446.     System.out.format("%s %s %s %s",447.         map.ceilingEntry(-100),448.         map.ceilingEntry(5),449.         map.ceilingEntry(6),

450.         map.ceilingEntry(100)451. );452. }

a. It prints “-1=A 7=O 7=O null”.b. It prints “-1=A 5=D 7=O null”.

453. What is the output of this code? (1 correct answer) 454. public static void main(String[] args) {455.     NavigableMap<Integer, String> map =456.     new TreeMap<Integer, String>();457.     map.put(34, "J");458.     map.put(-1, "a");459.     map.put(70, "v");460.     map.put(5, "a");461.     map.put(-1, "2");462.     System.out.format("%s %s %s %s",463.         map.ceilingEntry(-100),464.         map.floorEntry(5),465.         map.higherEntry(6),466.         map.lowerKey(5)467.     );468. }

a. It prints “-1=2 5=a 34=J -1″.b. It prints “-1=a 5=a 34=J -1″.c. It prints “-1=2 5=a 34=J 5″.d. It prints “-1=a 5=a 34=J 5″.

469. What is the output of this code? (1 correct answer) 470. public static void main(String[] args) {471.     NavigableMap<Integer, String> map =472.     new TreeMap<Integer, String>();473.     map.lastEntry();474.     System.out.println(map.size());475. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “0″.

476. What is the output of this code? (1 correct answer) 477. public static void main(String[] args) {478.     NavigableMap<Integer, String> map =479.     new TreeMap<Integer, String>();480.     map.pollFirstEntry();481.     System.out.println(map.size());482. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “0″.

483. What is the output of this code? (1 correct answer) 484. public static void main(String[] args) {

485.     NavigableMap<Integer, String> map =486.     new TreeMap<Integer, String>();487.     map.firstKey();488.     System.out.println(map.size());489. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “0″.

490. What is the output of this code? (1 correct answer) 491. public static void main(String[] args) {492.     NavigableMap<Integer, String> map =493.     new TreeMap<Integer, String>();494.     map.put(1, "A");495.     map.put(2, "B");496.     map.put(3, "C");497.     NavigableMap<Integer, String> sub =498.     map.subMap(0, 3);499.     System.out.println(sub.lastKey());500. }

a. It prints “2″.b. It prints “3″.c. Compilation fails.d. An exception is thrown at runtime.

501. What is the output of this code? (1 correct answer) 502. public static void main(String[] args) {503.     NavigableMap<Integer, String> map =504.     new TreeMap<Integer, String>();505.     map.put(1, "A");506.     map.put(2, "B");507.     map.put(3, "C");508.     NavigableMap<Integer, String> sub =509.     map.subMap(0, false, 3, false);510.     System.out.println(sub.lastKey());511. }

a. It prints “2″.b. It prints “3″.c. Compilation fails.d. An exception is thrown at runtime.

512. What is the output of this code? (1 correct answer) 513. public static void main(String[] args) {514.     NavigableMap<Integer, String> map =515.     new TreeMap<Integer, String>();516.     map.put(1, "A");517.     map.put(2, "B");518.     map.put(3, "C");519.     NavigableMap<Integer, String> sub =520.     map.subMap(0, false, 3, false);

521.     map.put(4, "D");522.     System.out.format("%d %d", map.size(),

sub.size());523. }

a. It prints “2 2″.b. It prints “3 2″.c. It prints “3 3″.d. It prints “4 2″.e. It prints “4 3″.f. Compilation fails.g. An exception is thrown at runtime.

524. What is the output of this code? (1 correct answer) 525. public static void main(String[] args) {526.     NavigableMap<Integer, String> map =527.     new TreeMap<Integer, String>();528.     map.put(1, "A");529.     map.put(2, "B");530.     map.put(3, "C");531.     NavigableMap<Integer, String> sub =532.     map.subMap(0, false, 3, false);533.     sub.put(4, "D");534.     System.out.format("%d %d", map.size(),

sub.size());535. }

a. It prints “2 2″.b. It prints “3 2″.c. It prints “3 3″.d. It prints “4 2″.e. It prints “4 3″.f. Compilation fails.g. 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. a2. a3. a4. d5. d6. a7. c8. a9. b10. a11. b12. b

13. b14. b15. a16. b17. a18. a19. d20. c21. c22. b23. a24. c25. a26. c27. c28. c29. b30. c31. a32. a33. a34. a35. a36. c37. a38. a39. a40. a41. a42. b43. a44. b45. b46. c47. a48. b49. c50. b51. a52. a53. a54. a55. a56. a57. b58. c59. a60. a61. b62. a63. a

64. a65. a66. b67. a68. b69. a70. b71. a72. b73. a74. c75. c76. a77. c78. a79. d80. 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.

Share this:

Email Digg Reddit Facebook StumbleUpon Twitter

Like this:

Like

One blogger likes this post.

This entry was posted on Saturday, September 13th, 2008 at 10:53 am and is filed under SCJP 6, Training. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Post navigation

« Previous Post Next Post »

6 Responses to SCJP Mock exam for new Java 6 features

1. Nikos says:

11 October 2008 at 12:09 am

Thanks to Maggie Zhou for her corrections on questions 45 and 58!

2. Free SCJP Mock exams « Niko’s Java blog says:

6 February 2009 at 12:28 am

[...] Free SCJP Mock exam for new Java 6 features [...]

3. MarcoC says:

7 March 2009 at 9:50 pm

Question 18: for what i know if the JVM has no console System.console() returns null, so the exception is thrown at line 2 (answer b)

Question 19: there’s an extra }, so compilation fails

Many thanks for the exams.

4. lakshmi says:

13 November 2009 at 10:34 am

Hi,

Good job Niko. Nice material has given to us.

Have doubt in question no 26,27 and 28. I am using NetBeans 6.7 IDE tool. I ran it .All these 3 programs giving NullPointerException.

And the 20 was compiled successfully but doent print anything.

Last but not least, How should we give a name to question no 21 and 24?

Please let me know if anyone known about them as soon as poaaible.

5. Nikos says:

17 November 2009 at 8:48 pm

Hi lakshmi, you should not use the NetBeans IDE to run the questions for java.io.Console. You should run them from the command prompt of your operating system.

6. Vibha says:

31 March 2010 at 3:46 am

Hi, in que 9. I think answer is true as there are overloaded readPassword() method in console class. Plz explain if I am wrong.

Leave a Reply

Enter your comment here...

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

Email (required) (Address never made public)

Name (required)

Website

Notify me of follow-up comments via email.

Nikos Pougounias is a senior Analyst-Programmer specialized in Java Enterprise Edition and Object Oriented Design. During the past years, he has been contributing to major portfolios under the European Commission.

Search

Visitorso 165,279 hits

Top posts (24h)o Free SCJP Mock exams o SCJP 6 Mock exam for I/O o SCJP Mock exam for Overriding & Overloading

Categorieso Application Server (3) o Database (6) o JavaFX (2) o JDBC (4) o JPA (4) o JSP/Servlets (8) o Quick Tutorials (39) o RMI (1) o Ruby (3) o SCJP 6 (7) o SCWCD (9) o Training (20) o Web Services (3) o Wicket (12) o XML (4)

Archiveso August 2011 o July 2010 o March 2010 o August 2009 o May 2009

o March 2009 o February 2009 o January 2009 o November 2008 o October 2008 o September 2008 o August 2008 o July 2008 o June 2008 o May 2008 o March 2008

Basis data certification Custom Tags Database dom4j Eclipse Glassfish Hibernate J2EE

tutorial Java Java EE Java EE tutorial JavaFX Java Persistence API JAX-WS JAXB JBoss JDBC JDeveloper

JPA JPA 2 JSP JWS Music MySQL NetBeans object-relational mapping OCPJP OCPJWCD OpenSolaris Oracle

RMI Ruby SCJP SCWCD Spring Spring 3 SQL Sun Web Web Services Wicket XML Βάση

δεδομένων

Theme: Contempt by Vault9.Blog at WordPress.com.

Follow

Follow “Nikos' Java blog”

Get every new post delivered to your Inbox.

Powered by WordPress.com

Nikos' Java blogquick & easy Java tutorials

Home

SCJP 6 Mock exam for Threads

Enter your

21 Votes

Threads is a valuable knowledge for every developer. This free mock exam presents important Thread skills required by the SCJP exam.

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

1. Runnable is an interface. (1 correct answer) a. trueb. false

2. Runnable belongs to the java.lang package. (1 correct answer) a. trueb. false

3. How many methods does the Runnable interface define? (1 correct answer) a. One: run()b. Two: run() and start()c. Three: run(), start() and join()

4. Runnable defines a method run. What’s the signature? (1 correct answer) a. public void run()b. public void run() throws InterruptedExceptionc. public void run(Runnable r)d. public void run(Runnable r) throws InterruptedException

5. Class Thread implements the Runnable interface. (1 correct answer) a. trueb. false

6. Consider these methods. ⇒ run() ⇒ start() ⇒ join() ⇒ sleep() ⇒ yield() ⇒ currentThread()How many of them are defined in the Thread class? (1 correct answer)

a. One.b. Two.c. Three.d. All.

7. Consider these methods. ⇒ run() ⇒ start() ⇒ join() ⇒ sleep() ⇒ yield() ⇒ currentThread()How many of them are static? (1 correct answer)

a. One.b. Two.

c. Three.d. All.

8. Consider these methods. ⇒ run() ⇒ start() ⇒ join() ⇒ sleep() ⇒ yield() ⇒ currentThread()How many of them declare a checked exception? (1 correct answer)

a. One.b. Two.c. Three.d. All.

9. What’s the signature of the method start()? (1 correct answer) a. public void start()b. public void start() throws InterruptedExceptionc. public void start() throws IllegalThreadStateException

10. What’s the signature of the method yield()? (1 correct answer) a. public void yield()b. public void yield() throws InterruptedExceptionc. public static void yield()d. public static void yield() throws InterruptedException

11. There are 2 overloaded methods of Thread with the name sleep. (1 correct answer) a. trueb. false

12. There are 3 overloaded methods of Thread with the name join. (1 correct answer) a. trueb. false

13. The Thread class has three public static and final integer fields: MIN_PRIORITY, NORM_PRIORITY, MAX_PRIORITY. (1 correct answer)

a. trueb. false

14. Consider these methods. ⇒ wait() ⇒ notify() ⇒ notifyAll()How many of them are defined in the Object class? (1 correct answer)

a. One.b. All.c. None.

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

a. One.b. All.c. None.

16. Consider these methods. ⇒ wait()

⇒ notify() ⇒ notifyAll()How many of them declare a checked exception? (1 correct answer)

a. One.b. All.c. None.

17. There are 3 overloaded methods of Object with the name wait. (1 correct answer) a. trueb. false

18. Will this code compile successfully? (1 correct answer) 19. class Test extends Runnable {20.21. }

a. Yes.b. No.

22. Will this code compile successfully? (1 correct answer) 23. class Test implements Runnable {24.25. }

a. Yes.b. No.

26. Will this code compile successfully? (1 correct answer) 27. class Test implements Runnable {28.     void run() {29.     }30. }

a. Yes.b. No.

31. Will this code compile successfully? (1 correct answer) 32. class Test implements Runnable {33.     public void run() {34.     }35.     void run(Runnable r) {36.     }37.     void run(String... s) {38.     }39. }

a. Yes.b. No.

40. Will this code compile successfully? (1 correct answer) 41. class Test extends Thread {42.43. }

a. Yes.b. No.

44. Will this code compile successfully? (1 correct answer) 45. class Test extends Thread {46.     public Test() {

47.         super();48.     }49. }

a. Yes.b. No.

50. Will this code compile successfully? (1 correct answer) 51. class Test extends Thread {52.     public Test() {53.         super("Nikos");54.     }55. }

a. Yes.b. No.

56. Will this code compile successfully? (1 correct answer) 57. class Test extends Thread {58.     public Test(String name) {59.         super(name);60.     }61. }

a. Yes.b. No.

62. Will this code compile successfully? (1 correct answer) 63. class Test extends Thread {64.     public Test(Runnable job, String name) {65.         super(job);66.     }67. }

a. Yes.b. No.

68. Will this code compile successfully? (1 correct answer) 69. class Test extends Thread {70.     public Test(Runnable job, String name) {71.         super(job, name);72.     }73.74. }

a. Yes.b. No.

75. Will this code compile successfully? (1 correct answer) 76. import java.util.NavigableSet;77. class Test extends Thread {78.     public void run(NavigableSet<Thread> set) {79.     }80. }

a. Yes.b. No.

81. Will this code compile successfully? (1 correct answer)

82. class Test extends Thread {83.     public void run() throws InterruptedException

{84.     }85. }

a. Yes.b. No.

86. Will this code compile successfully? (1 correct answer) 87. class Test extends Thread {88.     public void join() throws InterruptedException

{89.     }90. }

a. Yes.b. No.

91. What happens when this code gets compiled and executed? (1 correct answer) 92. public class Test extends Thread {93.     public void run() {94.         System.out.println("run 1");95.     }96.     public void run(String s) {97.         System.out.println("run 2");98.     }99.     public static void main(String[] args) {100.         new Test().start();101.     }102. }

a. It prints “run 1″.b. It prints “run 2″.c. Compilation fails.

103. What happens when this code gets compiled and executed? (1 correct answer) 104. public class Test extends Thread {105.     public void run() {106.         System.out.println("run 1");107.     }108.     public void run(String s) {109.         System.out.println("run 2");110.     }111.     public static void main(String[] args) {112.         new Test().start("hi");113.     }114. }

a. It prints “run 1″.b. It prints “run 2″.c. Compilation fails.

115. What happens when this code gets compiled and executed? (1 correct answer) 116. public class Test {

117.     public static void main(String[] args) {118.         String name =

Thread.currentThread().getName();119.         System.out.println(name);120.     }121. }

a. It prints nothing.b. It prints “main”.c. It prints “Thread-0″.d. Compilation fails because an InterruptedException is not handled.

122. What happens when this code gets compiled and executed? (1 correct answer) 123. public class Test extends Thread {124.     public static void main(String[] args) {125.         String name =

Thread.currentThread().getName();126.         System.out.println(name);127.     }128. }

a. It prints nothing.b. It prints “main”.c. It prints “Thread-0″.d. Compilation fails because an InterruptedException is not handled.e. Compilation fails because Test does not implement the run() method.

129. What happens when this code gets compiled and executed? (1 correct answer) 130. public class Run {131.     public static void main(String[] args) {132.         new Thread().run();133.     }134. }

a. Compilation fails.b. It compiles and runs fine.c. An IllegalThreadStateException is thrown at runtime.

135. What happens when this code gets compiled and executed? (1 correct answer) 136. public class Run {137.     public static void main(String[] args) {138.         Thread thread = new Thread();139.         thread.run();140.         thread.run();141.     }142. }

a. Compilation fails.b. It compiles and runs fine.c. An IllegalThreadStateException is thrown at runtime.

143. What happens when this code gets compiled and executed? (1 correct answer) 144. public class Run {145.     public static void main(String[] args) {146.         new Thread().start();

147.     }148. }

a. Compilation fails.b. It compiles and runs fine.c. An IllegalThreadStateException is thrown at runtime.

149. What happens when this code gets compiled and executed? (1 correct answer) 150. public class Run {151.     public static void main(String[] args) {152.         Thread thread = new Thread();153.         thread.start();154.         thread.start();155.     }156. }

a. Compilation fails.b. It compiles and runs fine.c. An IllegalThreadStateException is thrown at runtime.

157. What happens when this code gets compiled and executed? (1 correct answer) 158. public class Run {159.     public static void main(String[] args) {160.         Thread thread = new Thread();161.         thread.start();162.         thread = new Thread();163.         thread.start();164.     }165. }

a. Compilation fails.b. It compiles and runs fine.c. An IllegalThreadStateException is thrown at runtime.

166. What happens when this code gets compiled and executed? (1 correct answer) 167. public class Run {168.     public static void main(String[] args) {169.         Thread thread = new Thread();170.         thread.start();171.         Thread.yield();172.         thread.start();173.     }174. }

a. Compilation fails.b. It compiles and runs fine.c. An IllegalThreadStateException is thrown at runtime.

175. What happens when this code gets compiled and executed? (1 correct answer) 176. public class Run {177.     public static void main(String[] args) {178.         Thread thread = new Thread();179.         thread.run();180.         thread.start();181.     }

182. }a. Compilation fails.b. It compiles and runs fine.c. An IllegalThreadStateException is thrown at runtime.

183. What happens when this code gets compiled and executed? (1 correct answer) 184. public class Run {185.     public static void main(String[] args) {186.         System.out.print("A ");187.         new Thread(new Thread(new

Thread())).start();188.         System.out.println("B");189.     }190. }

a. It prints “A B”.b. Compilation fails.c. It prints “A ” and an exception is thrown.

191. What happens when this code gets compiled and executed? (1 correct answer) 192. public class Run {193.     public static void main(String[] args) {194.         System.out.print("A ");195.         new Thread(new Thread(new

Thread())).run();196.         System.out.println("B");197.     }198. }

a. It prints “A B”.b. Compilation fails.c. It prints “A ” and an exception is thrown.

199. What happens when this code gets compiled and executed? (1 correct answer) 200. public class Run {201. public static void main(String[] args) {202.     Thread thread = new Thread() {203.         public void run() {204.             System.out.println("Hello!");205.         }206.     };207.     thread.start();208. }209. }

a. It prints “Hello!”.b. Compilation fails.

210. What happens when this code gets compiled and executed? (1 correct answer) 211. public class Run {212. public static void main(String[] args) {213.     new Thread() {214.         public void run() {215.             System.out.println("Hello!");

216.         }217.     }.start();218. }219. }

a. It prints “Hello!”.b. Compilation fails.

220. What happens when this code gets compiled and executed? (1 correct answer) 221. public class Run {222. public static void main(String[] args) {223.     new Runnable() {224.         public void run() {225.             System.out.println("Hello!");226.         }227.     }.start();228. }229. }

a. It prints “Hello!”.b. Compilation fails.

230. What happens when this code gets compiled and executed? (1 correct answer) 231. public class Run {232. public static void main(String[] args) {233.     new Thread(new Runnable() {234.         public void run() {235.             System.out.println("Hello!");236.         }237.     }).start();238. }239. }

a. It prints “Hello!”.b. Compilation fails.

240. These classes are defined in the same file. What is the output? (1 correct answer) 241. class Job implements Runnable {242.     public void run() {243.         System.out.println("Working...");244.     }245. }246. public class My {247.     public static void main(String[] args) {248.         Thread thread1 = new Thread(new

Job());249.         Thread thread2 = new Thread(new

Job());250.         thread1.start();251.         thread2.start();252.     }253. }

a. Compilation fails.b. It prints “Working…” three times.c. It prints “Working…” one time and then an exception is thrown.

254. These classes are defined in the same file. What is the output? (1 correct answer) 255. class Job implements Runnable {256.     public void run() {257.         System.out.println("Working...");258.     }259. }260. public class My {261.     public static void main(String[] args) {262.         Job job = new Job();263.         Thread thread1 = new Thread(job);264.         Thread thread2 = new Thread(job);265.         thread1.start();266.         thread2.start();267.     }268. }

a. Compilation fails.b. It prints “Working…” three times.c. It prints “Working…” one time and then an exception is thrown.

269. What is the output? (1 correct answer) 270. public class Test extends Thread {271.     public void run() {272.         System.out.println(isAlive());273.     }274.     public static void main(String[] args) {275.         Test test = new Test();276.         System.out.println(test.isAlive());277.         test.start();278.     }279. }

a. false falseb. false truec. true falsed. true true

280. What is the output? (1 correct answer) 281. public class Test extends Thread {282.     public void run() {283.         System.out.println(isAlive());284.     }285.     public static void main(String[] args) {286.         Test test = new Test();287.         System.out.println(test.isAlive());288.         test.run();289.     }290. }

a. false falseb. false truec. true falsed. true true

291. What is the output? (1 correct answer) 292. public class Test extends Thread {293.     public Test() {294.         System.out.println(isAlive());295.     }296.     public static void main(String[] args) {297.         new Test();298.     }299. }

a. falseb. true

300. What is the output? (1 correct answer) 301. public class Test extends Thread {302.     public Test() {303.         System.out.println(isAlive());304.     }305.     public static void main(String[] args) {306.         new Test().run();307.     }308. }

a. falseb. true

309. What is the output? (1 correct answer) 310. public class Test extends Thread {311.     public Test() {312.         System.out.println(isAlive());313.     }314.     public static void main(String[] args) {315.         new Test().start();316.     }317. }

a. falseb. true

318. Is this class thread-safe? In other words, is it carefully designed to protect its state from concurrent access? (1 correct answer)

319. public class Account {320.     private Integer number = 0;321.     synchronized public void

setNumber(Integer number) {322.         this.number = number;323.     }324.     synchronized public Integer getNumber()

{

325.         return number;326.     }327. }

a. Yes.b. No.

328. Is this class thread-safe? In other words, is it carefully designed to protect its state from concurrent access? (1 correct answer)

329. public class Account {330.     private Integer number = 0;331.     public void setNumber(Integer number) {332.         synchronized (this) {333.             this.number = number;334.         }335.     }336.     synchronized public Integer getNumber()

{337.         return number;338.     }339. }

a. Yes.b. No.

340. Is this class thread-safe? In other words, is it carefully designed to protect its state from concurrent access? (1 correct answer)

341. public class Account {342.     private Integer number = 0;343.     public void setNumber(Integer number) {344.         synchronized (this) {345.             this.number = number;346.         }347.     }348.     public Integer getNumber() {349.         synchronized (this) {350.             return number;351.         }352.     }353. }

a. Yes.b. No.

354. 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.

355. class Account {356.     private Integer number = 0;357.     public synchronized void

setNumber(Integer number) {358.         this.number = number;359.     }

360.     public synchronized Integer getNumber() {

361.         return number;362.     }363. }364.365. class Client extends Thread {366.     Account account;367.     public Client(Account account) {368.         this.account = account;369.     }370.     public void run() {371.         for (int i = 1; i <= 1000; i++) {372.             account.setNumber(account.getNum

ber() + 1);373.374.         }375.     }376. }377.378. public class Run {379.     public static void main(String[] args)

throws Exception {380.         Account account = new Account();381.         Client one = new Client(account);382.         Client two = new Client(account);383.         one.start();384.         two.start();385.         one.join();386.         two.join();387.         // here388.     }389. }

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

a. trueb. false

390. These classes are defined in a single file. 391. class Account {392.     private Integer number = 0;393.     public synchronized void increase() {394.         number++;395.     }

396.     public synchronized Integer getNumber() {

397.         return number;398.     }399. }400.401. class Client extends Thread {402.     Account account;403.     public Client(Account account) {404.         this.account = account;405.     }406.     public void run() {407.         for (int i = 1; i <= 1000; i++) {408.             account.increase();409.         }410.     }411. }412.413. public class Run {414.     public static void main(String[] args)

throws Exception {415.         Account account = new Account();416.         Client one = new Client(account);417.         Client two = new Client(account);418.         one.start();419.         two.start();420.         one.join();421.         two.join();422.         // here423.     }424. }

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

a. trueb. false

425. These classes are defined in a single file. 426. class Account {427.     private Integer number = 0;428.     public void increase() {429.         number++;430.     }431.     public synchronized Integer getNumber()

{432.         return number;

433.     }434. }435.436. class Client extends Thread {437.     Account account;438.     public Client(Account account) {439.         this.account = account;440.     }441.     public void run() {442.         for (int i = 1; i <= 1000; i++) {443.             account.increase();444.         }445.     }446. }447.448. public class Run {449.     public static void main(String[] args)

throws Exception {450.         Account account = new Account();451.         Client one = new Client(account);452.         Client two = new Client(account);453.         one.start();454.         two.start();455.         one.join();456.         two.join();457.         // here458.     }459. }

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

a. trueb. false

460. What happens when this code gets compiled and executed? (1 correct answer) 461. public class Test {462.     public static void main(String[] args) {463.         String message = "Hello!";464.         message.wait(); // 1465.         System.out.println(message);466.     }467. }

a. Compilation fails.b. It prints “Hello!”.c. An IllegalMonitorStateException is thrown at runtime.d. Nothing gets printed, as the main thread just waits at line 1.

468. What happens when this code gets compiled and executed? (1 correct answer) 469. public class Test {470.     public static void main(String[] args) {471.         String message = "Hello!";472.         try {473.             message.wait();474.         } catch (InterruptedException e) {475.         }476.         System.out.println(message);477.     }478. }

a. Compilation fails.b. It prints “Hello!”.c. An IllegalMonitorStateException is thrown at runtime.d. Nothing gets printed, as the main thread just waits at line 1.

479. What happens when this code gets compiled and executed? (1 correct answer) 480. public class Test {481. public static void main(String[] args) {482.     String message = "Hello!";483.     synchronized (args) {484.         try {485.             message.wait();486.         } catch (InterruptedException e) {487.         }488.     }489.     System.out.println(message);490. }491. }

a. Compilation fails.b. It prints “Hello!”.c. An IllegalMonitorStateException is thrown at runtime.d. Nothing gets printed, as the main thread just waits at line 1.

492. What happens when this code gets compiled and executed? (1 correct answer) 493. public class Test {494. public static void main(String[] args) {495.     String message = "Hello!";496.     synchronized (message) {497.         try {498.             message.wait();499.         } catch (InterruptedException e) {500.         }501.     }502.     System.out.println(message);503. }504. }

a. Compilation fails.b. It prints “Hello!”.

c. An IllegalMonitorStateException is thrown at runtime.d. Nothing gets printed, as the main thread just waits at line 1.

505. These classes are defined in the same file. What is the output? (1 correct answer) 506. class Job extends Thread {507.     private Integer number = 0;508.     public void run() {509.         for (int i = 1; i < 1000000; i++) {510.             number++;511.         }512.     }513.     public Integer getNumber() {514.         return number;515.     }516. }517. public class Test {518.     public static void main(String[] args) {519.         Job thread = new Job();520.         thread.start();521.         System.out.println(thread.getNumber(

));522.     }523. }

a. It prints 0.b. It prints 999999.c. The output is not guaranteed to be any of the above.

524. These classes are defined in the same file. What is the output? (1 correct answer) 525. class Job extends Thread {526.     private Integer number = 0;527.     public void run() {528.         for (int i = 1; i < 1000000; i++) {529.             number++;530.         }531.     }532.     public Integer getNumber() {533.         return number;534.     }535. }536. public class Test {537.     public static void main(String[] args)538.     throws InterruptedException {539.         Job thread = new Job();540.         thread.start();541.         synchronized (thread) {542.             thread.wait();543.         }

544.         System.out.println(thread.getNumber());

545.     }546. }

a. It prints 0.b. It prints 999999.c. The output is not guaranteed to be any of the above.

547. These classes are defined in the same file. What is the output? (1 correct answer) 548. class Job extends Thread {549. private Integer number = 0;550.     public void run() {551.     synchronized (this) {552.         for (int i = 1; i < 1000000; i++) {553.             number++;554.         }555.         notify();556.     }557.     }558.     public Integer getNumber() {559.         return number;560.     }561. }562. public class Test {563.     public static void main(String[] args)

throws Exception {564.         Job thread = new Job();565.         thread.start();566.         synchronized (thread) {567.568.             thread.wait();569.         }570.         System.out.println(thread.getNumber(

));571.     }572. }

a. It prints 0.b. It prints 999999.c. The output is not guaranteed to be any of the above.

573. These classes are defined in the same file. What is the output? (1 correct answer) 574. class Job extends Thread {575.     private Integer number = 0;576.     public void run() {577.         for (int i = 1; i < 1000000; i++) {578.             number++;579.         }580.     }

581.     public Integer getNumber() {582.         return number;583.     }584. }585.586. public class Test {587.     public static void main(String[] args)

throws Exception {588.         Job thread = new Job();589.         thread.start();590.         thread.join();591.         System.out.println(thread.getNumber(

));592.     }593. }

a. It prints 0.b. It prints 999999.c. The output is not guaranteed to be any of the above.

594. What happens when this code gets compiled and executed? (1 correct answer) 595. public class Run {596.     public static void main(String[] args) {597.         System.out.print("A ");598.         notifyAll();599.         System.out.println("B");600.     }601. }

a. It prints “A B”.b. Compilation fails.c. It prints “A ” and an exception is thrown.

602. What happens when this code gets compiled and executed? (1 correct answer) 603. public class Run {604.     public static void main(String[] args) {605.         System.out.print("A ");606.         new Object().notifyAll();607.         System.out.println("B");608.     }609. }

a. It prints “A B”.b. Compilation fails.c. It prints “A ” and an exception is thrown.

610. What happens when this code gets compiled and executed? (1 correct answer) 611. public class Run {612.     public static void main(String[] args) {613.         System.out.print("A ");614.         synchronized (new Object()) {615.             new Object().notifyAll();616.         }

617.         System.out.println("B");618.     }619. }

a. It prints “A B”.b. Compilation fails.c. It prints “A ” and an exception is thrown.

620. What happens when this code gets compiled and executed? (1 correct answer) 621. public class Run {622.     public static void main(String[] args) {623.         System.out.print("A ");624.         final Object test = new Object();625.         synchronized (test) {626.             test.notifyAll();627.         }628.         System.out.println("B");629.     }630. }

a. It prints “A B”.b. Compilation fails.c. 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. a2. a3. a4. a5. a6. d7. c8. b9. a10. c11. a12. a13. a14. b15. c16. a17. a18. b19. b20. b21. a22. a23. a

24. a25. a26. a27. a28. a29. b30. b31. a32. c33. b34. b35. b36. b37. b38. c39. b40. c41. b42. a43. a44. a45. a46. b47. a48. b49. b50. b51. a52. a53. a54. a55. a56. a57. a58. b59. a60. b61. a62. c63. c64. d65. c66. b67. b68. b69. b70. c71. c72. 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.

Share this:

Email Digg Reddit Facebook StumbleUpon Twitter

Like this:

Like

Be the first to like this post.

This entry was posted on Monday, September 8th, 2008 at 11:25 pm and is filed under SCJP 6, Training. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Post navigation

« Previous Post Next Post »

18 Responses to SCJP 6 Mock exam for Threads

1. Nikos says:

11 October 2008 at 12:08 am

Thanks to Maggie Zhou for her corrections on questions 42 and 43!

2. Garima says:

6 November 2008 at 4:04 am

For the question no 59, answer should be b i.e. false. I ran and checked.

3. Nikos says:

7 November 2008 at 1:09 am

Hi Garima, now questions 58, 59 and 60 are cut and clear! Just provide the account a print method and check it again! Thanks.

4. Sexualxe says:

7 November 2008 at 5:25 pm

Hello you forum best. And Bye. just test. . :)

5. Orsi says:

2 December 2008 at 1:17 pm

Dear Nikos,

I do not understand why example 66 works the way it does. I have tried and it really prints what you say it does, but why, when nobody calls notify()? Also, if I add a Thread.sleep(1000) before entering the synchronized block for waiting, it hangs. What’s the _important_ difference between the two cases? I kind of think that if something does not work if I sleep in it a little, then it is not certain to work at all.

Similar question for example 67: it hangs if I add a Thread.sleep() before entering the synch block containing the wait call. Which is logical, because the notify() happens before the wait is entered. Then why can we say that without the sleep it certainly works? Does it not depend on the scheduler?

Thanks in advance!

6. mashimaro says:

29 December 2008 at 5:41 pm

hi, I ran question 48.“Working…” is only printed twice? not three times?

btw, thanks for your mock exams!

7. Free SCJP Mock exams « Niko’s java blog says:

30 December 2008 at 7:46 pm

[...] Free SCJP Mock exam for Threads [...]

8. Aruna says:

29 January 2009 at 9:47 pm

Very Good Practice Material.Thanks A lot.

9. kalyan says:

12 March 2009 at 6:52 am

Hi can you please explain questions 58,60,66,67 ?You are right..but why..i couldn’t infer….

The remaining questions are great for self test in Threads.Thanks

10. Michał B says:

27 March 2009 at 1:24 pm

Hi all.I would like to explain in short some questions:q58 – you have 2 independent methods “setNumber” and “getNumber”, two threads cannot get into one of them in the same time becouse of synchronization, this is clear, but line “account.setNumber(account.getNumber() + 1);” is unsafe becouse two threads may read the same number (getNumber()) (one thread get into method, read value and release lock, then second thread get into the same method and read the same value) then both tread wants to set their values (setNumber), so You dont have quarantee that the sequence will be: getNumber, setNumber, getNumber, setNumber… it can be: getNumber, getNumber, setNumber,setNumber

q60: – in this example You have quarantee sequence, read and set value (increment)

q66: – when the thread is dead (ended run method) it release lock (if thread end his work before main thread run wait method program will be wait for all)

g67: – similar to above example, when thread run notify method and go out from synchronized block main thread wake up, but You have to know that You imply that the thread will finish his work after main method run wait method on thread object.

I hope this help somone. Best regards

11. Cris says:

9 November 2009 at 9:37 pm

Hi

First of all – good job.

Look again at question 8 – I believe 3 methods (not 2) are static there.

12. lavanya says:

3 February 2010 at 9:28 pm

one of the excellent site for scjp aspirants….!!!!!!!!!!!!!!!!!!!!!!!!!!!1

13. ankur garg says:

25 October 2010 at 10:25 pm

Hello NikoCan you please explain output of Q 66?

14. Laura says:

27 October 2010 at 11:04 pm

Hey! Thanks for these questions, they are really useful!But I have a doubt regarding to questions number 24 and 66, though I checked them and the answer for both are correct, I can’t get how they work the way they do ..If someone is willing to give a hand here please, I’d really appreciate it Thanks again!

15. coder89 says:

4 January 2011 at 1:59 pm

Michał B: q60 is correct because increment/decrement operation is NOT an atomic operation and it takes more than 1 processor cycles.

However questions 66 & 67 ale incorrect because if you have any processes running on your machine which slow down your machine it may cause that scheduler switch threads before main thread lock thread object and than main thread will wait until run method will end. (the effect will be the same if you call Thread.sleep() method between Thread.start() and synchronized block) In that case the program may hang on in Thread.wait() method.

16. Jurica says:

30 January 2011 at 1:32 pm

Shouldn’t the answer number 2 in question 48 be “It prints “Working…” two times.” instead of “It prints “Working…” three times.” ?

17. Anup Sharma says:

27 August 2011 at 1:30 am

I hope it will help …

18. Anup Sharma says:

27 August 2011 at 1:35 am

jst wait and watch

Leave a Reply

Enter your comment here...

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

Email (required) (Address never made public)

Name (required)

Website

Notify me of follow-up comments via email.

Nikos Pougounias is a senior Analyst-Programmer specialized in Java Enterprise Edition and Object Oriented Design. During the past years, he has been contributing to major portfolios under the European Commission.

Search

Visitorso 165,279 hits

Top posts (24h)o Free SCJP Mock exams o SCJP 6 Mock exam for I/O o SCJP Mock exam for Overriding & Overloading

Categorieso Application Server (3) o Database (6) o JavaFX (2) o JDBC (4) o JPA (4) o JSP/Servlets (8) o Quick Tutorials (39) o RMI (1) o Ruby (3) o SCJP 6 (7) o SCWCD (9) o Training (20) o Web Services (3) o Wicket (12) o XML (4)

Archiveso August 2011 o July 2010 o March 2010 o August 2009 o May 2009

o March 2009 o February 2009 o January 2009 o November 2008 o October 2008 o September 2008 o August 2008 o July 2008 o June 2008 o May 2008 o March 2008

Basis data certification Custom Tags Database dom4j Eclipse Glassfish Hibernate J2EE

tutorial Java Java EE Java EE tutorial JavaFX Java Persistence API JAX-WS JAXB JBoss JDBC JDeveloper

JPA JPA 2 JSP JWS Music MySQL NetBeans object-relational mapping OCPJP OCPJWCD OpenSolaris Oracle

RMI Ruby SCJP SCWCD Spring Spring 3 SQL Sun Web Web Services Wicket XML Βάση

δεδομένων

Theme: Contempt by Vault9.Blog at WordPress.com.

Follow

Follow “Nikos' Java blog”

Get every new post delivered to your Inbox.

Powered by WordPress.com

Nikos' Java blogquick & easy Java tutorials

Home

SCJP Mock exam for Generics

Enter your

29 Votes

A collection of structured questions for Generics, under the scope of Sun Certified Java Programmer for Java SE 6.

If you are busy, just go through questions 1-40.

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) 2.List<Number> list1 = null;3.List<Integer> list2 = null;4.list1 = list2;

a. Yes.b. No.

5. Will this code compile successfully? (1 correct answer) 6.List<Number> list1 = null;7.List<Integer> list2 = null;8.list2 = list1;

a. Yes.b. No.

9. Will this code compile successfully? (1 correct answer) 10. List<? extends Number> list1 = null;11. List<Integer> list2 = null;12. list1 = list2;

a. Yes.b. No.

13. Will this code compile successfully? (1 correct answer) 14. List<? extends Number> list1 = null;15. List<Integer> list2 = null;16. list2 = list1;

a. Yes.b. No.

17. Will this code compile successfully? (1 correct answer) 18. List<Number> list1 = null;

19. List<? super Integer> list2 = null;20. list1 = list2;

a. Yes.b. No.

21. Will this code compile successfully? (1 correct answer) 22. List<Number> list1 = null;23. List<? super Integer> list2 = null;24. list2 = list1;

a. Yes.b. No.

25. Will this code compile successfully? (1 correct answer) 26. SortedSet<? super Number> set1 = null;27. SortedSet<Integer> set2 = null;28. set1 = set2;

a. Yes.b. No.

29. Will this code compile successfully? (1 correct answer) 30. SortedSet<? super Number> set1 = null;31. SortedSet<Integer> set2 = null;32. set2 = set1;

a. Yes.b. No.

33. Will this code compile successfully? (1 correct answer) 34. SortedSet<Number> set1 = null;35. SortedSet<? extends Integer> set2 = null;36. set1 = set2;

a. Yes.b. No.

37. Will this code compile successfully? (1 correct answer) 38. SortedSet<Number> set1 = null;39. SortedSet<? extends Integer> set2 = null;40. set2 = set1;

a. Yes.b. No.

41. Will this code compile successfully? (1 correct answer) 42. Queue<? extends Number> q1 = null;43. Queue<? super Integer> q2 = null;44. q1 = q2;

a. Yes.b. No.

45. Will this code compile successfully? (1 correct answer) 46. Queue<? extends Number> q1 = null;47. Queue<? super Integer> q2 = null;48. q2 = q1;

a. Yes.b. No.

49. Will this code compile successfully? (1 correct answer) 50. Queue<? super Number> q1 = null;

51. Queue<? extends Integer> q2 = null;52. q1 = q2;

a. Yes.b. No.

53. Will this code compile successfully? (1 correct answer) 54. Queue<? super Number> q1 = null;55. Queue<? extends Integer> q2 = null;56. q2 = q1;

a. Yes.b. No.

57. Will this code compile successfully? (1 correct answer) 58. Queue<?> q1 = null;59. Queue<Integer> q2 = null;60. q1 = q2;

a. Yes.b. No.

61. Will this code compile successfully? (1 correct answer) 62. LinkedList<?> list1 = null;63. LinkedList<Integer> list2 = null;64. list2 = list1;

a. Yes.b. No.

65. Will this code compile successfully? (1 correct answer) 66. LinkedList<?> list1 = null;67. LinkedList<? extends Integer> list2 = null;68. list1 = list2;

a. Yes.b. No.

69. Will this code compile successfully? (1 correct answer) 70. LinkedList<?> list1 = null;71. LinkedList<? extends Integer> list2 = null;72. list2 = list1;

a. Yes.b. No.

73. Will this code compile successfully? (1 correct answer) 74. LinkedList<?> list1 = null;75. LinkedList<? super Integer> list2 = null;76. list1 = list2;

a. Yes.b. No.

77. Will this code compile successfully? (1 correct answer) 78. LinkedList<?> list1 = null;79. LinkedList<? super Integer> list2 = null;80. list2 = list1;

a. Yes.b. No.

81. Will this code compile successfully? (1 correct answer) 82. PriorityQueue queue1 = null;

83. PriorityQueue<Integer> queue2 = null;84. queue1 = queue2;

a. Yes, without warnings.b. Yes, with a warning.c. No.

85. Will this code compile successfully? (1 correct answer) 86. PriorityQueue queue1 = null;87. PriorityQueue<Integer> queue2 = null;88. queue2 = queue1;

a. Yes, without warnings.b. Yes, with a warning.c. No.

89. Will this code compile successfully? (1 correct answer) 90. PriorityQueue<?> queue1 = null;91. PriorityQueue queue2 = null;92. queue1 = queue2;

a. Yes, without warnings.b. Yes, with a warning.c. No.

93. Will this code compile successfully? (1 correct answer) 94. PriorityQueue<?> queue1 = null;95. PriorityQueue queue2 = null;96. queue1 = queue2;

a. Yes, without warnings.b. Yes, with a warning.c. No.

97. Will this code compile successfully? (1 correct answer) 98. Set<Integer> set = new TreeSet<Integer>();99. set.add(10);

a. Yes.b. No.

100. Will this code compile successfully? (1 correct answer) 101. Set<Integer> set = new TreeSet<Integer>();102. set.add((int)1.0f);

a. Yes.b. No.

103. Will this code compile successfully? (1 correct answer) 104. Set<Integer> set = new TreeSet<Integer>();105. set.add(10L);

a. Yes.b. No.

106. Will this code compile successfully? (1 correct answer) 107. Set<Integer> set = new TreeSet<Integer>();108. int number = (short)10;109. set.add(number);

a. Yes.b. No.

110. Will this code compile successfully? (1 correct answer) 111. Set<Number> set = new TreeSet<Integer>();

112. set.add(10L);a. Yes.b. No.

113. Will this code compile successfully? (1 correct answer) 114. NavigableSet<?> set = new TreeSet<Object>();115. set.add(new Object());

a. Yes.b. No.

116. Will this code compile successfully? (1 correct answer) 117. NavigableSet<? super Object> set = new

TreeSet<Object>();118. set.add(new Object());

a. Yes.b. No.

119. Will this code compile successfully? (1 correct answer) 120. NavigableSet<? extends Object> set = new

TreeSet<Object>();121. set.add(new Object());

a. Yes.b. No.

122. Will this code compile successfully? (1 correct answer) 123. NavigableSet<? extends String> set = new

TreeSet<String>();124. set.add("string");

a. Yes.b. No.

125. Will this code compile successfully? (1 correct answer) 126. NavigableSet<? super String> set = new

TreeSet<String>();127. set.add("string");

a. Yes.b. No.

128. Will this code compile successfully? (1 correct answer) 129. NavigableSet<? super String> set = new

TreeSet<String>();130. set.add(new Object());

a. Yes.b. No.

131. Will this code compile successfully? (1 correct answer) 132. List<? extends Integer> list = new

ArrayList<Integer>();133. for (Integer element : list) {134. System.out.println(element);135. }

a. Yes.b. No.

136. Will this code compile successfully? (1 correct answer)

137. List<? extends Integer> list = new ArrayList<Integer>();

138. Integer first = list.get(0);a. Yes.b. No.

139. Will this code compile successfully? (1 correct answer) 140. List<? super Integer> list = new

ArrayList<Integer>();141. for (Integer element : list) {142. System.out.println(element);143. }

a. Yes.b. No.

144. Will this code compile successfully? (1 correct answer) 145. List<? super Integer> list = new

ArrayList<Integer>();146. Integer first = list.get(0);

a. Yes.b. No.

147. Will this code compile successfully? (1 correct answer) 148. List<? super Integer> list = new

ArrayList<Integer>();149. Object first = list.get(0);

a. Yes.b. No.

150. Will this code compile successfully? (1 correct answer) 151. import java.util.*;152.153. class Test {154.     void say(Set<Double> set) {155.     }156.     void say(SortedSet<Double> set) {157.     }158. }

a. Yes.b. No.

159. Will this code compile successfully? (1 correct answer) 160. import java.util.*;161.162. class Test {163.     void say(Set<Double> set) {164.     }165.     void say(Set<Boolean> set) {166.     }167. }

a. Yes.b. No.

168. Will this code compile successfully? (1 correct answer) 169. import java.util.*;170.171. class Test {172.     void say(Set<Double> set) {173.     }174.     void say(Set<Double>... set) {175.     }176. }

a. Yes.b. No.

177. Consider these classes. 178. import java.util.*;179.180. class Parent {181. void say(List<String> list) {182. System.out.println("parent");183. }184. }185. class Child extends Parent {186. void say(List list) {187. System.out.println("child");188. }189. }

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>());

}

a. It prints “child”.b. It prints “parent”.c. Compilation fails.

190. Consider these classes. 191. import java.util.*;192.193. class Parent {194. void say(List<String> list) {195. System.out.println("parent");196. }197. }198. class Child extends Parent {199. void say(List list) {200. System.out.println("child");

201. }202. }

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>>());

}

a. It prints “child”.b. It prints “parent”.c. Compilation fails.

203. Consider these classes. 204. import java.util.*;205.206. class Parent {207. void say(List<String> list) {208. System.out.println("parent");209. }210. }211. class Child extends Parent {212. void say(List list) {213. System.out.println("child");214. }215. }

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>());

}

a. It prints “child”.b. It prints “parent”.c. Compilation fails.

216. Consider these classes. 217. import java.util.*;218.219. class Parent {220. void say(List<String> list) {221. System.out.println("parent");222. }223. }224. class Child extends Parent {225. void say(List list) {

226. System.out.println("child");227. }228. }

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>());

}

a. It prints “child”.b. It prints “parent”.c. Compilation fails.

229. Will this code compile successfully? (1 correct answer) 230. import java.util.*;231.232. class Parent {233. void say(List<String> list) {234. System.out.println("parent");235. }236. }237. class Child extends Parent {238. void say(List<Integer> list) {239. System.out.println("child");240. }241. }

a. Yes.b. No.

242. Will this code compile successfully? (1 correct answer) 243. import java.util.*;244.245. class Parent {246. void say(List<? extends Number> list)

{247. System.out.println("parent");248. }249. }250. class Child extends Parent {251. void say(List<Integer> list) {252. System.out.println("child");253. }254. }

a. Yes.b. No.

255. Will this code compile successfully? (1 correct answer) 256. import java.util.*;

257.258. class Parent {259. void say(List list) {260. System.out.println("parent");261. }262. }263. class Child extends Parent {264. void say(List<Integer> list) {265. System.out.println("child");266. }267. }

a. Yes.b. No.

268. Will this code compile successfully? (1 correct answer) 269. Object set = new TreeSet<Integer>();270. boolean flag = set instanceof

NavigableSet<Integer>;a. Yes.b. No.

271. What is the output of the following code? (1 correct answer) 272. List<? extends String> list1 = new

ArrayList<String>();273. List<? super String> list2 = new

ArrayList<String>();274. List<Integer> list3 = new

ArrayList<Integer>();275. List list4 = new ArrayList();276.277. if (list1 instanceof List &&278.     list2 instanceof List &&279.     list3 instanceof List &&280.     list4 instanceof List) {281.     System.out.println("yes");282. }

a. It prints “yes”.b. It prints nothing.

283. Will this code compile successfully? (1 correct answer) 284. Class c = ArrayList<Integer>.class;

a. Yes.b. No.

285. Will this code compile successfully? (1 correct answer) 286. Class c = new

ArrayList<Integer>().getClass();a. Yes.b. No.

287. Will this code compile successfully? (1 correct answer) 288. new ArrayList<?>();

a. Yes.b. No.

289. Will this code compile successfully? (1 correct answer) 290. new TreeMap<String, ? super Integer>();

a. Yes.b. No.

291. Will this code compile successfully? (1 correct answer) 292. new ArrayList<Set<?>>();

a. Yes.b. No.

293. Will this code compile successfully? (1 correct answer) 294. class Test extends ArrayList<? extends

Number> {295. }

a. Yes.b. No.

296. Will this code compile successfully? (1 correct answer) 297. class Test implements Comparable<?> {298.     public int compareTo(Comparable<?>

object) {299.         return 0;300.     }301. }

a. Yes.b. No.

302. Will this code compile successfully? (1 correct answer) 303. class Test implements

Comparable<Comparable<?>> {304.     public int compareTo(Comparable<?>

object) {305.         return 0;306.     }307. }

a. Yes.b. No.

308. Will this code compile successfully? (1 correct answer) 309. class Test {310. <T> T getFirst(List<T> list) {311.     return list.get(0);312. }313. }

a. Yes.b. No.

314. Will this code compile successfully? (1 correct answer) 315. class Test {316. static <T> T getFirst(List<T> list) {317.     return list.get(0);318. }

319. }a. Yes.b. No.

320. Will this code compile successfully? (1 correct answer) 321. class Test <T> {322. T getFirst(List<T> list) {323.     return list.get(0);324. }325. }

a. Yes.b. No.

326. Will this code compile successfully? (1 correct answer) 327. class Test <T> {328. static T getFirst(List<T> list) {329.     return list.get(0);330. }331. }

a. Yes.b. No.

332. Will this code compile successfully? (1 correct answer) 333. class Test <T> {334. T instance;335. Test(T instance) {336.     this.instance = instance;337. }338. }

a. Yes.b. No.

339. Will this code compile successfully? (1 correct answer) 340. class Test <T> {341. Test(T my) {342.     boolean b = (my instanceof T);343. }344. }

a. Yes.b. No.

345. Will this code compile successfully? (1 correct answer) 346. class Test <T> {347. void test(T method) {348.     Object my = (T)method;349. }350. }

a. Yes.b. No.

351. Will this code compile successfully? (1 correct answer) 352. class Test <T> {353. void test() {

354.     NavigableMap map = new TreeMap<String, T>();

355. }356. }

a. Yes.b. No.

357. Will this code compile successfully? (1 correct answer) 358. class Test <T> {359. private T[] array = null;360. }

a. Yes.b. No.

361. Will this code compile successfully? (1 correct answer) 362. class Test <T> {363. private T[] array = new T[7];364. }

a. Yes.b. No.

365. Will this code compile successfully? (1 correct answer) 366. class Test<String> {367.     String my = "Hello!";368. }

a. Yes.b. No.

369. Will this code compile successfully? (1 correct answer) 370. class Test<String> {371.     String my;372.     public Test(String my) {373.         this.my = my;374.     }375.     public String get() {376.         return my;377.     }378. }379.380. public class RunTest {381.     public static void main(String[] args) {382.         Integer i = new

Test<Integer>(1).get();383.         System.out.println(i.getClass());384.     }385. }

a. Yes.b. No.

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

Answers

1. b2. b3. a4. b5. b6. a7. b8. b9. b10. b11. b12. b13. b14. b15. a16. b17. a18. b19. a20. b21. a22. b23. a24. a25. a26. a27. b28. a29. b30. b31. a32. b33. b34. a35. b36. a37. a38. b39. b40. a41. a42. b43. a44. a45. a46. a47. c48. b49. b

50. b51. b52. a53. b54. a55. b56. b57. a58. b59. b60. a61. a62. a63. a64. b65. a66. b67. a68. a69. a70. b71. b72. a

Share this:

Email Digg Reddit Facebook StumbleUpon Twitter

Like this:

Like

Be the first to like this post.

This entry was posted on Thursday, October 9th, 2008 at 8:32 pm and is filed under SCJP 6, Training. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Post navigation

« Previous Post Next Post »

20 Responses to SCJP Mock exam for Generics

1. Free SCJP Mock exams « Niko’s java blog says:

30 December 2008 at 7:47 pm

[...] java blog quick & easy Java tutorials « SCJP Mock exam for Generics JBoss integration with JDeveloper 11g [...]

2. Aruna says:

2 February 2009 at 11:39 am

Hii Niko This an Excellent Practice Stuff.it’s helped me a lot in grasping the Concepts of Generics fully.Thnaks A Lot.

3. JacQ says:

9 March 2009 at 4:02 pm

Thx ! nice one

4. himalay says:

11 March 2009 at 3:50 pm

kudos to you

5. Ahsan Jamshaid says:

23 March 2009 at 1:24 pm

Hi Niko,this is really excellent stuff. i liked it and i really appreciate your effort.

Thanks Buddy!..

6. Paolo says:

10 April 2009 at 11:06 am

Hi, thanks for this!

Questions 23 and 24 and respective answers are identical. Is it intended?

7. ajith says:

12 April 2009 at 8:27 am

hi Niko,,,thanks a lot …. i really enjoyed with these questions …well done

8. unexpected compilation error with Generics : java cms says:

15 April 2009 at 6:11 am

[...] This is from the practice exam for Generics on > NikoJava [...]

9. The true about the question 72?? says:

24 October 2009 at 1:55 am

Hi everyone

In question 72 the correct answer is b), i don’t know the exact reason; I supposed is a name colide because i tried to compile and the result is:

non-static class String cannot be referenced from a static context

Thanks for your time

10. Shan says:

23 February 2010 at 8:33 pm

Hi Nikos,Thanks for this wonderful effort.To add cream on top of this, if you could display the rules/logics/traps that you are testing in your questions, it would help us a lot in revising the concepts.

11. Tatiana says:

5 May 2010 at 8:35 am

48, 49 and 50 the answer is a! They compile just fine!

and about question 72, it compiles fine, so, it’s correct.

12. Mohamed Farouk says:

18 May 2010 at 5:39 pm

Hello GuysFinally cracked this all confusing generic collection type assignement. If you use this logic you can crack nicko questions on generics (1-24) without any problem as well understanding the concepts of generics assignments.

Solutions:Look at the assignments L1 = L2Read like thisI accept L1 = You have L2Question No Left hand side (I accept) Right Hand side is I have Can I accept Answer1 I accept Number I have Integer No No2 I accept Integer I have Number No No3 I accept subclasses of Number I have Integer Yes Yes4 I accept Integer I have any subclasses of Number No No5 I accept Number I have any super class of Integer (Number/Object) No No6 Any super classes of Integer (Number or Object) I have Number Yes Yes7 Any super classes of Number(Object) I have Integer No No8 Integer I have any super classes of Number(Object) No No9 Number Any subclasses of Integer No No10 Any subclass on Integer Number No No11 Any subclass of Number Super classes of Integer(Object/Number) No No12 Any super class of Integer(Number/Object) Subclasses of Number No No13 Only super classes of Number(Object) Integer No No14 Subclasses of Integer Any Super class of Number (Object) No No15 Anything Integer Yes Yes16 Integer Anything No No17 Anything Subclasses of Integer Yes Yes18 Subclasses of Integer Anything No No19 Anything Super class of Integer(Number/Object) Yes Yes20 Any super class of Integer(Number/Object) Anything No No21 PriorityQueue(non param read as anything) Integer Yes Yes22 Integer Anything No No23 Anything Anything Yes Yes24 Anything Anything Yes Yes

13. arjun says:

10 September 2010 at 12:41 am

48,49,50are compiler errorsplease if you could some explanation kind of thing or reasons for the answers,it will be great for some weird questions here.

14. Jean says:

3 August 2011 at 7:42 pm

Q64: it compiles fine

15. Shakthi balaji says:

8 August 2011 at 3:09 pm

Hi Nick,

This is simply great. You must have spent a lot of time over this to bring the questions in more organised fashion. Great work.

16. Balaji says:

9 August 2011 at 5:49 am

Hi Niko,One small question. The generics are used only during the compile time and not during the run time. Am i right? Ex – List will be just List during the runtime.

17. vipul reddy says:

16 December 2011 at 8:00 am

Hi Nikos Pougounias.

I am vipul Kumar. Thanks for providing the stuff on generics. I am preparing for scjp 1.6. The questions you have kept awared me of some topics i am not covered..

Thanks once again,Vipul Kumar

18. RaviTeja says:

3 January 2012 at 1:51 pm

whats wrong with 30th question??

Can anyone xplain why it can’t compile successfully

plz reply asap

19. RaviTeja says:

3 January 2012 at 2:01 pm

List list = new ArrayList();for (Integer element : list) {System.out.println(element);}

this should not compile. right??

but in 36th question the answer is YES..

can anyone explain please..

20. Hiral Jhaveri says:

5 January 2012 at 2:21 pm

This is good stuff.

Nice concepts.

Leave a Reply

Enter your comment here...

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

Email (required) (Address never made public)

Name (required)

Website

Notify me of follow-up comments via email.

Nikos Pougounias is a senior Analyst-Programmer specialized in Java Enterprise Edition and Object Oriented Design. During the past years, he has been contributing to major portfolios under the European Commission.

Search

Visitorso 165,279 hits

Top posts (24h)o Free SCJP Mock exams o SCJP 6 Mock exam for I/O o SCJP Mock exam for Overriding & Overloading

Categorieso Application Server (3) o Database (6) o JavaFX (2) o JDBC (4) o JPA (4) o JSP/Servlets (8) o Quick Tutorials (39) o RMI (1) o Ruby (3) o SCJP 6 (7) o SCWCD (9) o Training (20) o Web Services (3) o Wicket (12) o XML (4)

Archiveso August 2011 o July 2010 o March 2010

o August 2009 o May 2009 o March 2009 o February 2009 o January 2009 o November 2008 o October 2008 o September 2008 o August 2008 o July 2008 o June 2008 o May 2008 o March 2008

Basis data certification Custom Tags Database dom4j Eclipse Glassfish Hibernate J2EE

tutorial Java Java EE Java EE tutorial JavaFX Java Persistence API JAX-WS JAXB JBoss JDBC JDeveloper

JPA JPA 2 JSP JWS Music MySQL NetBeans object-relational mapping OCPJP OCPJWCD OpenSolaris Oracle

RMI Ruby SCJP SCWCD Spring Spring 3 SQL Sun Web Web Services Wicket XML Βάση

δεδομένων

Theme: Contempt by Vault9.Blog at WordPress.com.

Follow

Follow “Nikos' Java blog”

Get every new post delivered to your Inbox.

Powered by WordPress.com

Nikos' Java blogquick & easy Java tutorials

Home

SCJP Mock exam for Collections

Enter your

22 Votes

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

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

questions

1-16 SortedSet

17-30 NavigableSet

31-40 LinkedHashSet, HashSet, TreeSet

41-56 SortedMap

57-67 LinkedHashMap, HashMap, TreeMap

68-85 NavigableMap

86-110 PriorityQueue, Queue

111-130 List, LinkedList, ArrayList

1. SortedSet IS-A Set. (1 correct answer) a. trueb. false

2. SortedSet belongs to the java.util package. (1 correct answer) a. trueb. 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()

a. None.b. Two.c. Three.d. Five.

4. What’s the signature of the method headSet()? (1 correct answer)

a. SortedSet<E> headSet(E)b. SortedSet<E> headSet(E, E)c. SortedSet<E> headSet(E, boolean)

5. What happens when this code gets executed? (1 correct answer) 6.public static void main(String[] args) {7.    SortedSet<Integer> set = new

TreeSet<Integer>();8.    set.last();9.}

a. A NoSuchElementException is thrown.b. It compiles and runs fine.c. Compilation fails.

10. What happens when this code gets executed? (1 correct answer) 11. public static void main(String[] args) {12.     SortedSet<Integer> set = new

TreeSet<Integer>();13.     set.add(7);14.     set.add(-12);15.     SortedSet<Integer> sub = set.subSet(-100,

100);16.     System.out.format("%d %d", set.size(),

sub.size());17. }

a. It prints “2 2″.b. It prints “2 0″.

18. What happens when this code gets executed? (1 correct answer) 19. public static void main(String[] args) {20.     SortedSet<Integer> set = new

TreeSet<Integer>();21.     set.add(7);22.     set.add(-12);23.     SortedSet<Integer> sub = set.subSet(-100,

100);24.     sub.add(9);25.     System.out.format("%d %d", set.size(),

sub.size());26. }

a. An IllegalArgumentException is thrown.b. It prints “2 3″.c. It prints “2 1″.d. It prints “3 3″.

27. What happens when this code gets executed? (1 correct answer) 28. public static void main(String[] args) {29.     SortedSet<Integer> set = new

TreeSet<Integer>();30.     set.add(7);31.     set.add(-12);

32.     SortedSet<Integer> sub = set.subSet(-100, 100);

33.     sub.add(-100);34.     System.out.format("%d %d", set.size(),

sub.size());35. }

a. An IllegalArgumentException is thrown.b. It prints “2 3″.c. It prints “2 1″.d. It prints “3 3″.

36. What happens when this code gets executed? (1 correct answer) 37. public static void main(String[] args) {38.     SortedSet<Integer> set = new

TreeSet<Integer>();39.     set.add(7);40.     set.add(-12);41.     SortedSet<Integer> sub = set.subSet(-100,

100);42.     sub.add(100);43.     System.out.format("%d %d", set.size(),

sub.size());44. }

a. An IllegalArgumentException is thrown.b. It prints “2 3″.c. It prints “2 1″.d. It prints “3 3″.

45. What happens when this code gets executed? (1 correct answer) 46. public static void main(String[] args) {47.     SortedSet<Integer> set = new

TreeSet<Integer>();48.     set.add(7);49.     set.add(-12);50.     SortedSet<Integer> sub = set.subSet(-100,

100);51.     sub.add(99);52.     System.out.format("%d %d", set.size(),

sub.size());53. }

a. An IllegalArgumentException is thrown.b. It prints “2 3″.c. It prints “2 1″.d. It prints “3 3″.

54. What happens when this code gets executed? (1 correct answer) 55. public static void main(String[] args) {56.     SortedSet<Integer> set = new

TreeSet<Integer>();57.     set.add(7);

58.     set.add(-12);59.     SortedSet<Integer> sub = set.subSet(-100,

100);60.     sub.add(7);61.     System.out.format("%d %d", set.size(),

sub.size());62. }

a. An IllegalArgumentException is thrown.b. It prints “2 1″.c. It prints “2 2″.

63. What happens when this code gets compiled and executed? (1 correct answer) 64. public static void main(String[] args) {65.     SortedSet set = new TreeSet();66.     set.add(7);67.     set.add(-12);68.     SortedSet sub = set.subSet("Hello!", 100);69.     System.out.format("%d %d", set.size(),

sub.size());70. }

a. An IllegalArgumentException is thrown at runtime.b. A ClassCastException is thrown at runtime.c. Compilation fails.d. It prints “2 0″.e. It prints “2 2″.

71. What happens when this code gets compiled and executed? (1 correct answer) 72. public static void main(String[] args) {73.     SortedSet<Integer> set = new

TreeSet<Integer>();74.     set.add("Hello!");75. }

a. A ClassCastException is thrown at runtime.b. Compilation fails.c. None of the above.

76. What happens when this code gets compiled and executed? (1 correct answer) 77. public static void main(String[] args) {78.     SortedSet set = new TreeSet();79.     set.add("Hello!");80. }

a. A ClassCastException is thrown at runtime.b. Compilation fails.c. None of the above.

81. What happens when this code gets compiled and executed? (1 correct answer) 82. public static void main(String[] args) {83.     SortedSet set = new TreeSet();84.     set.add("Hello!");85.     set.add(1);86. }

a. A ClassCastException is thrown at runtime.b. Compilation fails.c. None of the above.

87. What happens when this code gets compiled and executed? (1 correct answer) 88. public static void main(String[] args) {89.     SortedSet set = new TreeSet();90.     set.add(null);91.     set.add(1);92. }

a. An NullPointerException is thrown at runtime.b. A ClassCastException is thrown at runtime.c. Compilation fails.d. None of the above.

93. NavigableSet extends SortedSet. (1 correct answer) a. trueb. false

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

a. None.b. Two.c. Four.d. All.

95. What’s the signature of the method lower()? (1 correct answer) a. E lower(E)b. boolean lower(E)c. E lower(NavigableSet<E>)

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

a. trueb. false

97. What is the output of this code? (1 correct answer) 98. public static void main(String[] args) {99.     NavigableSet<Integer> set = new

TreeSet<Integer>();100.     set.add(-12);101.     set.add(24);102.     System.out.format("%d %d %d %d",103.         set.lower(-12),104.         set.lower(0),105.         set.lower(24),106.         set.lower(100)107.     );108. }

a. An exception is thrown at runtime.b. It prints “null -12 -12 24″.c. It prints “-12 -12 24 24″.

109. What is the output of this code? (1 correct answer) 110. public static void main(String[] args) {111.     NavigableSet<Integer> set = new

TreeSet<Integer>();112.     set.add(-12);113.     set.add(24);114.     System.out.format("%d %d %d %d",115.         set.floor(-12),116.         set.floor(0),117.         set.floor(24),118.         set.floor(100)119.     );120. }

a. An exception is thrown at runtime.b. It prints “null -12 -12 24″.c. It prints “-12 -12 24 24″.

121. What is the output of this code? (1 correct answer) 122. public static void main(String[] args) {123.     NavigableSet<Integer> set = new

TreeSet<Integer>();124.     set.add(-12);125.     set.add(24);126.     System.out.format("%d %d %d %d",127.         set.higher(-12),128.         set.higher(0),129.         set.higher(24),130.         set.higher(100)131.     );132. }

a. An exception is thrown at runtime.b. It prints “24 24 null null”.c. It prints “-12 24 24 null”.

133. What is the output of this code? (1 correct answer) 134. public static void main(String[] args) {135.     NavigableSet<Integer> set = new

TreeSet<Integer>();136.     set.add(-12);137.     set.add(24);138.     System.out.format("%d %d %d %d",139.         set.ceiling(-12),140.         set.ceiling(0),141.         set.ceiling(24),142.         set.ceiling(100)143.     );

144. }a. An exception is thrown at runtime.b. It prints “24 24 null null”.c. It prints “-12 24 24 null”.

145. What is the output of this code? (1 correct answer) 146. public static void main(String[] args) {147.     NavigableSet<Integer> set = new

TreeSet<Integer>();148.     set.add(-12);149.     set.add(24);150.     set.add(-28);151.     set.add(-0);152.     set.add(0);153.     set.add(+0);154.     set.add(11);155.     set.add(145);156.     System.out.format("%d %d %d %d",157.         set.higher(-28),158.         set.lower(24),159.         set.floor(-0),160.         set.ceiling(100)161.     );162. }

a. An exception is thrown at runtime.b. It prints “-12 11 0 100″.c. It prints “-12 11 0 145″.d. It prints “-28 24 0 100″.e. It prints “-28 24 0 145″.

163. What is the output of this code? (1 correct answer) 164. public static void main(String[] args) {165.     NavigableSet<Integer> set = new

TreeSet<Integer>();166.     set.pollFirst();167.     System.out.println(set.size());168. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “0″.

169. What is the output of this code? (1 correct answer) 170. public static void main(String[] args) {171.     NavigableSet<Integer> set = new

TreeSet<Integer>();172.     set.first();173.     System.out.println(set.size());174. }

a. An exception is thrown at runtime.b. Compilation fails.

c. It prints “0″.175. What is the output of this code? (1 correct answer) 176. public static void main(String[] args) {177.     NavigableSet<Integer> set = new

TreeSet<Integer>();178.     set.add(1);179.     set.add(2);180.     set.add(4);181.     NavigableSet<Integer> sub =

set.headSet(4);182.     System.out.println(sub.last());183. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “4″.d. It prints “2″.

184. What is the output of this code? (1 correct answer) 185. public static void main(String[] args) {186.     NavigableSet<Integer> set = new

TreeSet<Integer>();187.     set.add(1);188.     set.add(2);189.     set.add(4);190.     NavigableSet<Integer> sub =

set.headSet(4, true);191.     System.out.println(sub.last());192. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “4″.d. It prints “2″.

193. What is the output of this code? (1 correct answer) 194. public static void main(String[] args) {195.     NavigableSet<Integer> set = new

TreeSet<Integer>();196.     set.add(1);197.     set.add(2);198.     set.add(4);199.     for (Iterator iterator =

set.descendingSet().iterator();200.         iterator.hasNext();) {201.         System.out.format("%d ",

iterator.next());202.     }203. }

a. It prints “1 2 4 “.b. It prints “4 2 1 “.

c. Compilation fails.204. What is the output of this code? (1 correct answer) 205. import java.util.*;206.207. public class TestSet {208.     static void add(Set<? super String> set)

{209.         set.add(null);210.         System.out.println(set.size());211.     }212.     public static void main(String[] args) {213.         Set<String> set = new

HashSet<String>();214.         add(set);215.     }216. }

a. It prints “0″.b. It prints “1″.c. An exception is thrown at runtime.

217. What is the output of this code? (1 correct answer) 218. import java.util.*;219.220. public class TestSet {221.     static void add(Set<? super String> set)

{222.         set.add(null);223.         set.add(null);224.         System.out.println(set.size());225.     }226.     public static void main(String[] args) {227.         Set<String> set = new

HashSet<String>();228.         add(set);229.     }230. }

a. It prints “0″.b. It prints “1″.c. It prints “2″.d. An exception is thrown at runtime.

231. What is the output of this code? (1 correct answer) 232. import java.util.*;233.234. public class TestSet {235.     static void add(Set<? super String> set)

{236.         set.add("Hi");

237.         set.add("Hi");238.         System.out.println(set.size());239.     }240.     public static void main(String[] args) {241.         Set<String> set = new

HashSet<String>();242.         add(set);243.     }244. }

a. It prints “0″.b. It prints “1″.c. It prints “2″.d. An exception is thrown at runtime.

245. What is the output of this code? (1 correct answer) 246. import java.util.*;247.248. public class TestSet {249.     static void add(Set set) {250.         set.add("Hi");251.         set.add(1);252.         System.out.println(set.size());253.     }254.     public static void main(String[] args) {255.         Set<String> set = new

HashSet<String>();256.         add(set);257.     }258. }

a. It prints “0″.b. It prints “1″.c. It prints “2″.d. An exception is thrown at runtime.

259. What is the output of this code? (1 correct answer) 260. import java.util.*;261.262. public class TestSet {263.     static void add(Set<? super String> set)

{264.         set.add(null);265.         System.out.println(set.size());266.     }267.     public static void main(String[] args) {268.         Set<String> set = new

TreeSet<String>();269.         add(set);270.     }

271. }a. It prints “0″.b. It prints “1″.c. An exception is thrown at runtime.

272. What is the output of this code? (1 correct answer) 273. import java.util.*;274.275. public class TestSet {276.     static void add(Set<? super String> set)

{277.         set.add(null);278.         set.add(null);279.         System.out.println(set.size());280.     }281.     public static void main(String[] args) {282.         Set<String> set = new

TreeSet<String>();283.         add(set);284.     }285. }

a. It prints “0″.b. It prints “1″.c. It prints “2″.d. An exception is thrown at runtime.

286. What is the output of this code? (1 correct answer) 287. import java.util.*;288.289. public class TestSet {290.     static void add(Set<? super String> set)

{291.         set.add("Hi");292.         set.add("Hi");293.         System.out.println(set.size());294.     }295.     public static void main(String[] args) {296.         Set<String> set = new

TreeSet<String>();297.         add(set);298.     }299. }

a. It prints “0″.b. It prints “1″.c. It prints “2″.d. An exception is thrown at runtime.

300. What is the output of this code? (1 correct answer) 301. import java.util.*;302.

303. public class TestSet {304.     static void add(Set set) {305.         set.add("Hi");306.         set.add(1);307.         System.out.println(set.size());308.     }309.     public static void main(String[] args) {310.         Set<String> set = new

TreeSet<String>();311.         add(set);312.     }313. }

a. It prints “0″.b. It prints “1″.c. It prints “2″.d. An exception is thrown at runtime.

314. What is the output of this code? (1 correct answer) 315. import java.util.*;316.317. public class TestSet {318.     static void print(Set<? extends String>

set) {319.         for (String element : set) {320.             System.out.format("%s ",

element);321.         }322.     }323.     public static void main(String[] args) {324.         Set<String> set = new

LinkedHashSet<String>();325.         set.add("A");326.         set.add("J");327.         set.add("A");328.         set.add("X");329.         print(set);330.     }331. }

a. It prints “A J A X “.b. It prints “A J X “.c. The output cannot be determined.

332. What is the output of this code? (1 correct answer) 333. import java.util.*;334.335. public class TestSet {336.     static void print(Set<? extends String>

set) {

337.         for (String element : set) {338.             System.out.format("%s ",

element);339.         }340.     }341.     public static void main(String[] args) {342.         Set<String> set = new

HashSet<String>();343.         set.add("A");344.         set.add("J");345.         set.add("A");346.         set.add("X");347.         print(set);348.     }349. }

a. It prints “A J A X “.b. It prints “A J X “.c. The output cannot be determined.

350. SortedMap IS-A Collection. (1 correct answer) a. trueb. false

351. SortedMap IS-A Map. (1 correct answer) a. trueb. false

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

a. trueb. false

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

a. None.b. Two.c. Three.d. Five.

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

a. K firstKey()b. V firstKey()

c. K firstKey(K)d. V firstKey(K)

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

a. SortedMap<K,V> tailMap(K)b. SortedMap<K,V> tailMap(K, V)c. SortedMap<K,V> tailMap(K, boolean)

356. What happens when this code gets executed? (1 correct answer) 357. public static void main(String[] args) {358.     SortedMap<String, Integer> map = new

TreeMap<String, Integer>();359.     map.last();360. }

a. A NoSuchElementException is thrown.b. It compiles and runs fine.c. Compilation fails.

361. What happens when this code gets executed? (1 correct answer) 362. public static void main(String[] args) {363.     SortedMap<String, Integer> map = new

TreeMap<String, Integer>();364.     map.lastKey();365. }

a. A NoSuchElementException is thrown.b. It compiles and runs fine.c. Compilation fails.

366. What happens when this code gets executed? (1 correct answer) 367. public static void main(String[] args) {368.     SortedMap<String, Integer> map = new

TreeMap<String, Integer>();369.     map.lastEntry();370. }

a. A NoSuchElementException is thrown.b. It compiles and runs fine.c. Compilation fails.

371. What happens when this code gets executed? (1 correct answer) 372. public static void main(String[] args) {373.     SortedMap<String, Integer> map = new

TreeMap<String, Integer>();374.     map.put("B", 1);375.     System.out.println(map.put("B", 2));376. }

a. It prints “B”.b. It prints “null”.c. It prints “1″.d. It prints “2″.

377. What happens when this code gets executed? (1 correct answer) 378. public static void main(String[] args) {

379.     SortedMap<String, Integer> map = new TreeMap<String, Integer>();

380.     map.put("B", 1);381.     map.put("B", 2);382.     map.put("a", 4);383.     System.out.format("%s %d",384.         map.lastKey(),385.         map.size());386. }

a. It prints “a 2″.b. It prints “a 3″.c. It prints “B 2″.d. It prints “B 3″.

387. What happens when this code gets executed? (1 correct answer) 388. public static void main(String[] args) {389.     SortedMap<String, Integer> map = new

TreeMap<String, Integer>();390.     map.put("K", 1);391.     map.put("B", 2);392.     map.put("F", 3);393.     System.out.println(map.tailMap("C").size

());394. }

a. It prints “0″.b. It prints “1″.c. It prints “2″.d. It prints “3″.

395. What happens when this code gets executed? (1 correct answer) 396. public static void main(String[] args) {397.     SortedMap<String, Integer> map = new

TreeMap<String, Integer>();398.     map.put("K", 1);399.     map.put("B", 2);400.     map.put("F", 3);401.     System.out.println(map.tailMap("C").put(

"D", 1));402. }

a. An IllegalArgumentException is thrown.b. It prints “null”.c. It prints “D”.d. It prints “F”.

403. What happens when this code gets executed? (1 correct answer) 404. public static void main(String[] args) {405.     SortedMap<String, Integer> map = new

TreeMap<String, Integer>();406.     map.put("K", 1);407.     map.put("B", 2);

408.     map.put("F", 3);409.     System.out.println(map.tailMap(

"c").size());410. }

a. An IllegalArgumentException is thrown.b. It prints “0″.c. It prints “1″.d. It prints “2″.e. It prints “3″.

411. What happens when this code gets executed? (1 correct answer) 412. public static void main(String[] args) {413.     SortedMap<String, Integer> map = new

TreeMap<String, Integer>();414.     map.put("K", 1);415.     map.put("B", 2);416.     map.put("F", 3);417.     System.out.println(map.tailMap(

"c").put("c", 1));418. }

a. An IllegalArgumentException is thrown.b. It prints “null”.c. It prints “c”.d. It prints “F”.

419. What happens when this code gets executed? (1 correct answer) 420. public static void main(String[] args) {421.     SortedMap<String, Integer> map = new

TreeMap<String, Integer>();422.     map.put("K", 1);423.     map.put("B", 2);424.     map.put("F", 3);425.     System.out.println(map.tailMap("c").put(

"A", 1));426. }

a. An IllegalArgumentException is thrown.b. It prints “null”.c. It prints “c”.d. It prints “F”.

427. What happens when this code gets executed? (1 correct answer) 428. public static void main(String[] args) {429.     Map map = new TreeMap();430.     map.put("K", 1);431.     map.put("L", "L");432. }

a. An exception is thrown at runtime.b. Compilation fails.c. None of the above.

433. What happens when this code gets executed? (1 correct answer) 434. public static void main(String[] args) {

435.     Map map = new TreeMap();436.     map.put("K", 1);437.     map.put(2, "L");438. }

a. An exception is thrown at runtime.b. Compilation fails.c. None of the above.

439. What happens when this code gets executed? (1 correct answer) 440. public static void main(String[] args) {441.     Map map = new LinkedHashMap();442.     map.put("K", 1);443.     map.put(2, "L");444. }

a. An exception is thrown at runtime.b. Compilation fails.c. None of the above.

445. What happens when this code gets executed? (1 correct answer) 446. public static void main(String[] args) {447.     Map map = new LinkedHashMap();448.     map.put("K", 1);449.     map.put(2, "L");450.     for (Object element : map.keySet()) {451.         System.out.print(element);452.     }453. }

a. An exception is thrown at runtime.b. Compilation fails.c. None of the above.

454. What happens when this code gets executed? (1 correct answer) 455. public static void main(String[] args) {456. new LinkedHashMap<String,

Integer>().put(null, null);457. new HashMap<Object,

Number>().put(null, null);458. new TreeMap<String,

Queue<String>>().put(null, null);459. }

a. An exception is thrown at runtime.b. It compiles and runs fine.c. Compilation fails.

460. What happens when this code gets executed? (1 correct answer) 461. public static void main(String[] args) {462. new Hashtable().put(1, null);463. }

a. An exception is thrown at runtime.b. It compiles and runs fine.c. Compilation fails.

464. What happens when this code gets executed? (1 correct answer)

465. public static void main(String[] args) {466. new Hashtable().put(null, 1);467. }

a. An exception is thrown at runtime.b. It compiles and runs fine.c. Compilation fails.

468. What happens when this code gets compiled and executed? (1 correct answer) 469. public static void main(String[] args) {470.     Map<String, Integer> map = new

LinkedHashMap<String, Integer>();471.     map.put("J", 1);472.     map.put("A", 2);473.     map.put("V", 3);474.     map.put("A", 4);475.     for (Object element : map.keySet()) {476.         System.out.format("%s ", element);477.     }478. }

a. It prints “J A V A”b. It prints “J A V”c. It prints “A J V”d. The output order cannot be guaranteed.

479. What happens when this code gets compiled and executed? (1 correct answer) 480. public static void main(String[] args) {481.     Map<String, Integer> map = new

TreeMap<String, Integer>();482.     map.put("J", 1);483.     map.put("A", 2);484.     map.put("V", 3);485.     map.put("A", 4);486.     for (Object element : map.keySet()) {487.         System.out.format("%s ", element);488.     }489. }

a. It prints “J A V A”b. It prints “J A V”c. It prints “A J V”d. The output order cannot be guaranteed.

490. What happens when this code gets compiled and executed? (1 correct answer) 491. public static void main(String[] args) {492.     Map<String, Integer> map = new

HashMap<String, Integer>();493.     map.put("T", 1);494.     map.put("M", 2);495.     map.keySet().add("A");496.     System.out.println(map.size());497. }

a. It prints “2″b. It prints “3″c. An exception is thrown at runtime.

498. What happens when this code gets compiled and executed? (1 correct answer) 499. public static void main(String[] args) {500.     Map<String, Integer> map = new

HashMap<String, Integer>();501.     map.put("T", 1);502.     map.put("M", 2);503.     map.keySet().remove("T");504.     System.out.println(map.size());505. }

a. It prints “1″b. It prints “2″c. An exception is thrown at runtime.

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

a. trueb. false

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

a. trueb. false

508. What is the output of this code? (1 correct answer) 509. public static void main(String[] args) {510.     NavigableMap<Integer, String> map =511.     new TreeMap<Integer, String>();512.     map.put(5, "D");513.     map.put(-1, "A");514.     map.put(7, "O");515.     System.out.format("%d %d %d %d",516.         map.lowerKey(-100),517.         map.lowerKey(5),518.         map.lowerKey(6),519.         map.lowerKey(100)520. );521. }

a. It prints “null -1 5 7″.b. It prints “null 5 5 7″.

522. What is the output of this code? (1 correct answer) 523. public static void main(String[] args) {524.     NavigableMap<Integer, String> map =

525.     new TreeMap<Integer, String>();526.     map.put(5, "D");527.     map.put(-1, "A");528.     map.put(7, "O");529.     System.out.format("%d %d %d %d",530.         map.floorKey(-100),531.         map.floorKey(5),532.         map.floorKey(6),533.         map.floorKey(100)534. );535. }

a. It prints “null -1 5 7″.b. It prints “null 5 5 7″.

536. What is the output of this code? (1 correct answer) 537. public static void main(String[] args) {538.     NavigableMap<Integer, String> map =539.     new TreeMap<Integer, String>();540.     map.put(5, "D");541.     map.put(-1, "A");542.     map.put(7, "O");543.     System.out.format("%d %d %d %d",544.         map.higherKey(-100),545.         map.higherKey(5),546.         map.higherKey(6),547.         map.higherKey(100)548. );549. }

a. It prints “-1 7 7 null”.b. It prints “-1 5 7 null”.

550. What is the output of this code? (1 correct answer) 551. public static void main(String[] args) {552.     NavigableMap<Integer, String> map =553.     new TreeMap<Integer, String>();554.     map.put(5, "D");555.     map.put(-1, "A");556.     map.put(7, "O");557.     System.out.format("%d %d %d %d",558.         map.ceilingKey(-100),559.         map.ceilingKey(5),560.         map.ceilingKey(6),561.         map.ceilingKey(100)562. );563. }

a. It prints “-1 7 7 null”.b. It prints “-1 5 7 null”.

564. What is the output of this code? (1 correct answer)

565. public static void main(String[] args) {566.     NavigableMap<Integer, String> map =567.     new TreeMap<Integer, String>();568.     map.put(5, "D");569.     map.put(-1, "A");570.     map.put(7, "O");571.     System.out.format("%s %s %s %s",572.         map.lowerEntry(-100),573.         map.lowerEntry(5),574.         map.lowerEntry(6),575.         map.lowerEntry(100)576. );577. }

a. It prints “null -1=A 5=D 7=O”.b. It prints “null 5=D 5=D 7=O”.

578. What is the output of this code? (1 correct answer) 579. public static void main(String[] args) {580.     NavigableMap<Integer, String> map =581.     new TreeMap<Integer, String>();582.     map.put(5, "D");583.     map.put(-1, "A");584.     map.put(7, "O");585.     System.out.format("%s %s %s %s",586.         map.floorEntry(-100),587.         map.floorEntry(5),588.         map.floorEntry(6),589.         map.floorEntry(100)590. );591. }

a. It prints “null -1=A 5=D 7=O”.b. It prints “null 5=D 5=D 7=O”.

592. What is the output of this code? (1 correct answer) 593. public static void main(String[] args) {594.     NavigableMap<Integer, String> map =595.     new TreeMap<Integer, String>();596.     map.put(5, "D");597.     map.put(-1, "A");598.     map.put(7, "O");599.     System.out.format("%s %s %s %s",600.         map.higherEntry(-100),601.         map.higherEntry(5),602.         map.higherEntry(6),603.         map.higherEntry(100)604. );605. }

a. It prints “-1=A 7=O 7=O null”.

b. It prints “-1=A 5=D 7=O null”.606. What is the output of this code? (1 correct answer) 607. public static void main(String[] args) {608.     NavigableMap<Integer, String> map =609.     new TreeMap<Integer, String>();610.     map.put(5, "D");611.     map.put(-1, "A");612.     map.put(7, "O");613.     System.out.format("%s %s %s %s",614.         map.ceilingEntry(-100),615.         map.ceilingEntry(5),616.         map.ceilingEntry(6),617.         map.ceilingEntry(100)618. );619. }

a. It prints “-1=A 7=O 7=O null”.b. It prints “-1=A 5=D 7=O null”.

620. What is the output of this code? (1 correct answer) 621. public static void main(String[] args) {622.     NavigableMap<Integer, String> map =623.     new TreeMap<Integer, String>();624.     map.put(34, "J");625.     map.put(-1, "a");626.     map.put(70, "v");627.     map.put(5, "a");628.     map.put(-1, "2");629.     System.out.format("%s %s %s %s",630.         map.ceilingEntry(-100),631.         map.floorEntry(5),632.         map.higherEntry(6),633.         map.lowerKey(5)634.     );635. }

a. It prints “-1=2 5=a 34=J -1″.b. It prints “-1=a 5=a 34=J -1″.c. It prints “-1=2 5=a 34=J 5″.d. It prints “-1=a 5=a 34=J 5″.

636. What is the output of this code? (1 correct answer) 637. public static void main(String[] args) {638.     NavigableMap<Integer, String> map =639.     new TreeMap<Integer, String>();640.     map.lastEntry();641.     System.out.println(map.size());642. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “0″.

643. What is the output of this code? (1 correct answer) 644. public static void main(String[] args) {645.     NavigableMap<Integer, String> map =646.     new TreeMap<Integer, String>();647.     map.pollFirstEntry();648.     System.out.println(map.size());649. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “0″.

650. What is the output of this code? (1 correct answer) 651. public static void main(String[] args) {652.     NavigableMap<Integer, String> map =653.     new TreeMap<Integer, String>();654.     map.firstKey();655.     System.out.println(map.size());656. }

a. An exception is thrown at runtime.b. Compilation fails.c. It prints “0″.

657. What is the output of this code? (1 correct answer) 658. public static void main(String[] args) {659.     NavigableMap<Integer, String> map =660.     new TreeMap<Integer, String>();661.     map.put(1, "A");662.     map.put(2, "B");663.     map.put(3, "C");664.     NavigableMap<Integer, String> sub =665.     map.subMap(0, 3);666.     System.out.println(sub.lastKey());667. }

a. It prints “2″.b. It prints “3″.c. Compilation fails.d. An exception is thrown at runtime.

668. What is the output of this code? (1 correct answer) 669. public static void main(String[] args) {670.     NavigableMap<Integer, String> map =671.     new TreeMap<Integer, String>();672.     map.put(1, "A");673.     map.put(2, "B");674.     map.put(3, "C");675.     NavigableMap<Integer, String> sub =676.     map.subMap(0, false, 3, false);677.     System.out.println(sub.lastKey());678. }

a. It prints “2″.

b. It prints “3″.c. Compilation fails.d. An exception is thrown at runtime.

679. What is the output of this code? (1 correct answer) 680. public static void main(String[] args) {681.     NavigableMap<Integer, String> map =682.     new TreeMap<Integer, String>();683.     map.put(1, "A");684.     map.put(2, "B");685.     map.put(3, "C");686.     NavigableMap<Integer, String> sub =687.     map.subMap(0, false, 3, false);688.     map.put(4, "D");689.     System.out.format("%d %d", map.size(),

sub.size());690. }

a. It prints “2 2″.b. It prints “3 2″.c. It prints “3 3″.d. It prints “4 2″.e. It prints “4 3″.f. Compilation fails.g. An exception is thrown at runtime.

691. What is the output of this code? (1 correct answer) 692. public static void main(String[] args) {693.     NavigableMap<Integer, String> map =694.     new TreeMap<Integer, String>();695.     map.put(1, "A");696.     map.put(2, "B");697.     map.put(3, "C");698.     NavigableMap<Integer, String> sub =699.     map.subMap(0, false, 3, false);700.     sub.put(4, "D");701.     System.out.format("%d %d", map.size(),

sub.size());702. }

a. It prints “2 2″.b. It prints “3 2″.c. It prints “3 3″.d. It prints “4 2″.e. It prints “4 3″.f. Compilation fails.g. An exception is thrown at runtime.

703. Queue is an interface. (1 correct answer) a. trueb. false

704. PriorityQueue IS-A Queue. (1 correct answer) a. true

b. false705. PriorityQueue IS-A AbstractQueue. (1 correct answer)

a. trueb. false

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

a. trueb. false

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

a. One.b. Two.c. Three.

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

a. One.b. Two.c. Three.

709. Consider Queue<E>. What’s the signature of the method offer()? (1 correct answer) a. E offer(E)b. void offer(E)c. boolean offer(E)

710. Consider Queue<E>. What’s the signature of the method peek()? (1 correct answer) a. E peek()b. E peek(E)c. boolean peek(E)

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

a. trueb. false

712. In the Queue interface both add() and offer() add an element to the queue. (1 correct answer)

a. trueb. false

713. What is the output of this code? (1 correct answer) 714. public static void main(String[] args) {715.     Queue<String> queue = new

PriorityQueue<String>();716.     System.out.println(queue.add(null));717. }

a. It prints “true”.b. It prints “false”.c. An exception is thrown at runtime.

718. What is the output of this code? (1 correct answer) 719. public static void main(String[] args) {720.     Queue<String> queue = new

PriorityQueue<String>();721.     System.out.println(queue.offer(null));722. }

a. It prints “true”.b. It prints “false”.c. An exception is thrown at runtime.

723. What is the output of this code? (1 correct answer) 724. public static void main(String[] args) {725.     Queue<String> queue = new

PriorityQueue<String>();726.     System.out.println(queue.peek());727. }

a. It prints “null”.b. An exception is thrown at runtime.

728. What is the output of this code? (1 correct answer) 729. public static void main(String[] args) {730.     Queue<String> queue = new

PriorityQueue<String>();731.     System.out.println(queue.poll());732. }

a. It prints “null”.b. An exception is thrown at runtime.

733. What is the output of this code? (1 correct answer) 734. public static void main(String[] args) {735.     Queue<String> queue = new

PriorityQueue<String>();736.     System.out.println(queue.element());737. }

a. It prints “null”.b. An exception is thrown at runtime.

738. What is the output of this code? (1 correct answer) 739. public static void main(String[] args) {740.     Queue<String> queue = new

PriorityQueue<String>();741.     System.out.println(queue.remove());742. }

a. It prints “null”.b. An exception is thrown at runtime.

743. What is the output of this code? (1 correct answer) 744. public static void main(String[] args) {745.     Queue<String> queue = new

PriorityQueue<String>();

746.     System.out.format("%b %b %d",747.         queue.add("A"),748.         queue.add("A"),749.         queue.size());750. }

a. It prints “true true 2″.b. It prints “true false 1″.c. An exception is thrown at runtime.

751. What is the output of this code? (1 correct answer) 752. public static void main(String[] args) {753.     Queue<String> queue = new

PriorityQueue<String>();754.     queue.add("E");755.     queue.add("J");756.     queue.add("B");757.     queue.add("3");758.     System.out.println(queue.element());759. }

a. It prints “B”.b. It prints “J”.c. It prints “3″.

760. What is the output of this code? (1 correct answer) 761. public static void main(String[] args) {762.     Queue<String> queue = new

PriorityQueue<String>(763.     Collections.reverseOrder());764.     queue.add("E");765.     queue.add("J");766.     queue.add("B");767.     queue.add("3");768.     System.out.println(queue.element());769. }

a. It prints “B”.b. It prints “J”.c. It prints “3″.d. Compilation fails.

770. What is the output of this code? (1 correct answer) 771. public static void main(String[] args) {772.     Queue<String> queue = new

PriorityQueue<String>(773.     4, Collections.reverseOrder());774.     queue.add("E");775.     queue.add("J");776.     queue.add("B");777.     queue.add("3");778.     System.out.println(queue.element());779. }

a. It prints “B”.b. It prints “J”.c. It prints “3″.d. Compilation fails.

780. What is the output of this code? (1 correct answer) 781. public static void main(String[] args) {782.     Queue<String> queue = new

PriorityQueue<String>();783.     queue.add("J");784.     queue.add("A");785.     queue.add("V");786.     queue.add("A");787.     Arrays.sort(queue.toArray());788.     for (String element : queue) {789.         System.out.format("%s ", element);790.     }791. }

a. It prints “J A V A”.b. It prints “A A J V”.c. The output order is not guaranteed.

792. What is the output of this code? (1 correct answer) 793. public static void main(String[] args) {794.     Queue<String> queue = new

PriorityQueue<String>();795.     queue.add("J");796.     queue.add("A");797.     queue.add("V");798.     queue.add("A");799.     String[] array = queue.toArray(new

String[0]);800.     Arrays.sort(array);801.     for (String element : array) {802.         System.out.format("%s ", element);803.     }804. }

a. It prints “J A V A”.b. It prints “A A J V”.c. The output order is not guaranteed.

805. What happens when this code is executed? (1 correct answer) 806. public static void main(String[] args) {807.     Queue queue = new PriorityQueue();808.     queue.add(null);809. }

a. An exception is thrown at runtime.b. It runs just fine.

810. What is the output of this code? (1 correct answer) 811. public static void main(String[] args) {

812.     Queue queue = new PriorityQueue();813.     queue.add(1);814.     queue.add("J");815.     System.out.print(queue.element());816. }

a. It prints “J”.b. It prints “1″.c. An exception is thrown at runtime.

817. What is the output of this code? (1 correct answer) 818. public static void main(String[] args) {819.     Queue<String> queue = new

PriorityQueue<String>();820.     queue.add("Z");821.     queue.add("T");822.     queue.add("F");823.     queue.add("A");824.     queue.add("A");825.     queue.add("C");826.     queue.offer("G");827.     queue.poll();828.     queue.peek();829.     queue.remove();830.     queue.element();831.     System.out.format("%s %d",832.         queue.poll(),833.         queue.size()834.     );835. }

a. It prints “A 6″.b. It prints “A 5″.c. It prints “C 5″.d. It prints “C 4″.e. It prints “F 4″.f. It prints “F 3″.

836. List is an interface. (1 correct answer) a. trueb. false

837. LinkedList IS-A List. Also, LinkedList IS-A Queue since Java 1.5. (1 correct answer) a. trueb. false

838. In the List interface there are two overloaded methods with the name remove. a. trueb. false

839. In the List interface there are two overloaded methods with the name addAll. a. trueb. false

840. Consider List<E>. What is the signature of the method lastIndexOf? a. E lastIndexOf(int)

b. int lastIndexOf(E)c. int lastIndexOf(Object)

841. Consider List<E>. What is the signature of the method set? a. E set(int, E)b. int set(int, E)c. boolean set(int, E)

842. What is the output of this code? (1 correct answer) 843. public static void main(String[] args) {844.     List<Integer> list = new

ArrayList<Integer>();845.     List<Integer> sub = list.subList(0, 1);846.     System.out.println(sub.size());847. }

a. It prints “0″.b. It prints “1″.c. Compilation fails.d. An exception is thrown at runtime.

848. What is the output of this code? (1 correct answer) 849. public static void main(String[] args) {850.     List<Integer> list = new

ArrayList<Integer>();851.     list.add(1);852.     list.add(2);853.     List<Integer> sub = list.subList(0, 0);854.     System.out.println(sub.size());855. }

a. It prints “0″.b. It prints “1″.c. Compilation fails.d. An exception is thrown at runtime.

856. What is the output of this code? (1 correct answer) 857. public static void main(String[] args) {858.     List list = new ArrayList();859.     list.add(1);860.     list.add(2);861.     for (int element : list) {862.         System.out.format("%d ", element);863.     }864. }

a. It prints “1 2 “.b. Compilation fails.

865. What is the output of this code? (1 correct answer) 866. public static void main(String[] args) {867.     List<Integer> list = new

ArrayList<Integer>();868.     list.add(1);869.     list.add(2);870.     for (int element : list) {

871.         System.out.format("%d ", element);872.     }873. }

a. It prints “1 2 “.b. Compilation fails.

874. What is the output of this code? (1 correct answer) 875. public static void main(String[] args) {876.     List<Integer> list = new

ArrayList<Integer>();877.     list.add(1);878.     list.add(2);879.     for (Object element : list) {880.         System.out.format("%d ", element);881.     }882. }

a. It prints “1 2 “.b. Compilation fails.

883. What is the output of this code? (1 correct answer) 884. public static void main(String[] args) {885.     List list = new ArrayList();886.     list.add(1);887.     list.add(2);888.     for (Object element : list) {889.         System.out.format("%d ", element);890.     }891. }

a. It prints “1 2 “.b. Compilation fails.

892. What is the output of this code? (1 correct answer) 893. public static void main(String[] args) {894.     List<String> list = new

LinkedList<String>();895.     list.add("X");896.     list.add("M");897.     list.add("L");898.     for (String element : list) {899.         System.out.format("%s ", element);900.     }901. }

a. It prints “X M L “.b. Compilation fails.

902. What is the output of this code? (1 correct answer) 903. public static void main(String[] args) {904.     List<String> list = new

LinkedList<String>();905.     list.add("X");906.     list.add("M");

907.     list.add("L");908.     System.out.println(list.peek());909. }

a. It prints “X”.b. It prints “L”.c. Compilation fails.

910. What is the output of this code? (1 correct answer) 911. public static void main(String[] args) {912.     LinkedList<String> list = new

LinkedList<String>();913.     list.add("X");914.     list.add("M");915.     list.add("L");916.     System.out.println(list.peek());917. }

a. It prints “X”.b. It prints “L”.c. Compilation fails.

918. What is the output of this code? (1 correct answer) 919. public static void main(String[] args) {920.     List<Number> list = new

ArrayList<Number>();921.     list.add(7);922.     list.add(8);923.     list.add(9);924.     list.remove(7);925.     System.out.println(list.size());926. }

a. It prints “2″.b. It prints “3″.c. An exception is thrown at runtime.

927. What is the output of this code? (1 correct answer) 928. public static void main(String[] args) {929.     List<Number> list = new

ArrayList<Number>();930.     list.add(7);931.     list.add(8);932.     list.add(9);933.     list.remove(Integer.valueOf(7));934.     System.out.println(list.size());935. }

a. It prints “2″.b. It prints “3″.c. An exception is thrown at runtime.

936. What is the output of this code? (1 correct answer) 937. public static void main(String[] args) {

938.     List<Number> list = new ArrayList<Number>();

939.     list.add(7);940.     list.add(8);941.     list.add(7);942.     Number index = list.get(7);943.     System.out.println(index);944. }

a. It prints “0″.b. It prints “2″.c. An exception is thrown at runtime.

945. What is the output of this code? (1 correct answer) 946. public static void main(String[] args) {947.     List<Number> list = new

ArrayList<Number>();948.     list.add(7);949.     list.add(8);950.     list.add(7);951.     Number index =

list.get(Integer.valueOf(7));952.     System.out.println(index);953. }

a. It prints “0″.b. It prints “2″.c. An exception is thrown at runtime.

954. What is the output of this code? (1 correct answer) 955. public static void main(String[] args) {956.     List<Number> list = new

ArrayList<Number>();957.     System.out.format("%b %b %b %d",958.         list.add(7),959.         list.add(null),960.         list.add(7),961.         list.size());962. }

a. It prints “true false false 1″.b. It prints “true true false 2″.c. It prints “true false true 2″.d. It prints “true true true 3″.e. 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. a3. d4. a5. a6. a7. d8. d9. a10. d11. c12. b13. b14. c15. a16. a17. a18. c19. a20. a21. b22. c23. b24. c25. b26. c27. a28. b29. c30. b31. b32. b33. b34. c35. b36. d37. b38. d39. b40. c41. b42. a43. a44. d45. a46. a47. c48. a49. c50. c51. a52. c

53. b54. b55. b56. a57. c58. a59. c60. c61. b62. a63. a64. b65. c66. c67. a68. a69. a70. a71. b72. a73. b74. a75. b76. a77. b78. a79. c80. c81. a82. c83. a84. d85. g86. a87. a88. a89. a90. a91. c92. c93. a94. a95. a96. c97. c98. a99. a100. b101. b102. a103. c

104. d105. b106. c107. b108. a109. c110. d111. a112. a113. a114. a115. c116. a117. d118. a119. b120. a121. a122. a123. a124. c125. a126. c127. a128. c129. c130. d

Share this:

Email Digg Reddit Facebook 2 StumbleUpon Twitter

Like this:

Like

Be the first to like this post.

This entry was posted on Monday, October 6th, 2008 at 9:06 pm and is filed under SCJP 6, Training. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Post navigation

« Previous Post Next Post »

13 Responses to SCJP Mock exam for Collections

1. Srilatha says:

10 November 2008 at 11:49 am

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

2. Nikos says:

11 November 2008 at 9:39 pm

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

3. G says:

25 November 2008 at 1:49 am

Hi, I think the answer for 106) is b

4. Free SCJP Mock exams « Niko’s java blog says:

2 January 2009 at 12:16 am

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

5. Aruna says:

29 January 2009 at 1:19 pm

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

6. Pedro Parra says:

26 April 2009 at 7:36 pm

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

7. Alex Serna says:

22 October 2009 at 4:24 pm

Thank you. Excellent Job!!!

8. Azat says:

19 December 2009 at 9:45 pm

Thanks,

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

9. java pocetnica says:

23 July 2011 at 2:08 am

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

10. arpit says:

22 October 2011 at 4:43 pm

thanks for providing such a best Mock……

11. rkb says:

15 November 2011 at 8:23 pm

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

12. balaji says:

30 November 2011 at 3:33 pm

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

13. Hiral Jhaveri says:

11 January 2012 at 7:55 am

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

Leave a Reply

Enter your comment here...

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

Email (required) (Address never made public)

Name (required)

Website

Notify me of follow-up comments via email.

Nikos Pougounias is a senior Analyst-Programmer specialized in Java Enterprise Edition and Object Oriented Design. During the past years, he has been contributing to major portfolios under the European Commission.

Search

Visitorso 165,279 hits

Top posts (24h)o Free SCJP Mock exams o SCJP 6 Mock exam for I/O o SCJP Mock exam for Overriding & Overloading

Categorieso Application Server (3) o Database (6) o JavaFX (2) o JDBC (4) o JPA (4) o JSP/Servlets (8) o Quick Tutorials (39) o RMI (1) o Ruby (3) o SCJP 6 (7) o SCWCD (9) o Training (20) o Web Services (3) o Wicket (12) o XML (4)

Archiveso August 2011 o July 2010 o March 2010 o August 2009 o May 2009 o March 2009 o February 2009 o January 2009 o November 2008 o October 2008 o September 2008 o August 2008 o July 2008 o June 2008 o May 2008 o March 2008

Basis data certification Custom Tags Database dom4j Eclipse Glassfish Hibernate J2EE

tutorial Java Java EE Java EE tutorial JavaFX Java Persistence API JAX-WS JAXB JBoss JDBC JDeveloper

JPA JPA 2 JSP JWS Music MySQL NetBeans object-relational mapping OCPJP OCPJWCD OpenSolaris Oracle

RMI Ruby SCJP SCWCD Spring Spring 3 SQL Sun Web Web Services Wicket XML Βάση

δεδομένων

Theme: Contempt by Vault9.Blog at WordPress.com.

Follow

Follow “Nikos' Java blog”

Get every new post delivered to your Inbox.

Powered by WordPress.com

Enter your