Sie sind auf Seite 1von 140

-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) a. Two. b. Four. c. All. d. None. 6. Whats the signature of the method readPassword()? (1 correct answer) a. public char[] readPassword() b. public byte[] readPassword() c. public String readPassword() 7. Whats 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. true b. false 9. There are 2 overloaded methods of Console with the name getPassword. (1 correct answer) a. true b. false 10. Theres 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(); // 1 71. console.format("Hello from your console!"); // 2 72. }

73. }
a. A runtime exception is thrown at line 1 b. A runtime exception is thrown at line 2 c. 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! What happens when this code is compiled and executed? (1 correct answer)

110.

111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122.

import java.io.Console; public class Test { public static void main(String[] args) { Console console = System.console(); if (console != null) { String name = console. readLine("Please enter your name: "); console.format("Hello %z!", name); } } }
a. Compilation fails. b. An exception is thrown at runtime. c. If the user enters Nikos, the console prints Hello Nikos! What happens when this code is compiled and executed? (1 correct answer)

123.

124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136.

import java.io.Console; public class Test { public static void main(String[] args) { Console console = System.console(); if (console != null) { console.reader().close(); String name = console. readLine("Please enter your name: "); console.format("Hello %s!", name); } } }
a. Compilation fails. b. An exception is thrown at runtime. c. If the user enters Nikos, the console prints Hello Nikos! What happens when this code is compiled and executed? (1 correct answer)

137.

138. 139. 140. 141. 142. 143. 144.

import java.io.Console; import java.io.IOException; public class Test { public static void main(String[] args) { Console console = System.console(); try {

145. 146. 147. 148. 149. 150. 151. 152.

console.reader().close(); } catch (IOException e) { } String name = console. readLine("Please enter your name: "); console.format("Hello %s!", name); } }
a. Compilation fails. b. An exception is thrown at runtime. c. If the user enters Nikos, the console prints Hello Nikos! What happens when this code is compiled and executed? (1 correct answer)

153.

154. 155. 156. 157. 158. 159. 160. 161.

import java.io.Console; public class Test { public static void main(String[] args) { Console console = System.console(); console.println("Hello!"); } }
a. Compilation fails. b. An exception is thrown at runtime. c. The console prints Hello!. What happens when this code is compiled and executed? (1 correct answer)

162.

163. 164. 165. 166. 167. 168. 169. 170.

import java.io.Console; public class Test { public static void main(String[] args) { Console console = System.console(); console.writer().println("Hello!"); } }
a. Compilation fails. b. An exception is thrown at runtime. c. The console prints Hello!. What happens when this code is compiled and executed? (1 correct answer)

171.

172. 173. 174. 175. 176. 177. 178. 179. 180. 181.

import java.io.Console; public class Test { public static void main(String[] args) { Console console = System.console(); console.writer().append("Hello ") .append("from ").append("console!"); console.flush(); } }

182.

a. Compilation fails. b. An exception is thrown at runtime. c. The console prints Hello from console!. What happens when this code is compiled and executed? (1 correct answer)

183. 184. 185. 186. 187. 188. 189. 190.

import java.io.Console; public class Test { public static void main(String[] args) { Console console = System.console(); console.format("Hello!", null); } }
a. Compilation fails. b. An exception is thrown at runtime. c. The console prints Hello!. What happens when this code is compiled and executed? (1 correct answer)

191.

192. 193. 194. 195. 196. 197. 198. 199.

import java.io.Console; public class Test { public static void main(String[] args) { Console console = System.console(); console.format(null); } }
a. Compilation fails. b. An exception is thrown at runtime. c. The console prints null. What happens when this code is compiled and executed? (1 correct answer)

200.

201. 202. 203. 204. 205. 206. 207. 208. 209.

import java.io.Console; public class Test { public static void main(String[] args) { Console console = System.console(); console.format("Hello ").printf("from ") .format("console!"); } }
a. Compilation fails. b. An exception is thrown at runtime. c. The console prints Hello from console!. NavigableSet is an interface. (1 correct answer) a. true b. false NavigableSet extends SortedSet. (1 correct answer) a. true b. false

210.

211.

212. In Java 1.5 TreeSet implements SortedSet, whereas in Java 1.6 TreeSet implements NavigableSet. (1 correct answer) a. true b. false 213. All these methods belong to NavigableSet. (1 correct answer) lower() higher() floor() ceiling() pollFirst() pollLast() a. true b. 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. Whats the signature of the method lower()? (1 correct answer) a. E lower(E) b. boolean lower(E) c. E lower(NavigableSet<E>) 217. Whats 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. true b. false

219. In the NavigableSet interface there are 2 overloaded methods with the name headSet. (1 correct answer) a. true b. 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. }
232. a. It prints null -12 -12 24. b. It prints -12 -12 24 24. 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. }
244. a. It prints null -12 -12 24. b. It prints -12 -12 24 24. 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.
256.

}
a. It prints 24 24 null null. b. It prints -12 24 24 null. 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. }
268. a. It prints 24 24 null null. b. It prints -12 24 24 null. 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. b. c. d. 286. It prints -12 11 0 100. It prints -12 11 0 145. It prints -28 24 0 100. It prints -28 24 0 145. What is the output of this code? (1 correct answer)

287. public static void main(String[] args) { 288. NavigableSet<Integer> set = new TreeSet<Integer>(); 289. set.pollFirst();

290. 291.

System.out.println(set.size()); }
a. An exception is thrown at runtime. b. Compilation fails. c. It prints 0. What is the output of this code? (1 correct answer)

292.

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. What is the output of this code? (1 correct answer)

298.

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. b. c. d. 307. An exception is thrown at runtime. Compilation fails. It prints 4. It prints 2. 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. b. c. d. 316. An exception is thrown at runtime. Compilation fails. It prints 4. It prints 2. 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. true b. false 328. NavigableMap IS-A SortedMap. (1 correct answer) a. true b. false 329. In Java 1.5 TreeMap implements SortedMap, whereas in Java 1.6 TreeMap implements NavigableMap. (1 correct answer) a. true b. false 330. All these methods belong to NavigableMap. (1 correct answer) lowerKey() higherKey() floorKey() ceilingKey() a. true b. false 331. All these methods belong to NavigableMap. (1 correct answer) lowerEntry() higherEntry() floorEntry() ceilingEntry() a. true b. false 332. All these methods belong to NavigableMap. (1 correct answer) pollFirstEntry() pollLastEntry() firstEntry() lastEntry() a. true b. false 333. All these methods belong to NavigableMap. (1 correct answer) pollFirstKey() pollLastKey()

firstKey() lastKey() a. true b. false 334. Assume NavigableMap<K,V>. Whats 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>. Whats 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>. Whats 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. true b. false 340. In NavigableMap there are 2 overloaded methods with the name headMap. (1 correct answer) a. true

341.

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

342. 343. 344. 345. 346. 347. 348. 349. 350. 351. 352. 353. 354.
355.

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

356. 357. 358. 359. 360. 361. 362. 363. 364. 365. 366. 367. 368.
369.

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

370. 371. 372. 373. 374. 375. 376. 377. 378. 379. 380. 381.

public static void main(String[] args) { NavigableMap<Integer, String> map = new TreeMap<Integer, String>(); map.put(5, "D"); map.put(-1, "A"); map.put(7, "O"); System.out.format("%d %d %d %d", map.higherKey(-100), map.higherKey(5), map.higherKey(6), map.higherKey(100) );

382.
383.

}
a. It prints -1 7 7 null. b. It prints -1 5 7 null. What is the output of this code? (1 correct answer)

384. 385. 386. 387. 388. 389. 390. 391. 392. 393. 394. 395. 396.
397.

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

398. 399. 400. 401. 402. 403. 404. 405. 406. 407. 408. 409. 410.
411.

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

412. 413. 414. 415. 416. 417. 418. 419. 420. 421.

public static void main(String[] args) { NavigableMap<Integer, String> map = new TreeMap<Integer, String>(); map.put(5, "D"); map.put(-1, "A"); map.put(7, "O"); System.out.format("%s %s %s %s", map.floorEntry(-100), map.floorEntry(5), map.floorEntry(6),

422. 423. 424.


425.

map.floorEntry(100) ); }
a. It prints null -1=A 5=D 7=O. b. It prints null 5=D 5=D 7=O. What is the output of this code? (1 correct answer)

426. 427. 428. 429. 430. 431. 432. 433. 434. 435. 436. 437. 438.
439.

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

440. 441. 442. 443. 444. 445. 446. 447. 448. 449. 450. 451. 452.
453.

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

454. 455. 456. 457. 458. 459. 460. 461.

public static void main(String[] args) { NavigableMap<Integer, String> map = new TreeMap<Integer, String>(); map.put(34, "J"); map.put(-1, "a"); map.put(70, "v"); map.put(5, "a"); map.put(-1, "2");

462. 463. 464. 465. 466. 467. 468.


a. b. c. d. 469.

System.out.format("%s %s %s %s", map.ceilingEntry(-100), map.floorEntry(5), map.higherEntry(6), map.lowerKey(5) ); }


It prints -1=2 5=a 34=J -1. It prints -1=a 5=a 34=J -1. It prints -1=2 5=a 34=J 5. It prints -1=a 5=a 34=J 5. What is the output of this code? (1 correct answer)

470. 471. 472. 473. 474. 475.

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

476.

477. 478. 479. 480. 481. 482.

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

483.

484. 485. 486. 487. 488. 489.

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

490.

491. 492. 493. 494. 495.

public static void main(String[] args) { NavigableMap<Integer, String> map = new TreeMap<Integer, String>(); map.put(1, "A"); map.put(2, "B");

496. 497. 498. 499. 500.


a. b. c. d. 501.

map.put(3, "C"); NavigableMap<Integer, String> sub = map.subMap(0, 3); System.out.println(sub.lastKey()); }


It prints 2. It prints 3. Compilation fails. An exception is thrown at runtime. What is the output of this code? (1 correct answer)

502. 503. 504. 505. 506. 507. 508. 509. 510. 511.
a. b. c. d. 512.

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

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. b. c. d. e. f. g. 524. It prints 2 2. It prints 3 2. It prints 3 3. It prints 4 2. It prints 4 3. Compilation fails. An exception is thrown at runtime. What is the output of this code? (1 correct answer)

525. 526.

public static void main(String[] args) { 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. b. c. d. e. f. g. It prints 2 2. It prints 3 2. It prints 3 3. It prints 4 2. It prints 4 3. Compilation fails. An exception is thrown at runtime.

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

Answers
1. a 2. a 3. a 4. d 5. d 6. a 7. c 8. a 9. b 10. a 11. b 12. b 13. b 14. b 15. a 16. b 17. a 18. a 19. d 20. c 21. c 22. b 23. a 24. c 25. a 26. c

27. c 28. c 29. b 30. c 31. a 32. a 33. a 34. a 35. a 36. c 37. a 38. a 39. a 40. a 41. a 42. b 43. a 44. b 45. b 46. c 47. a 48. b 49. c 50. b 51. a 52. a 53. a 54. a 55. a 56. a 57. b 58. c 59. a 60. a 61. b 62. a 63. a 64. a 65. a 66. b 67. a 68. b 69. a 70. b 71. a 72. b 73. a 74. c 75. c 76. a

77. c 78. a 79. d 80. g

References
A friendly community of people preparing for the Sun Certified Java Programmer certification can be found at the JavaRanch SCJP forum. You may contact me for any suggestion.

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 Nikos 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: theres 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

Visitors
o o o o

165,272 hits Free SCJP Mock exams SCJP 6 Mock exam for I/O SCJP Mock exam for Collections Application Server (3) Database (6)

Top posts (24h)

Categories
o o

o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
Basis data

JavaFX (2) JDBC (4) JPA (4) JSP/Servlets (8) Quick Tutorials (39) RMI (1) Ruby (3) SCJP 6 (7) SCWCD (9) Training (20) Web Services (3) Wicket (12) XML (4) August 2011 July 2010 March 2010 August 2009 May 2009 March 2009 February 2009 January 2009 November 2008 October 2008 September 2008 August 2008 July 2008 June 2008 May 2008 March 2008

Archives

certification Custom Tags Database dom4j Eclipse Glassfish Hibernate J2EE

Nikos' Java blog


quick & 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. 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()

5.

6.

7.

8.

9.

10.

11.

format() How many of them are static? (1 correct answer) a. Two. b. Four. c. All. d. None. 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. Whats the signature of the method readPassword()? (1 correct answer) a. public char[] readPassword() b. public byte[] readPassword() c. public String readPassword() Whats 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) There are 2 overloaded methods of Console with the name readLine. (1 correct answer) a. true b. false There are 2 overloaded methods of Console with the name getPassword. (1 correct answer) a. true b. false Theres maximum one object of type Console available per JVM. (1 correct answer) a. True. b. False. 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(); // 1 71. console.format("Hello from your console!"); // 2 72. } 73. }
a. A runtime exception is thrown at line 1 b. A runtime exception is thrown at line 2 c. 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. 94. 95. } 96. }

console.printf("Hello!"); }

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! What happens when this code is compiled and executed? (1 correct answer)

110.

111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122.

import java.io.Console; public class Test { public static void main(String[] args) { Console console = System.console(); if (console != null) { String name = console. readLine("Please enter your name: "); console.format("Hello %z!", name); } } }
a. Compilation fails. b. An exception is thrown at runtime. c. If the user enters Nikos, the console prints Hello Nikos! What happens when this code is compiled and executed? (1 correct answer)

123.

124. 125. 126. 127. 128. 129.

import java.io.Console; public class Test { public static void main(String[] args) { Console console = System.console(); if (console != null) {

130. 131. 132. 133. 134. 135. 136.

console.reader().close(); String name = console. readLine("Please enter your name: "); console.format("Hello %s!", name); } } }
a. Compilation fails. b. An exception is thrown at runtime. c. If the user enters Nikos, the console prints Hello Nikos! What happens when this code is compiled and executed? (1 correct answer)

137.

138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152.

import java.io.Console; import java.io.IOException; public class Test { public static void main(String[] args) { Console console = System.console(); try { console.reader().close(); } catch (IOException e) { } String name = console. readLine("Please enter your name: "); console.format("Hello %s!", name); } }
a. Compilation fails. b. An exception is thrown at runtime. c. If the user enters Nikos, the console prints Hello Nikos! What happens when this code is compiled and executed? (1 correct answer)

153.

154. 155. 156. 157. 158. 159. 160. 161.

import java.io.Console; public class Test { public static void main(String[] args) { Console console = System.console(); console.println("Hello!"); } }
a. Compilation fails. b. An exception is thrown at runtime. c. The console prints Hello!. What happens when this code is compiled and executed? (1 correct answer)

162.

163. 164. 165. 166. 167.

import java.io.Console; public class Test { public static void main(String[] args) { Console console = System.console();

168. 169. 170.

console.writer().println("Hello!"); } }
a. Compilation fails. b. An exception is thrown at runtime. c. The console prints Hello!. What happens when this code is compiled and executed? (1 correct answer)

171.

172. 173. 174. 175. 176. 177. 178. 179. 180. 181.

import java.io.Console; public class Test { public static void main(String[] args) { Console console = System.console(); console.writer().append("Hello ") .append("from ").append("console!"); console.flush(); } }
a. Compilation fails. b. An exception is thrown at runtime. c. The console prints Hello from console!. What happens when this code is compiled and executed? (1 correct answer)

182.

183. 184. 185. 186. 187. 188. 189. 190.

import java.io.Console; public class Test { public static void main(String[] args) { Console console = System.console(); console.format("Hello!", null); } }
a. Compilation fails. b. An exception is thrown at runtime. c. The console prints Hello!. What happens when this code is compiled and executed? (1 correct answer)

191.

192. 193. 194. 195. 196. 197. 198. 199.

import java.io.Console; public class Test { public static void main(String[] args) { Console console = System.console(); console.format(null); } }
a. Compilation fails. b. An exception is thrown at runtime. c. The console prints null. What happens when this code is compiled and executed? (1 correct answer)

200.

201. 202.

import java.io.Console;

203. 204. 205. 206. 207. 208. 209.

public class Test { public static void main(String[] args) { Console console = System.console(); console.format("Hello ").printf("from ") .format("console!"); } }

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. true b. false 211. NavigableSet extends SortedSet. (1 correct answer) a. true b. false 212. In Java 1.5 TreeSet implements SortedSet, whereas in Java 1.6 TreeSet implements NavigableSet. (1 correct answer) a. true b. false 213. All these methods belong to NavigableSet. (1 correct answer) lower() higher() floor() ceiling() pollFirst() pollLast() a. true b. 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. Whats the signature of the method lower()? (1 correct answer) a. E lower(E) b. boolean lower(E) c. E lower(NavigableSet<E>) 217. Whats 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. true b. false 219. In the NavigableSet interface there are 2 overloaded methods with the name headSet. (1 correct answer) a. true b. 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. }
232. a. It prints null -12 -12 24. b. It prints -12 -12 24 24. 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. }
256. a. It prints 24 24 null null. b. It prints -12 24 24 null. 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. }
268. a. It prints 24 24 null null. b. It prints -12 24 24 null. 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. 284. 285.


a. b. c. d. 286.

set.ceiling(100) ); }
It prints -12 11 0 100. It prints -12 11 0 145. It prints -28 24 0 100. It prints -28 24 0 145. What is the output of this code? (1 correct answer)

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. What is the output of this code? (1 correct answer)

292.

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. What is the output of this code? (1 correct answer)

298.

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. b. c. d. 307. An exception is thrown at runtime. Compilation fails. It prints 4. It prints 2. 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. b. c. d. 316. An exception is thrown at runtime. Compilation fails. It prints 4. It prints 2. 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. true b. false 328. NavigableMap IS-A SortedMap. (1 correct answer) a. true b. false 329. In Java 1.5 TreeMap implements SortedMap, whereas in Java 1.6 TreeMap implements NavigableMap. (1 correct answer) a. true b. false 330. All these methods belong to NavigableMap. (1 correct answer) lowerKey() higherKey() floorKey() ceilingKey() a. true b. false 331. All these methods belong to NavigableMap. (1 correct answer) lowerEntry() higherEntry() floorEntry() ceilingEntry()

a. true b. false 332. All these methods belong to NavigableMap. (1 correct answer) pollFirstEntry() pollLastEntry() firstEntry() lastEntry() a. true b. false 333. All these methods belong to NavigableMap. (1 correct answer) pollFirstKey() pollLastKey() firstKey() lastKey() a. true b. false 334. Assume NavigableMap<K,V>. Whats 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>. Whats 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>. Whats 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. b. c. d.

339. answer) a. true b. false 340. In NavigableMap there are 2 overloaded methods with the name headMap. (1 correct answer) a. true b. false 341. What is the output of this code? (1 correct answer)

None. Two. Four. All. In NavigableMap there are 2 overloaded methods with the name subMap. (1 correct

342. 343. 344. 345. 346. 347. 348. 349. 350. 351. 352. 353. 354.
355.

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

356. 357. 358. 359. 360. 361. 362. 363. 364. 365. 366. 367. 368.
369.

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

370. 371.

public static void main(String[] args) { NavigableMap<Integer, String> map =

372. 373. 374. 375. 376. 377. 378. 379. 380. 381. 382.
383.

new TreeMap<Integer, String>(); map.put(5, "D"); map.put(-1, "A"); map.put(7, "O"); System.out.format("%d %d %d %d", map.higherKey(-100), map.higherKey(5), map.higherKey(6), map.higherKey(100) ); }
a. It prints -1 7 7 null. b. It prints -1 5 7 null. What is the output of this code? (1 correct answer)

384. 385. 386. 387. 388. 389. 390. 391. 392. 393. 394. 395. 396.
397.

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

398. 399. 400. 401. 402. 403. 404. 405. 406. 407. 408. 409. 410.
411.

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

412. 413. 414. 415. 416. 417. 418. 419. 420. 421. 422. 423. 424.
425.

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

426. 427. 428. 429. 430. 431. 432. 433. 434. 435. 436. 437. 438.
439.

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

440. 441. 442. 443. 444. 445. 446. 447. 448. 449. 450. 451. 452.

public static void main(String[] args) { NavigableMap<Integer, String> map = new TreeMap<Integer, String>(); map.put(5, "D"); map.put(-1, "A"); map.put(7, "O"); System.out.format("%s %s %s %s", map.ceilingEntry(-100), map.ceilingEntry(5), map.ceilingEntry(6), map.ceilingEntry(100) ); }
a. It prints -1=A 7=O 7=O null.

453.

b. It prints -1=A 5=D 7=O null. What is the output of this code? (1 correct answer)

454. 455. 456. 457. 458. 459. 460. 461. 462. 463. 464. 465. 466. 467. 468.
a. b. c. d. 469.

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

470. 471. 472. 473. 474. 475.

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

476.

477. 478. 479. 480. 481. 482.

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

483.

484. 485. 486. 487. 488.

public static void main(String[] args) { NavigableMap<Integer, String> map = new TreeMap<Integer, String>(); map.firstKey(); System.out.println(map.size());

489.

}
a. An exception is thrown at runtime. b. Compilation fails. c. It prints 0. What is the output of this code? (1 correct answer)

490.

491. 492. 493. 494. 495. 496. 497. 498. 499. 500.
a. b. c. d. 501.

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

502. 503. 504. 505. 506. 507. 508. 509. 510. 511.
a. b. c. d. 512.

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

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. b. c. d. e. f. g. 524.

It prints 2 2. It prints 3 2. It prints 3 3. It prints 4 2. It prints 4 3. Compilation fails. An exception is thrown at runtime. What is the output of this code? (1 correct answer)

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. b. c. d. e. f. g. It prints 2 2. It prints 3 2. It prints 3 3. It prints 4 2. It prints 4 3. Compilation fails. An exception is thrown at runtime.

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

Answers
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. a a a d d a c a b a b b b b a b

17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67.

a a d c c b a c a c c c b c a a a a a c a a a a a b a b b c a b c b a a a a a a b c a a b a a a a b a

68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80.

b a b a b a c c a c a d g

References
A friendly community of people preparing for the Sun Certified Java Programmer certification can be found at the JavaRanch SCJP forum. You may contact me for any suggestion.

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 Nikos 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: theres 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

Visitors
o o o o

165,279 hits Free SCJP Mock exams SCJP 6 Mock exam for I/O SCJP Mock exam for Overriding & Overloading Application Server (3) Database (6) JavaFX (2) JDBC (4) JPA (4) JSP/Servlets (8) Quick Tutorials (39) RMI (1) Ruby (3) SCJP 6 (7) SCWCD (9) Training (20) Web Services (3) Wicket (12) XML (4) August 2011 July 2010 March 2010 August 2009 May 2009 March 2009 February 2009 January 2009 November 2008 October 2008 September 2008

Top posts (24h) Categories


o o o o o o o o o o o o o o o

Archives
o o o o o o o o o o o

o o o o o
Basis data tutorial

August 2008 July 2008 June 2008 May 2008 March 2008

certification Custom Tags Database dom4j Eclipse Glassfish Hibernate J2EE


Java EE Java EE tutorial

Java

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.
Enter your

Powered by WordPress.com

Nikos' Java blog


quick & easy Java tutorials

Home

SCJP 6 Mock exam for Threads

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. true b. false 2. Runnable belongs to the java.lang package. (1 correct answer) a. true b. 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. Whats the signature? (1 correct answer) a. public void run() b. public void run() throws InterruptedException c. public void run(Runnable r) d. public void run(Runnable r) throws InterruptedException 5. Class Thread implements the Runnable interface. (1 correct answer) a. true b. 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()

9.

10.

11.

12.

13.

14.

15.

16.

sleep() yield() currentThread() How many of them declare a checked exception? (1 correct answer) a. One. b. Two. c. Three. d. All. Whats the signature of the method start()? (1 correct answer) a. public void start() b. public void start() throws InterruptedException c. public void start() throws IllegalThreadStateException Whats the signature of the method yield()? (1 correct answer) a. public void yield() b. public void yield() throws InterruptedException c. public static void yield() d. public static void yield() throws InterruptedException There are 2 overloaded methods of Thread with the name sleep. (1 correct answer) a. true b. false There are 3 overloaded methods of Thread with the name join. (1 correct answer) a. true b. false The Thread class has three public static and final integer fields: MIN_PRIORITY, NORM_PRIORITY, MAX_PRIORITY. (1 correct answer) a. true b. false 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. Consider these methods. wait() notify() notifyAll() How many of them are static? (1 correct answer) a. One. b. All. c. None. 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. true b. 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. What happens when this code gets compiled and executed? (1 correct answer)

103.

104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114.

public class Test extends Thread { public void run() { System.out.println("run 1"); } public void run(String s) { System.out.println("run 2"); } public static void main(String[] args) { new Test().start("hi"); } }
a. It prints run 1. b. It prints run 2. c. Compilation fails. What happens when this code gets compiled and executed? (1 correct answer)

115.

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. b. c. d. 122.

It prints nothing. It prints main. It prints Thread-0. Compilation fails because an InterruptedException is not handled. 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. b. c. d. e. 129. It prints nothing. It prints main. It prints Thread-0. Compilation fails because an InterruptedException is not handled. Compilation fails because Test does not implement the run() method. What happens when this code gets compiled and executed? (1 correct answer)

130. 131. 132. 133. 134.

public class Run { public static void main(String[] args) { new Thread().run(); } }
a. Compilation fails. b. It compiles and runs fine. c. An IllegalThreadStateException is thrown at runtime. What happens when this code gets compiled and executed? (1 correct answer)

135.

136. 137. 138. 139. 140. 141. 142.

public class Run { public static void main(String[] args) { Thread thread = new Thread(); thread.run(); thread.run(); } }
a. Compilation fails. b. It compiles and runs fine. c. An IllegalThreadStateException is thrown at runtime. What happens when this code gets compiled and executed? (1 correct answer)

143.

144. 145. 146. 147. 148.

public class Run { public static void main(String[] args) { new Thread().start(); } }
a. Compilation fails. b. It compiles and runs fine. c. An IllegalThreadStateException is thrown at runtime. What happens when this code gets compiled and executed? (1 correct answer)

149.

150.

public class Run {

151. 152. 153. 154. 155. 156.

public static void main(String[] args) { Thread thread = new Thread(); thread.start(); thread.start(); } }
a. Compilation fails. b. It compiles and runs fine. c. An IllegalThreadStateException is thrown at runtime. What happens when this code gets compiled and executed? (1 correct answer)

157.

158. 159. 160. 161. 162. 163. 164. 165.

public class Run { public static void main(String[] args) { Thread thread = new Thread(); thread.start(); thread = new Thread(); thread.start(); } }
a. Compilation fails. b. It compiles and runs fine. c. An IllegalThreadStateException is thrown at runtime. What happens when this code gets compiled and executed? (1 correct answer)

166.

167. 168. 169. 170. 171. 172. 173. 174.

public class Run { public static void main(String[] args) { Thread thread = new Thread(); thread.start(); Thread.yield(); thread.start(); } }
a. Compilation fails. b. It compiles and runs fine. c. An IllegalThreadStateException is thrown at runtime. What happens when this code gets compiled and executed? (1 correct answer)

175.

176. 177. 178. 179. 180. 181. 182.

public class Run { public static void main(String[] args) { Thread thread = new Thread(); thread.run(); thread.start(); } }
a. Compilation fails. b. It compiles and runs fine. c. An IllegalThreadStateException is thrown at runtime. What happens when this code gets compiled and executed? (1 correct answer)

183.

184. 185.

public class Run { 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. What happens when this code gets compiled and executed? (1 correct answer)

191.

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. What happens when this code gets compiled and executed? (1 correct answer)

199.

200. 201. 202. 203. 204. 205. 206. 207. 208. 209.
210.

public class Run { public static void main(String[] args) { Thread thread = new Thread() { public void run() { System.out.println("Hello!"); } }; thread.start(); } }
a. It prints Hello!. b. Compilation fails. What happens when this code gets compiled and executed? (1 correct answer)

211. 212. 213. 214. 215. 216. 217. 218. 219.


220.

public class Run { public static void main(String[] args) { new Thread() { public void run() { System.out.println("Hello!"); } }.start(); } }
a. It prints Hello!. b. Compilation fails. What happens when this code gets compiled and executed? (1 correct answer)

221. 222. 223. 224. 225. 226. 227. 228. 229.


230.

public class Run { public static void main(String[] args) { new Runnable() { public void run() { System.out.println("Hello!"); } }.start(); } }
a. It prints Hello!. b. Compilation fails. What happens when this code gets compiled and executed? (1 correct answer)

231. 232. 233. 234. 235. 236. 237. 238. 239.


240.

public class Run { public static void main(String[] args) { new Thread(new Runnable() { public void run() { System.out.println("Hello!"); } }).start(); } }
a. It prints Hello!. b. Compilation fails. 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. These classes are defined in the same file. What is the output? (1 correct answer)

254.

255. 256. 257.

class Job implements Runnable { public void run() { System.out.println("Working...");

258. 259. 260. 261. 262. 263. 264. 265. 266. 267. 268.

} } public class My { public static void main(String[] args) { Job job = new Job(); Thread thread1 = new Thread(job); Thread thread2 = new Thread(job); thread1.start(); thread2.start(); } }
a. Compilation fails. b. It prints Working three times. c. It prints Working one time and then an exception is thrown. What is the output? (1 correct answer)

269.

270. 271. 272. 273. 274. 275. 276. 277. 278. 279.
a. b. c. d. 280.

public class Test extends Thread { public void run() { System.out.println(isAlive()); } public static void main(String[] args) { Test test = new Test(); System.out.println(test.isAlive()); test.start(); } }
false false false true true false true true What is the output? (1 correct answer)

281. 282. 283. 284. 285. 286. 287. 288. 289. 290.
a. b. c. d. 291.

public class Test extends Thread { public void run() { System.out.println(isAlive()); } public static void main(String[] args) { Test test = new Test(); System.out.println(test.isAlive()); test.run(); } }
false false false true true false true true What is the output? (1 correct answer)

292. 293.

public class Test extends Thread { public Test() {

294. 295. 296. 297. 298. 299.


300.

System.out.println(isAlive()); } public static void main(String[] args) { new Test(); } }


a. false b. true What is the output? (1 correct answer)

301. 302. 303. 304. 305. 306. 307. 308.


309.

public class Test extends Thread { public Test() { System.out.println(isAlive()); } public static void main(String[] args) { new Test().run(); } }
a. false b. true What is the output? (1 correct answer)

310. 311. 312. 313. 314. 315. 316. 317.

public class Test extends Thread { public Test() { System.out.println(isAlive()); } public static void main(String[] args) { new Test().start(); } }

a. false b. 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. 330. 331. 332. 333. 334. 335. 336. { 337. 338. 339.

public class Account { private Integer number = 0; public void setNumber(Integer number) { synchronized (this) { this.number = number; } } synchronized public Integer getNumber() return number; } }

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. 342. 343. 344. 345. 346. 347. 348. 349. 350. 351. 352. 353.

public class Account { private Integer number = 0; public void setNumber(Integer number) { synchronized (this) { this.number = number; } } public Integer getNumber() { synchronized (this) { return number; } } }

a. Yes. b. No. 354. These classes are defined in a single file. Theres a value object Account that carefully protects its 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. // here 388. } 389. }
Just before the main method exits, the accounts number field is guaranteed to have value 2000.
a. true b. false These classes are defined in a single file.

390.

391. 392. 393. 394. 395. 396. { 397. 398. 399. 400. 401.

class Account { private Integer number = 0; public synchronized void increase() { number++; } public synchronized Integer getNumber() return number; } } class Client extends Thread {

402. 403. 404. 405. 406. 407. 408. 409. 410. 411. 412. 413. 414. throws 415. 416. 417. 418. 419. 420. 421. 422. 423. 424.

Account account; public Client(Account account) { this.account = account; } public void run() { for (int i = 1; i <= 1000; i++) { account.increase(); } } } public class Run { public static void main(String[] args) Exception { Account account = new Account(); Client one = new Client(account); Client two = new Client(account); one.start(); two.start(); one.join(); two.join(); // here } }

Just before the main method exits, the accounts number field is guaranteed to have value 2000.
a. true b. false These classes are defined in a single file.

425.

426. 427. 428. 429. 430. 431. { 432. 433. 434. 435. 436. 437. 438. 439.

class Account { private Integer number = 0; public void increase() { number++; } public synchronized Integer getNumber() return number; } } class Client extends Thread { Account account; public Client(Account account) { this.account = account;

440. 441. 442. 443. 444. 445. 446. 447. 448. 449. throws 450. 451. 452. 453. 454. 455. 456. 457. 458. 459.

} public void run() { for (int i = 1; i <= 1000; i++) { account.increase(); } } } public class Run { public static void main(String[] args) Exception { Account account = new Account(); Client one = new Client(account); Client two = new Client(account); one.start(); two.start(); one.join(); two.join(); // here } }

Just before the main method exits, the accounts number field is guaranteed to have value 2000.
a. true b. false What happens when this code gets compiled and executed? (1 correct answer)

460.

461. 462. 463. 464. 465. 466. 467.


a. b. c. d. 468.

public class Test { public static void main(String[] args) { String message = "Hello!"; message.wait(); // 1 System.out.println(message); } }
Compilation fails. It prints Hello!. An IllegalMonitorStateException is thrown at runtime. Nothing gets printed, as the main thread just waits at line 1. What happens when this code gets compiled and executed? (1 correct answer)

469. 470. 471. 472. 473. 474.

public class Test { public static void main(String[] args) { String message = "Hello!"; try { message.wait(); } catch (InterruptedException e) {

475. 476. 477. 478.


a. b. c. d. 479.

} System.out.println(message); } }
Compilation fails. It prints Hello!. An IllegalMonitorStateException is thrown at runtime. Nothing gets printed, as the main thread just waits at line 1. What happens when this code gets compiled and executed? (1 correct answer)

480. 481. 482. 483. 484. 485. 486. 487. 488. 489. 490. 491.
a. b. c. d. 492.

public class Test { public static void main(String[] args) { String message = "Hello!"; synchronized (args) { try { message.wait(); } catch (InterruptedException e) { } } System.out.println(message); } }
Compilation fails. It prints Hello!. An IllegalMonitorStateException is thrown at runtime. Nothing gets printed, as the main thread just waits at line 1. What happens when this code gets compiled and executed? (1 correct answer)

493. 494. 495. 496. 497. 498. 499. 500. 501. 502. 503. 504.
a. b. c. d. 505.

public class Test { public static void main(String[] args) { String message = "Hello!"; synchronized (message) { try { message.wait(); } catch (InterruptedException e) { } } System.out.println(message); } }
Compilation fails. It prints Hello!. An IllegalMonitorStateException is thrown at runtime. Nothing gets printed, as the main thread just waits at line 1. These classes are defined in the same file. What is the output? (1 correct answer)

506. 507. 508. 509.

class Job extends Thread { private Integer number = 0; public void run() { for (int i = 1; i < 1000000; i++) {

510. 511. 512. 513. 514. 515. 516. 517. 518. 519. 520. 521. )); 522. 523.

number++; } } public Integer getNumber() { return number; } } public class Test { public static void main(String[] args) { Job thread = new Job(); thread.start(); System.out.println(thread.getNumber( } }

524.

a. It prints 0. b. It prints 999999. c. The output is not guaranteed to be any of the above. These classes are defined in the same file. What is the output? (1 correct answer)

525. 526. 527. 528. 529. 530. 531. 532. 533. 534. 535. 536. 537. 538. 539. 540. 541. 542. 543. 544. )); 545. 546.

class Job extends Thread { private Integer number = 0; public void run() { for (int i = 1; i < 1000000; i++) { number++; } } public Integer getNumber() { return number; } } public class Test { public static void main(String[] args) throws InterruptedException { Job thread = new Job(); thread.start(); synchronized (thread) { thread.wait(); } System.out.println(thread.getNumber( } }

547.

a. It prints 0. b. It prints 999999. c. The output is not guaranteed to be any of the above. These classes are defined in the same file. What is the output? (1 correct answer)

548. 549. 550. 551. 552. 553. 554. 555. 556. 557. 558. 559. 560. 561. 562. 563. throws 564. 565. 566. 567. 568. 569. 570. )); 571. 572.

class Job extends Thread { private Integer number = 0; public void run() { synchronized (this) { for (int i = 1; i < 1000000; i++) { number++; } notify(); } } public Integer getNumber() { return number; } } public class Test { public static void main(String[] args) Exception { Job thread = new Job(); thread.start(); synchronized (thread) { thread.wait(); } System.out.println(thread.getNumber( } }

573.

a. It prints 0. b. It prints 999999. c. The output is not guaranteed to be any of the above. These classes are defined in the same file. What is the output? (1 correct answer)

574. 575. 576. 577. 578. 579. 580. 581. 582. 583. 584. 585. 586. 587. throws

class Job extends Thread { private Integer number = 0; public void run() { for (int i = 1; i < 1000000; i++) { number++; } } public Integer getNumber() { return number; } } public class Test { public static void main(String[] args) Exception {

588. 589. 590. 591. )); 592. 593.

Job thread = new Job(); thread.start(); thread.join(); System.out.println(thread.getNumber( } }

594.

a. It prints 0. b. It prints 999999. c. The output is not guaranteed to be any of the above. What happens when this code gets compiled and executed? (1 correct answer)

595. 596. 597. 598. 599. 600. 601.

public class Run { public static void main(String[] args) { System.out.print("A "); notifyAll(); System.out.println("B"); } }
a. It prints A B. b. Compilation fails. c. It prints A and an exception is thrown. What happens when this code gets compiled and executed? (1 correct answer)

602.

603. 604. 605. 606. 607. 608. 609.

public class Run { public static void main(String[] args) { System.out.print("A "); new Object().notifyAll(); System.out.println("B"); } }
a. It prints A B. b. Compilation fails. c. It prints A and an exception is thrown. What happens when this code gets compiled and executed? (1 correct answer)

610.

611. 612. 613. 614. 615. 616. 617. 618. 619.

public class Run { public static void main(String[] args) { System.out.print("A "); synchronized (new Object()) { new Object().notifyAll(); } System.out.println("B"); } }
a. It prints A B. b. Compilation fails. c. It prints A and an exception is thrown. What happens when this code gets compiled and executed? (1 correct answer)

620.

621.

public class Run {

622. 623. 624. 625. 626. 627. 628. 629. 630.

public static void main(String[] args) { System.out.print("A "); final Object test = new Object(); synchronized (test) { test.notifyAll(); } System.out.println("B"); } }
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. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. a a a a a d c b a c a a a b c a a b b b a a a a a a a a b b a c

33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72.

b b b b b c b c b a a a a b a b b b a a a a a a a b a b a c c d c b b b b c c 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. Whats 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 Nikos 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 couldnt 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 Niko Can 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 cant get how they work the way they do .. If someone is willing to give a hand here please, Id 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

Shouldnt 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

Visitors
o o o o

165,279 hits Free SCJP Mock exams SCJP 6 Mock exam for I/O SCJP Mock exam for Overriding & Overloading Application Server (3) Database (6) JavaFX (2) JDBC (4) JPA (4) JSP/Servlets (8) Quick Tutorials (39) RMI (1) Ruby (3) SCJP 6 (7) SCWCD (9) Training (20) Web Services (3) Wicket (12) XML (4) August 2011 July 2010 March 2010 August 2009 May 2009 March 2009 February 2009 January 2009 November 2008 October 2008 September 2008 August 2008 July 2008 June 2008 May 2008 March 2008

Top posts (24h) Categories


o o o o o o o o o o o o o o o

Archives
o o o o o o o o o o o o o o o o

Basis data tutorial

certification Custom Tags Database dom4j Eclipse Glassfish Hibernate J2EE


Java EE Java EE tutorial

Java

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.
Enter your

Powered by WordPress.com

Nikos' Java blog


quick & easy Java tutorials

Home

SCJP Mock exam for Generics

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 1-24 25-40 topic assignment and hierarchy add and get elements from a generic collection

41-50 51-60 61-72

overloading and overriding various details 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);


100. a. Yes. b. No. Will this code compile successfully? (1 correct answer)

101. 102.
103.

Set<Integer> set = new TreeSet<Integer>(); set.add((int)1.0f);


a. Yes. b. No. Will this code compile successfully? (1 correct answer)

104. 105.
106.

Set<Integer> set = new TreeSet<Integer>(); set.add(10L);


a. Yes. b. No. Will this code compile successfully? (1 correct answer)

107. 108. 109.


110.

Set<Integer> set = new TreeSet<Integer>(); int number = (short)10; set.add(number);


a. Yes. b. No. Will this code compile successfully? (1 correct answer)

111. 112.
113.

Set<Number> set = new TreeSet<Integer>(); set.add(10L);


a. Yes. b. No. Will this code compile successfully? (1 correct answer)

114. 115.
116.

NavigableSet<?> set = new TreeSet<Object>(); set.add(new Object());


a. Yes. b. No. Will this code compile successfully? (1 correct answer)

117. NavigableSet<? super Object> set = new TreeSet<Object>(); 118. set.add(new Object());
119. a. Yes. b. No. Will this code compile successfully? (1 correct answer)

120. NavigableSet<? extends Object> set = new TreeSet<Object>(); 121. set.add(new Object());
122. a. Yes. b. No. Will this code compile successfully? (1 correct answer)

123. NavigableSet<? extends String> set = new TreeSet<String>(); 124. set.add("string");


125. a. Yes. b. No. Will this code compile successfully? (1 correct answer)

126. NavigableSet<? super String> set = new TreeSet<String>(); 127. set.add("string");


128. a. Yes. b. No. Will this code compile successfully? (1 correct answer)

129. NavigableSet<? super String> set = new TreeSet<String>(); 130. set.add(new Object());
131. a. Yes. b. No. 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. }
136. a. Yes. b. No. Will this code compile successfully? (1 correct answer)

137. List<? extends Integer> list = new ArrayList<Integer>(); 138. Integer first = list.get(0);
139. a. Yes. b. No. 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. }
144. a. Yes. b. No. Will this code compile successfully? (1 correct answer)

145. List<? super Integer> list = new ArrayList<Integer>();

146.
147.

Integer first = list.get(0);


a. Yes. b. No. Will this code compile successfully? (1 correct answer)

148. List<? super Integer> list = new ArrayList<Integer>(); 149. Object first = list.get(0);
150. a. Yes. b. No. Will this code compile successfully? (1 correct answer)

151. 152. 153. 154. 155. 156. 157. 158.


159.

import java.util.*; class Test { void say(Set<Double> set) { } void say(SortedSet<Double> set) { } }
a. Yes. b. No. Will this code compile successfully? (1 correct answer)

160. 161. 162. 163. 164. 165. 166. 167.


168.

import java.util.*; class Test { void say(Set<Double> set) { } void say(Set<Boolean> set) { } }
a. Yes. b. No. Will this code compile successfully? (1 correct answer)

169. 170. 171. 172. 173. 174. 175. 176.


177.

import java.util.*; class Test { void say(Set<Double> set) { } void say(Set<Double>... set) { } }
a. Yes. b. No. Consider these classes.

178. 179. 180. 181.

import java.util.*; class Parent { void say(List<String> list) {

182. 183. 184. 185. 186. 187. 188. 189.

System.out.println("parent"); } } class Child extends Parent { void say(List list) { System.out.println("child"); } }

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

public static void main(String[] java) { Child c = new Child(); c.say(new LinkedList<String>()); }
a. It prints child. b. It prints parent. c. Compilation fails. Consider these classes.

190.

191. 192. 193. 194. 195. 196. 197. 198. 199. 200. 201. 202.

import java.util.*; class Parent { void say(List<String> list) { System.out.println("parent"); } } class Child extends Parent { void say(List list) { System.out.println("child"); } }

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

public static void main(String[] java) { Child c = new Child(); c.say(new LinkedList<List<Boolean>>()); }
a. It prints child. b. It prints parent. c. Compilation fails. Consider these classes.

203.

204. 205. 206.

import java.util.*; class Parent {

207. 208. 209. 210. 211. 212. 213. 214. 215.

void say(List<String> list) { System.out.println("parent"); } } class Child extends Parent { void say(List list) { System.out.println("child"); } }

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

public static void main(String[] java) { Parent c = new Child(); c.say(new LinkedList<String>()); }
a. It prints child. b. It prints parent. c. Compilation fails. Consider these classes.

216.

217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228.

import java.util.*; class Parent { void say(List<String> list) { System.out.println("parent"); } } class Child extends Parent { void say(List list) { System.out.println("child"); } }

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

public static void main(String[] java) { Parent c = new Child(); c.say(new LinkedList<Long>()); }
a. It prints child. b. It prints parent. c. Compilation fails. Will this code compile successfully? (1 correct answer)

229.

230. 231.

import java.util.*;

232. 233. 234. 235. 236. 237. 238. 239. 240. 241.
242.

class Parent { void say(List<String> list) { System.out.println("parent"); } } class Child extends Parent { void say(List<Integer> list) { System.out.println("child"); } }
a. Yes. b. No. Will this code compile successfully? (1 correct answer)

243. 244. 245. 246. { 247. 248. 249. 250. 251. 252. 253. 254.
255.

import java.util.*; class Parent { void say(List<? extends Number> list) System.out.println("parent"); } } class Child extends Parent { void say(List<Integer> list) { System.out.println("child"); } }
a. Yes. b. No. Will this code compile successfully? (1 correct answer)

256. 257. 258. 259. 260. 261. 262. 263. 264. 265. 266. 267.
268.

import java.util.*; class Parent { void say(List list) { System.out.println("parent"); } } class Child extends Parent { void say(List<Integer> list) { System.out.println("child"); } }
a. Yes. b. No. Will this code compile successfully? (1 correct answer)

269.

Object set = new TreeSet<Integer>();

270. boolean flag = set instanceof NavigableSet<Integer>;


271. a. Yes. b. No. 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. }
283. a. It prints yes. b. It prints nothing. Will this code compile successfully? (1 correct answer)

284.
285.

Class c = ArrayList<Integer>.class;
a. Yes. b. No. Will this code compile successfully? (1 correct answer)

286. Class c = new ArrayList<Integer>().getClass();


287. a. Yes. b. No. Will this code compile successfully? (1 correct answer)

288.
289.

new ArrayList<?>();
a. Yes. b. No. Will this code compile successfully? (1 correct answer)

290.
291.

new TreeMap<String, ? super Integer>();


a. Yes. b. No. Will this code compile successfully? (1 correct answer)

292.
293.

new ArrayList<Set<?>>();
a. Yes. b. No. 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. }
302. a. Yes. b. No. 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. }
308. a. Yes. b. No. Will this code compile successfully? (1 correct answer)

309. 310. 311. 312. 313.


314.

class Test { <T> T getFirst(List<T> list) { return list.get(0); } }


a. Yes. b. No. Will this code compile successfully? (1 correct answer)

315. 316. 317. 318. 319.


320.

class Test { static <T> T getFirst(List<T> list) { return list.get(0); } }


a. Yes. b. No. Will this code compile successfully? (1 correct answer)

321. 322. 323. 324. 325.


326.

class Test <T> { T getFirst(List<T> list) { return list.get(0); } }


a. Yes. b. No. Will this code compile successfully? (1 correct answer)

327. 328. 329.

class Test <T> { static T getFirst(List<T> list) { return list.get(0);

330. 331.
332.

} }
a. Yes. b. No. Will this code compile successfully? (1 correct answer)

333. 334. 335. 336. 337. 338.


339.

class Test <T> { T instance; Test(T instance) { this.instance = instance; } }


a. Yes. b. No. Will this code compile successfully? (1 correct answer)

340. 341. 342. 343. 344.


345.

class Test <T> { Test(T my) { boolean b = (my instanceof T); } }


a. Yes. b. No. Will this code compile successfully? (1 correct answer)

346. 347. 348. 349. 350.


351.

class Test <T> { void test(T method) { Object my = (T)method; } }


a. Yes. b. No. Will this code compile successfully? (1 correct answer)

352. 353. 354. T>(); 355. 356.


357.

class Test <T> { void test() { NavigableMap map = new TreeMap<String, } }

a. Yes. b. No. Will this code compile successfully? (1 correct answer)

358. 359. 360.


361.

class Test <T> { private T[] array = null; }


a. Yes. b. No. Will this code compile successfully? (1 correct answer)

362. 363.

class Test <T> { private T[] array = new T[7];

364.
365.

}
a. Yes. b. No. Will this code compile successfully? (1 correct answer)

366. 367. 368.


369.

class Test<String> { String my = "Hello!"; }


a. Yes. b. No. 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. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. b b a b b a b b b b b b b

14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64.

b a b a b a b a b a a a a b a b b a b b a b a a b b a a b a a a a c b b b b a b a b b a b b a a a a b

65. 66. 67. 68. 69. 70. 71. 72.

a b a a a b b 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 Nikos 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.its 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 !
4.

nice one

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 dont 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, its correct.
12. Mohamed Farouk says: 18 May 2010 at 5:39 pm

Hello Guys Finally 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 = L2 Read like this I accept L1 = You have L2 Question No Left hand side (I accept) Right Hand side is I have Can I accept Answer 1 I accept Number I have Integer No No 2 I accept Integer I have Number No No

3 I accept subclasses of Number I have Integer Yes Yes 4 I accept Integer I have any subclasses of Number No No 5 I accept Number I have any super class of Integer (Number/Object) No No 6 Any super classes of Integer (Number or Object) I have Number Yes Yes 7 Any super classes of Number(Object) I have Integer No No 8 Integer I have any super classes of Number(Object) No No 9 Number Any subclasses of Integer No No 10 Any subclass on Integer Number No No 11 Any subclass of Number Super classes of Integer(Object/Number) No No 12 Any super class of Integer(Number/Object) Subclasses of Number No No 13 Only super classes of Number(Object) Integer No No 14 Subclasses of Integer Any Super class of Number (Object) No No 15 Anything Integer Yes Yes 16 Integer Anything No No 17 Anything Subclasses of Integer Yes Yes 18 Subclasses of Integer Anything No No 19 Anything Super class of Integer(Number/Object) Yes Yes 20 Any super class of Integer(Number/Object) Anything No No 21 PriorityQueue(non param read as anything) Integer Yes Yes 22 Integer Anything No No 23 Anything Anything Yes Yes 24 Anything Anything Yes Yes
13. arjun says: 10 September 2010 at 12:41 am

48,49,50 are compiler errors please 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 cant 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

Visitors
o o o o

165,279 hits Free SCJP Mock exams SCJP 6 Mock exam for I/O SCJP Mock exam for Overriding & Overloading Application Server (3) Database (6) JavaFX (2) JDBC (4) JPA (4) JSP/Servlets (8) Quick Tutorials (39) RMI (1) Ruby (3) SCJP 6 (7) SCWCD (9) Training (20) Web Services (3) Wicket (12) XML (4) August 2011 July 2010 March 2010 August 2009 May 2009 March 2009 February 2009 January 2009 November 2008 October 2008 September 2008 August 2008 July 2008 June 2008 May 2008 March 2008

Top posts (24h) Categories


o o o o o o o o o o o o o o o

Archives
o o o o o o o o o o o o o o o o

Basis data tutorial

certification Custom Tags Database dom4j Eclipse Glassfish Hibernate J2EE


Java EE Java EE tutorial

Java

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.
Enter your

Powered by WordPress.com

Nikos' Java blog


quick & easy Java tutorials

Home

SCJP Mock exam for Collections

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 17-30 31-40 41-56 57-67 68-85 86-110

SortedSet NavigableSet LinkedHashSet, HashSet, TreeSet SortedMap LinkedHashMap, HashMap, TreeMap NavigableMap PriorityQueue, Queue

111-130 List, LinkedList, ArrayList

1. SortedSet IS-A Set. (1 correct answer) a. true b. false 2. SortedSet belongs to the java.util package. (1 correct answer) a. true b. 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. Whats 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. b. c. d. An NullPointerException is thrown at runtime. A ClassCastException is thrown at runtime. Compilation fails. None of the above.

93. NavigableSet extends SortedSet. (1 correct answer) a. true b. 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. Whats 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. true b. 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. What is the output of this code? (1 correct answer)

109.

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. 119. 120.

set.floor(100) ); }
a. An exception is thrown at runtime. b. It prints null -12 -12 24. c. It prints -12 -12 24 24. What is the output of this code? (1 correct answer)

121.

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. What is the output of this code? (1 correct answer)

133.

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. What is the output of this code? (1 correct answer)

145.

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. 154. 155. 156. 157. 158. 159. 160. 161. 162.
a. b. c. d. e. 163.

set.add(+0); set.add(11); set.add(145); System.out.format("%d %d %d %d", set.higher(-28), set.lower(24), set.floor(-0), set.ceiling(100) ); }


An exception is thrown at runtime. It prints -12 11 0 100. It prints -12 11 0 145. It prints -28 24 0 100. It prints -28 24 0 145. What is the output of this code? (1 correct answer)

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. What is the output of this code? (1 correct answer)

169.

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. What is the output of this code? (1 correct answer)

175.

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.

184.

b. Compilation fails. c. It prints 4. d. It prints 2. 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. b. c. d. 193. An exception is thrown at runtime. Compilation fails. It prints 4. It prints 2. 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. What is the output of this code? (1 correct answer)

204.

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. 215. 216.

add(set); } }
a. It prints 0. b. It prints 1. c. An exception is thrown at runtime. What is the output of this code? (1 correct answer)

217.

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. b. c. d. 231. It prints 0. It prints 1. It prints 2. An exception is thrown at runtime. 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. b. c. d. 245. It prints 0. It prints 1. It prints 2. An exception is thrown at runtime. 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. b. c. d. 259. It prints 0. It prints 1. It prints 2. An exception is thrown at runtime. 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. What is the output of this code? (1 correct answer)

272.

273. 274. 275. 276. { 277. 278. 279. 280.

import java.util.*; public class TestSet { static void add(Set<? super String> set) set.add(null); set.add(null); System.out.println(set.size()); }

281. public static void main(String[] args) { 282. Set<String> set = new TreeSet<String>(); 283. add(set); 284. } 285. }
a. b. c. d. 286. It prints 0. It prints 1. It prints 2. An exception is thrown at runtime. 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. b. c. d. 300. It prints 0. It prints 1. It prints 2. An exception is thrown at runtime. 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.

314.

c. It prints 2. d. An exception is thrown at runtime. 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. What is the output of this code? (1 correct answer)

332.

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. true b. false 351. SortedMap IS-A Map. (1 correct answer) a. true b. false 352. All the following methods are defined in SortedMap. (1 correct answer) firstKey() lastKey() headMap() tailMap() subMap() comparator() a. true b. 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>. Whats 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>. Whats 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. What happens when this code gets executed? (1 correct answer)

366.

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. What happens when this code gets executed? (1 correct answer)

371.

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. b. c. d. 377. It prints B. It prints null. It prints 1. It prints 2. 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. b. c. d. 387. It prints a 2. It prints a 3. It prints B 2. It prints B 3. 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. b. c. d. 395. It prints 0. It prints 1. It prints 2. It prints 3. 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. b. c. d. 403. An IllegalArgumentException is thrown. It prints null. It prints D. It prints F. 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. b. c. d. e. 411. An IllegalArgumentException is thrown. It prints 0. It prints 1. It prints 2. It prints 3. 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. 417. "c", 1)); 418. }


a. b. c. d. 419.

map.put("F", 3); System.out.println(map.tailMap("c").put(

An IllegalArgumentException is thrown. It prints null. It prints c. It prints F. 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. b. c. d. 427. An IllegalArgumentException is thrown. It prints null. It prints c. It prints F. What happens when this code gets executed? (1 correct answer)

428. 429. 430. 431. 432.

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

433.

434. 435. 436. 437. 438.

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

439.

440. 441. 442. 443. 444.

public static void main(String[] args) { Map map = new LinkedHashMap(); map.put("K", 1); map.put(2, "L"); }
a. An exception is thrown at runtime. b. Compilation fails.

445.

c. None of the above. What happens when this code gets executed? (1 correct answer)

446. 447. 448. 449. 450. 451. 452. 453.

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

454.

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. What happens when this code gets executed? (1 correct answer)

460.

461. 462. 463.

public static void main(String[] args) { new Hashtable().put(1, null); }


a. An exception is thrown at runtime. b. It compiles and runs fine. c. Compilation fails. What happens when this code gets executed? (1 correct answer)

464.

465. 466. 467.

public static void main(String[] args) { new Hashtable().put(null, 1); }


a. An exception is thrown at runtime. b. It compiles and runs fine. c. Compilation fails. What happens when this code gets compiled and executed? (1 correct answer)

468.

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. 476. 477. 478.


a. b. c. d. 479.

for (Object element : map.keySet()) { System.out.format("%s ", element); } }


It prints J A V A It prints J A V It prints A J V The output order cannot be guaranteed. 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. b. c. d. 490. It prints J A V A It prints J A V It prints A J V The output order cannot be guaranteed. 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. What happens when this code gets compiled and executed? (1 correct answer)

498.

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. All the following methods belong to NavigableMap. (1 correct answer) lowerEntry() higherEntry() floorEntry() ceilingEntry() a. true b. false 507. All the following methods belong to NavigableMap. (1 correct answer) pollFirstEntry() pollLastEntry() firstEntry() lastEntry() a. true b. false 508. What is the output of this code? (1 correct answer) 506.

509. 510. 511. 512. 513. 514. 515. 516. 517. 518. 519. 520. 521.
522.

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

523. 524. 525. 526. 527. 528. 529. 530. 531. 532. 533. 534. 535.
536.

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

537. 538. 539. 540. 541. 542. 543. 544. 545. 546. 547. 548. 549.
550.

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

551. 552. 553. 554. 555. 556. 557. 558. 559. 560. 561. 562. 563.
564.

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

565. 566. 567. 568. 569. 570. 571. 572. 573. 574. 575. 576. 577.

public static void main(String[] args) { NavigableMap<Integer, String> map = new TreeMap<Integer, String>(); map.put(5, "D"); map.put(-1, "A"); map.put(7, "O"); System.out.format("%s %s %s %s", map.lowerEntry(-100), map.lowerEntry(5), map.lowerEntry(6), map.lowerEntry(100) ); }
a. It prints null -1=A 5=D 7=O.

578.

b. It prints null 5=D 5=D 7=O. What is the output of this code? (1 correct answer)

579. 580. 581. 582. 583. 584. 585. 586. 587. 588. 589. 590. 591.
592.

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

593. 594. 595. 596. 597. 598. 599. 600. 601. 602. 603. 604. 605.
606.

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

607. 608. 609. 610. 611. 612. 613. 614. 615. 616. 617. 618.

public static void main(String[] args) { NavigableMap<Integer, String> map = new TreeMap<Integer, String>(); map.put(5, "D"); map.put(-1, "A"); map.put(7, "O"); System.out.format("%s %s %s %s", map.ceilingEntry(-100), map.ceilingEntry(5), map.ceilingEntry(6), map.ceilingEntry(100) );

619.
620.

}
a. It prints -1=A 7=O 7=O null. b. It prints -1=A 5=D 7=O null. What is the output of this code? (1 correct answer)

621. 622. 623. 624. 625. 626. 627. 628. 629. 630. 631. 632. 633. 634. 635.
a. b. c. d. 636.

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

637. 638. 639. 640. 641. 642.

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

643.

644. 645. 646. 647. 648. 649.

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

650.

651. 652. 653.

public static void main(String[] args) { NavigableMap<Integer, String> map = new TreeMap<Integer, String>();

654. 655. 656.

map.firstKey(); System.out.println(map.size()); }
a. An exception is thrown at runtime. b. Compilation fails. c. It prints 0. What is the output of this code? (1 correct answer)

657.

658. 659. 660. 661. 662. 663. 664. 665. 666. 667.
a. b. c. d. 668.

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

669. 670. 671. 672. 673. 674. 675. 676. 677. 678.
a. b. c. d. 679.

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

680. 681. 682. 683. 684. 685. 686. 687. 688.

public static void main(String[] args) { NavigableMap<Integer, String> map = new TreeMap<Integer, String>(); map.put(1, "A"); map.put(2, "B"); map.put(3, "C"); NavigableMap<Integer, String> sub = map.subMap(0, false, 3, false); map.put(4, "D");

689. System.out.format("%d %d", map.size(), sub.size()); 690. }


a. b. c. d. e. f. g. 691. It prints 2 2. It prints 3 2. It prints 3 3. It prints 4 2. It prints 4 3. Compilation fails. An exception is thrown at runtime. What is the output of this code? (1 correct answer)

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. }
It prints 2 2. It prints 3 2. It prints 3 3. It prints 4 2. It prints 4 3. Compilation fails. An exception is thrown at runtime. Queue is an interface. (1 correct answer) a. true b. false PriorityQueue IS-A Queue. (1 correct answer) a. true b. false PriorityQueue IS-A AbstractQueue. (1 correct answer) a. true b. false All the following are methods of Queue. (1 correct answer) offer() poll() peek() add() remove() element() a. true b. false a. b. c. d. e. f. g.

703.

704.

705.

706.

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>. Whats 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>. Whats 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. true b. false 712. In the Queue interface both add() and offer() add an element to the queue. (1 correct answer) a. true b. 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. What is the output of this code? (1 correct answer)

718.

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. }
728. a. It prints null. b. An exception is thrown at runtime. 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. }
733. a. It prints null. b. An exception is thrown at runtime. 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. }
738. a. It prints null. b. An exception is thrown at runtime. 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. }
743. a. It prints null. b. An exception is thrown at runtime. 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. What is the output of this code? (1 correct answer)

751.

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. What is the output of this code? (1 correct answer)

760.

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. b. c. d. 770. It prints B. It prints J. It prints 3. Compilation fails. 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. b. c. d. 780. It prints B. It prints J. It prints 3. Compilation fails. 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. 786. 787. 788. 789. 790. 791.

queue.add("V"); queue.add("A"); Arrays.sort(queue.toArray()); for (String element : queue) { System.out.format("%s ", element); } }
a. It prints J A V A. b. It prints A A J V. c. The output order is not guaranteed. What is the output of this code? (1 correct answer)

792.

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. What happens when this code is executed? (1 correct answer)

805.

806. 807. 808. 809.


810.

public static void main(String[] args) { Queue queue = new PriorityQueue(); queue.add(null); }
a. An exception is thrown at runtime. b. It runs just fine. What is the output of this code? (1 correct answer)

811. 812. 813. 814. 815. 816.

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

817.

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. b. c. d. e. f. 836. a. b. 837. a. b. 838. a. b. 839. a. b. 840. a. b. c. 841. a. b. c. 842. It prints A 6. It prints A 5. It prints C 5. It prints C 4. It prints F 4. It prints F 3. List is an interface. (1 correct answer) true false LinkedList IS-A List. Also, LinkedList IS-A Queue since Java 1.5. (1 correct answer) true false In the List interface there are two overloaded methods with the name remove. true false In the List interface there are two overloaded methods with the name addAll. true false Consider List<E>. What is the signature of the method lastIndexOf? E lastIndexOf(int) int lastIndexOf(E) int lastIndexOf(Object) Consider List<E>. What is the signature of the method set? E set(int, E) int set(int, E) boolean set(int, E) 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. 846. 847.


a. b. c. d. 848.

List<Integer> sub = list.subList(0, 1); System.out.println(sub.size()); }


It prints 0. It prints 1. Compilation fails. An exception is thrown at runtime. 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. b. c. d. 856. It prints 0. It prints 1. Compilation fails. An exception is thrown at runtime. What is the output of this code? (1 correct answer)

857. 858. 859. 860. 861. 862. 863. 864.


865.

public static void main(String[] args) { List list = new ArrayList(); list.add(1); list.add(2); for (int element : list) { System.out.format("%d ", element); } }
a. It prints 1 2 . b. Compilation fails. 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. }
874. a. It prints 1 2 . b. Compilation fails. 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. 878. 879. 880. 881. 882.


883.

list.add(1); list.add(2); for (Object element : list) { System.out.format("%d ", element); } }


a. It prints 1 2 . b. Compilation fails. What is the output of this code? (1 correct answer)

884. 885. 886. 887. 888. 889. 890. 891.


892.

public static void main(String[] args) { List list = new ArrayList(); list.add(1); list.add(2); for (Object element : list) { System.out.format("%d ", element); } }
a. It prints 1 2 . b. Compilation fails. 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. }
902. a. It prints X M L . b. Compilation fails. 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. What is the output of this code? (1 correct answer)

910.

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. What is the output of this code? (1 correct answer)

918.

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. What is the output of this code? (1 correct answer)

927.

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. What is the output of this code? (1 correct answer)

936.

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.

945.

b. It prints 2. c. An exception is thrown at runtime. 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. What is the output of this code? (1 correct answer)

954.

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. b. c. d. e. It prints true false false 1. It prints true true false 2. It prints true false true 2. It prints true true true 3. 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. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. a a d a a a d d a d c

12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62.

b b c a a a c a a b c b c b c a b c b b b b c b d b d b c b a a d a a c a c c a c b b b a c a c c b a

63. a 64. b 65. c 66. c 67. a 68. a 69. a 70. a 71. b 72. a 73. b 74. a 75. b 76. a 77. b 78. a 79. c 80. c 81. a 82. c 83. a 84. d 85. g 86. a 87. a 88. a 89. a 90. a 91. c 92. c 93. a 94. a 95. a 96. c 97. c 98. a 99. a 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113.

b b a c d b c b a c d a a a

114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130.

a c a d a b a a a a c a c a c c d

Share this:

Email Digg Reddit Facebook2 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 Nikos 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

Visitors
o o o o

165,279 hits Free SCJP Mock exams SCJP 6 Mock exam for I/O SCJP Mock exam for Overriding & Overloading

Top posts (24h)

Categories
o o o o o o o o o o o o o o o

Application Server (3) Database (6) JavaFX (2) JDBC (4) JPA (4) JSP/Servlets (8) Quick Tutorials (39) RMI (1) Ruby (3) SCJP 6 (7) SCWCD (9) Training (20) Web Services (3) Wicket (12) XML (4) August 2011 July 2010 March 2010 August 2009 May 2009 March 2009 February 2009 January 2009 November 2008 October 2008 September 2008 August 2008 July 2008 June 2008 May 2008 March 2008

Archives
o o o o o o o o o o o o o o o o

Basis data tutorial

certification Custom Tags Database dom4j Eclipse Glassfish Hibernate J2EE


Java EE Java EE tutorial

Java

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.

Enter your

Powered by WordPress.com

Das könnte Ihnen auch gefallen