forked from RookiePJ/SafariOCAJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestions.txt
More file actions
286 lines (242 loc) · 6.44 KB
/
Questions.txt
File metadata and controls
286 lines (242 loc) · 6.44 KB
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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
Q1)
Given:
class X {
static int MAX_VAL = 100;
int value = 99;
public void setVal(int x) {
if (x <= MAX_VAL) value = x;
}
}
And:
X a = new X();
X b = new X();
a.setVal(120);
System.out.print(a.value + ", ");
b.MAX_VAL = 150; // EXACTLY EQUIVALENT to X.MAX_VAL!!!!
a.setVal(140);
System.out.println(a.value);
What is the result?
A) 99, 99
B) 120, 140
C) 99, 140
D) Compilation failure
E) Exception at runtime
Q2)
Given:
class Y {
static String sayHi() { return "Y-Hello"; }
String sayHi2() { return "Y-Hello2"; }
}
And:
class Z extends Y {
static String sayHi() { return "Z-Hello"; }
String sayHi2() { return "Z-Hello2"; }
}
And this code fragment:
Y y = new Z();
System.out.println(y.sayHi() + ", " + y.sayHi2());
What is the result?
A) Y-Hello, Y-Hello2
B) Y-Hello, Z-Hello2
C) Z-Hello, Z-Hello2
D) Z-Hello, Y-Hello2
E) Compilation fails
Q3)
given (all the Date/Holiday code and...)
_____ () {
return "Holiday called " + name;
}
What must fill the blank to cause current test code to output: Holiday called DecemberDay!
No A) String toString
No B) void toString
No C) @Override String toString
Yes D) public String toString
No E) None of the above!
Q4)
Given:
class Base {
void doBaseStuff() { System.out.println("doBaseStuff"); }
}
and:
class Sub extends Base {
void doBaseStuff() { System.out.println("doSubStuff"); }
void doOtherStuff() { System.out.println("doOtherStuff"); }
}
In the Base and Sub classes, which three are true in isolation? (Choose 3)
No A) doBaseStuff in Base can be marked public without causing errors
Yes B) doBaseStuff in Base can be marked private without causing errors
Yes C) doBaseStuff in Sub can be marked public without causing errors
Yes D) doBaseStuff in Sub can be marked protected without causing errors
No E) doBaseStuff in Sub can be marked private without causing errors
Q5)
Y A) doOtherStuff in Sub can be marked private without causing errors
Y B) doOtherStuff in Sub can be marked public without causing errors
N C) doBaseStuff in Sub can be modified to return int without causing errors
Y D) doBaseStuff in Sub can be modified to take an int and return an int without causing errors
Y E) doBaseStuff in Sub can be modified to take an int and be private without causing errors
Q6)
Given:
public void q11A(int x) {
x = x + 1;
}
And the fragment:
int x = 99;
q11A(x);
System.out.println(x);
What is the result?
A) 99
B) 100
C) An error
Q7)
Given
public void q12A(StringBuilder x) {
x.append(" world");
}
And:
StringBuilder x = new StringBuilder("Hello");
q12A(x);
System.out.println(x);
What is the result?
A) Hello
B) Hello world
C) world
D) An error
Q8)
public void q14A(Integer x) {
x = x + 1;
}
Integer x = 99;
q14A(x);
System.out.println(x);
What is the result?
A) 99
B) 100
C) An error
Q9)
public void q13A(int[] x) {
x[0] = x[0] + 1;
}
int x[] = {99};
q13A(x);
System.out.println(x[0]);
What is the result?
A) 99
B) 100
C) An error
Q10)
Q7)
Given
public void q12A(String x) {
x = "Hello World!";
}
And:
String x = "Hello";
q12A(x);
System.out.println(x);
What is the result?
A) Hello
B) Hello world
C) world
D) An error
Q11)
class Base {
void doBaseStuff() {
System.out.println("doBaseStuff");
}
}
class Sub extends Base {
void doBaseStuff() {
System.out.println("doSubStuff");
}
void doOtherStuff() {
System.out.println("doOtherStuff");
}
}
Base b = new Sub();
b.doOtherStuff();
What is the result?
A) doSubStuff
B) doOtherStuff
C) doBaseStuff
D) Exception at runtime
E) Compilation failure
12)
Given
void doStuff(Sub s) {
Other x = (Other)s;
}
What is the result?
A) success
B) Compilation fails
C) Runtime Exception
D) Something else.
13)
Which are valid (i.e. compile...) individually
A) Predicate<String> ps = s -> s.length();
B) Predicate<String> ps = x => return x.length() > 3;
C) Predicate<Object> po = o -> o == "this";
D) Predicate<String> ps = (s) -> { x.length() < 2 };
14)
Which <n> are valid (i.e. compile...) individually (Choose n)
A) Predicate<Object> ps = s -> s.length() > 3;
Predicate<Object> requires "public boolean test(Object o)"
B) Predicate<String> ps = String s -> s.length() > 3; // needs parens round String s
C) Predicate<Object> po = (String s) -> { return s.length() < 4; }; // argument must be Object
D) Predicate<String> ps = (s) -> s.length() % 2 == 0 ? false : true;
// Side note :)
UnaryOperator<String> uos = s -> s.toUpperCase();
15)
Given:
String h1 = "Hello";
String h2 = new String("Hello");
String h3 = "Hello";
StringBuilder sb1 = new StringBuilder("Hello");
StringBuilder sb2 = new StringBuilder("Hello");
Which expressions are true?
A) h1 == h2
B) h1.equals(h2)
C) h1.equals(sb1)
D) h1 == h3
E) sb1.equals(sb2)
16)
Select all the valid answers
void fun(int arr ...){ ... }
a) fun( {1,2,3,} ) NO
b) fun( new int[]) NO
c) fun(new int {1,2,3}) NO
d) fun(1, 2, 3) YES
17)
class Base {
void doBaseStuff() {
System.out.println("doBaseStuff");
}
}
class Sub extends Base {
void doBaseStuff() {
System.out.println("doSubStuff");
}
In the Base and Sub classes, which are true in isolation?
Y A) doBaseStuff in Base can be marked throws Exception without causing errors
Y B) doBaseStuff in Base can be marked throws RuntimeException without causing errors
N C) doBaseStuff in Sub can be marked throws Exception without causing errors
Y D) doBaseStuff in Sub can be marked throws RuntimeException without causing errors
Y E) if doBaseStuff in Base were marked throws Exception,
then doBaseStuff in Sub can be marked throws IOException without causing errors
18)
StringBuilder sb1 = new StringBuilder("Hello");
StringBuilder sb2 = sb1;
sb1 = null;
Which is true?
A) sb1 is unreferenced and eligible for GC
B) sb1 and sb2 are unreferenced and eligible for GC
C) No objects are eligible for GC
19)
StringBuilder sb1 = new StringBuilder("Hello");
StringBuilder sb2 = new StringBuilder("World");
sb1 = null;
sb2 = sb1;
Which is true?
A) Object containing "Hello" is unreferenced and eligible for GC
B) Object containing "World" is unreferenced and eligible for GC
C) Object containing "Hello", and "World" are both unreferenced and eligible for GC
D) No objects are eligible for GC