Chapter 9 String Build..
Chapter 9 String Build..
1. None
2. One
3. Two
4. Three
9. What is the output of the following?
10. List<String> tools = new ArrayList<>();
11. tools.add("hammer");
12. tools.add("nail");
13. tools.add("hex key");
System.out.println(tools.get(1));
1. hammer
2. hex key
3. nail
4. None of the above
14. What is the result of the following code?
15. StringBuilder sb = new StringBuilder("radical")
16. .insert(sb.length(), "robots");
System.out.println(sb);
1. radicarobots
2. radicalrobots
3. The code does not compile.
4. The code compiles but throws an exception at runtime.
17. What is the output of the following?
18. List<String> museums = new ArrayList<>(1);
19. museums.add("Natural History");
20. museums.add("Science");
21. museums.add("Art");
22. museums.remove(2);
System.out.println(museums);
1. [Natural History, Science]
2. [Natural History, Art, Science]
3. The code does not compile.
4. The code compiles but throws an exception at runtime.
23. What is the output of the following?
24. 12: StringBuilder b = new StringBuilder("12");
25. 13: b = b.append("3");
26. 14: b.reverse();
15: System.out.println(b.toString());
1. 12
2. 123
3. 321
4. The code does not compile.
27. What is the main benefit of a lambda expression?
1. It allows you to convert a primitive to a wrapper class.
2. It allows you to change the bytecode while the application is
running.
3. It allows you to inherit from multiple classes.
4. It allows you to write code that has the execution deferred.
28. What is the output of the following?
29. 5: StringBuilder line = new StringBuilder("-");
30. 6: StringBuilder anotherLine = line.append("-");
31. 7: System.out.print(line == anotherLine);
32. 8: System.out.print(" ");
9: System.out.print(line.length());
1. false 1
2. false 2
3. true 1
4. true 2
33. The author of this method forgot to include the data type. Which of the
following reference types can fill in the blank to complete this method?
34. public static void secret( ____________mystery) {
35. mystery.add("metal");
36. String str = mystery.get(0);
37. int num = mystery.length();
}
1. ArrayList
2. ArrayList<String>
3. StringBuilder
4. None of the above
38. Which portion of code can be removed so that this line of code continues
to compile?
Predicate<StringBuilder> p = (StringBuilder b) ‐> {return true;};
1. Remove StringBuilder b
2. Remove ->
3. Remove { and ;}
4. Remove { return and ;}
39. What is the output of the following?
40. 20: List<Character> chars = new ArrayList<>();
41. 21: chars.add('a');
42. 22: chars.add('b');
43. 23: chars.set(1, 'c');
44. 24: chars.remove(0);
25: System.out.print(chars.size() + " " + chars.contains('b'));
1. 1 false
2. 1 true
3. 2 false
4. 2 true
45. What is the output of the following?
46. 12: String b = "12";
47. 13: b += "3";
48. 14: b.reverse();
15: System.out.println(b.toString());
1. 12
2. 123
3. 321
4. The code does not compile.
49. How many of these lines fail to compile?
50. Predicate<String> pred1 = s ‐> false;
51. Predicate<String> pred2 = (s) ‐> false;
52. Predicate<String> pred3 = String s ‐> false;
Predicate<String> pred4 = (String s) ‐> false;
1. One
2. Two
3. Three
4. Four
53. What does the following do?
54. public class Shoot {
55. interface Target {
56. boolean needToAim(double angle);
57. }
58. static void prepare(double angle, Target t) {
59. boolean ready = t.needToAim(angle); // k1
60. System.out.println(ready);
61. }
62. public static void main(String[] args) {
63. prepare(45, d -> d > 5 || d < -5); // k2
64. }
}
1. It prints true.
2. It prints false.
3. It doesn’t compile due to line k1.
4. It doesn’t compile due to line k2.
65. What is the output of the following?
66. String teams = new String("694");
67. teams.concat(" 1155");
68. teams.concat(" 2265");
69. teams.concat(" 2869");
System.out.println(teams);
1. 694
2. 694 1155 2265 2869
3. The code compiles but outputs something else.
4. The code does not compile.
70. Which of these classes are in the java.util package?
1.ArrayList
2. LocalDate
3. String
4.I only
5. II only
6. I and II
7. I, II, and III
71. Which of the answer choices results in a different value being output than
the other three choices?
72. StringBuilder sb = new StringBuilder("radical ");
73. sb = ________________________;
System.out.print(sb);
0.new StringBuilder("radical ")
1. .append("robots")
1. new StringBuilder("radical ")
0. .delete(1, 100)
1. .append("obots")
2. .insert(1, "adical r")
2. new StringBuilder("radical ")
0. .insert(7, "robots")
3. new StringBuilder("radical ")
0. .insert(sb.length(), "robots")
74. What is the output of the following?
75. String[] array = {"Natural History", "Science"};
76. List<String> museums = Arrays.asList(array);
77. museums.set(0, "Art");
System.out.println(museums.contains("Art"));
0.true
1. false
2. The code does not compile.
3. The code compiles but throws an exception at runtime.
78. Which is a true statement?
0.If s.contains("abc") is true, then s.equals("abc") is also true.
1. If s.contains("abc") is true, then s.startsWith("abc") is also true.
2. If s.startsWith("abc") is true, then s.equals("abc") is also true.
3. If s.startsWith("abc") is true, then s.contains("abc") is also true.
79. What is the output of the following?
80. 20: List<Character> chars = new ArrayList<>();
81. 21: chars.add('a');
82. 22: chars.add('b');
83. 23: chars.set(1, 'c');
84. 24: chars.remove(0);
25: System.out.print(chars.length());
0.0
1. 1
2. 2
3. None of the above
85. The author of this method forgot to include the data type. Which of the
following reference types can fill in the blank to complete this method?
86. public static void secret(_____________ mystery) {
87. mystery = mystery.replace("1", "8");
88. mystery.startsWith("paper");
89. String s = mystery.toString();
}
0.ArrayList
1. String
2. StringBuilder
3. None of the above
90. Which statement is true about the following figure while ensuring the
code continues to compile?
0.One
1. Two
2. None. The code does not compile.
3. None. The code throws an exception at runtime.
197. What is the output of the following?
198. 12: List<String> magazines = new ArrayList();
199. 13: magazines.add("Readers Digest");
200. 14: magazines.add("People");
201. 15: magazines.clear();
202. 16: magazines.add("The Economist");
203. 17: magazines.remove(1);
18: System.out.println(magazines.size());
0.0
1. 1
2. The code does not compile.
3. The code compiles but throws an exception at runtime.
204. What is the output of the following?
205. public class Costume {
206. public static void main(String[] black) {
207. String witch = 'b';
208. String tail = "lack";
209. witch = witch.concat(tail);
210. System.out.println(witch);
211. }
}
0.b
1. black
2. The code does not compile.
3. The code compiles but throws an exception at runtime.
212. What is the result of the following?
213. LocalDate xmas = LocalDate.of(2016, 12, 25);
214. xmas.setYear(2017);
System.out.println(xmas.getYear());
0.2016
1. 2017
2. The code does not compile.
3. The code compiles but throws an exception at runtime.