-
Notifications
You must be signed in to change notification settings - Fork 0
/
Questions.txt
723 lines (622 loc) · 15.5 KB
/
Questions.txt
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
Q) Which are legal:
A) class X {
var x = 99;
}
B) void doStuff(var x) { }
C) void doStuff() {
var x;
x = 100;
}
D) void doStuff() {
var x = 100;
}
E) void doStuff() {
var x = null;
}
Q) Which are legal:
A) for (var x = 0; x < 3; x++)
System.out.println(x);
B) try (var in = new FileReader("");
var out = new FileWriter("")) {
}
C) try (FileReader in = new FileReader("")) {
} catch (var ex) { }
D) void doStuff() {
var x = new int[]{ 1, 2, 3 };
}
E) void doStuff() {
var x = { 1, 2, 3 };
}
Q) Given:
var x = true ? "99" : 99;
What is the type of x?
A) none, it's fails to compile
B) int
C) String
D) Object
E) none of the above
Q) Given:
public static void main(String[] args) {
final StringBuilder sb = new StringBuilder("Hello");
sb.append(" world!");
System.out.println(sb);
}
What is the result?
A) Compilation fails
B) Hello
C) Hello world!
D) Exception at runtime
Q) Given:
String message = """Hello\
Java 17 World!""";
System.out.println(message + "XXX");
What is the result?
A) Compilation fails
B) Exception at runtime
C) Hello
Java 17 World!XXX
D) Hello Java 17 World!XXX
E) Hello Java 17 World!
XXX
Q) Given:
String message = """
Hello
Java 17 World!""";
System.out.println(message + "XXX");
What is the result?
A)
Hello
Java 17 World!XXX
B)
Hello
Java 17 World!XXX
C)
Hello
Java 17 World!
XXX
D)
Hello
Java 17 World!
XXX
Q)
int i = 3, j = 12, k = 2;
System.out.println(i + j / k - i);
What is the result?
A) -15
B) 4
C) 6
D) -9
Q)
int x = 10;
System.out.println("value is " + (x++ = 99));
What is the result?
A) 10
B) 11
C) 99
D) 109
E) Compilation fails
Q) Given:
int j = 0;
for (int i = 0; i < 3; i++) {
if (i > ++j && i % 2 == 0) ++i = ++j;
if (j > i + 2) continue;
++j;
System.out.println(i + ", " + j);
}
What is the result?
A) 0, 2
1, 4
B) 0, 2
C) 1, 4
D) The program runs forever
E) Compilation fails
Q) Given:
public static boolean getValue() {
System.out.println("Getting value");
return true;
}
and:
boolean b1 = false;
System.out.println(b1 & getValue());
What is the result:
A) Compilation fails
B) false
C) true
D) Getting value
false
E) Getting value
true
Q) Given:
int x = -1;
System.out.println(x & 5);
What is the result?
A) Compilation fails
B) Exception at runtime
C) -1
D) 0x00000005
E) 5
Q) Which print "true"
A) String s = "Hello";
String t = "He";
t += "llo";
sout(s == t);
B) String s = "Hello";
String t = "He";
t += "llo";
sout(s.equals(t));
C) StringBuilder s = new StringBuilder("Hello");
StringBuilder t = new StringBuilder("He");
t.append("llo");
sout(s.equals(t));
D) List<String> ls = List.of("Fred", "Jim");
List<String> ls2 = new ArrayList<>(ls);
sout(ls.equals(ls2));
E) LocalDate ld = LocalDate.of(2021, 3, 29);
LocalDate ld2 = LocalDate.of(2021, 3, 29);
sout(ld.equals(ld2));
Q) Which are true?
String s1 = "Hello";
String s2 = new StringBuilder("Hello").toString();
String s3 = s2.intern();
and, in another jarfile:
class X {
static String h = "Hel" + "lo";
}
A) s1 == s2
B) s2 == s3
C) s1 == s3
D) X.h == s1
E) None of the above
Q) Given
class Thing {
int x = 99;
static void showIt() {
int x = 98;
System.out.println("x is " + x);
}
}
What is the effect of:
Thing.showIt();
A) Compilation fails
B) Exception
C) 99
D) 98
Q) 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"); }
}
and:
Base b = new Sub();
((Runnable) b).run();
What is the result?
A) doSubStuff
B) doOtherStuff
C) doBaseStuff
D) Exception at runtime
E) Compilation failure
Q) Given:
Object obj = "Hello";
boolean answered = false;
if (obj instanceof String) {
String str = (String)obj;
if (str.length() > 3) {
System.out.println(str);
answered = true;
}
}
if (!answered) System.out.println("Nope");
Which replace the if clause to produce the same output?
A) System.out.println(obj instanceof String str && str.length() > 3 ? str : "Nope");
B) if (obj instanceof String str when str.length() > 3) System.out.println(str);
else System.out.println("Nope");
C) if (obj instanceof String str if str.length() > 3) System.out.println(str);
else System.out.println("Nope");
D) System.out.println(obj instanceof String.class str && str.length() > 3 ? str : "Nope");
E) if (!(obj instanceof String str) || str.length() <= 3) System.out.println("Nope");
else System.out.println(str);
Q) Given:
String s = "Hello";
switch (s) {
case "Hello" ->
System.out.print("Bonjour "); // line n1
System.out.print("Guten Tag ");
default -> System.out.print("Bye");
}
Which is true?
A) The code prints: Bonjour Guten Tag Bye
B) The code prints: Bonjour Guten Tag
C) If line n1 is removed the code prints: Guten Tag Bye
D) If line n1 is removed the code prints: Guten Tag
E) The code fails to compile
Q)
int x = 1;
// line n1
switch(x) {
case 1: LocalDate.of(2023, x, 1);
default: LocalDate.of(2022, 12, 1);
};
Which are true?
A) The code compiles
B) The code throws an exception at runtime
C) The code compiles if line n1 is replaced with
var d =
D) The code compiles if the word "yield" is inserted
after the colon in both the case and default
E) The code compiles if the changes of both C and D
are applied
Q) Given
void doStuff() throws IOException {};
void doStuff2() throws FileNotFoundException {};
And:
void tryStuff() {
try {
doStuff();
doStuff2();
} enter code here {
// handle both exceptions
}
}
What can be inserted at "enter code here"
to provide handling of exceptions from both
methods?
A) catch (IOException | FileNotFoundException e)
B) catch (FileNotFoundException | IOException e)
C) catch (FileNotFoundException e)
D) catch (IOException e)
E) finally
Q) Given
void doStuff() throws SQLException {};
void doStuff2() throws FileNotFoundException {};
And:
void tryStuff() /*...insert here...*/ {
try {
doStuff();
doStuff2();
} catch (SQLException | FileNotFoundException e) {
throw e;
}
}
What is best at "...insert here..."?
A) throws Exception
B) throws SQLException
C) throws FileNotFoundException
D) throws FileNotFoundException, SQLException
E) nothing is needed, compilation succeeds as is
Q) Given:
class AC implements AutoCloseable {
private String name;
public AC(String name) { this.name = name; }
@Override public void close() {
System.out.print("Closing " + name);
}
}
And:
var ac0 = new AC("zero");
try (
var ac1 = new AC("one");
var ac2 = new AC("two");
ac0;
) {
}
}
What is the result?
A) Compilation fails
B) Closing zero Closing two Closing one
C) Closing zero Closing one Closing two
D) Closing two Closing one Closing zero
E) Closing one Closing two Closing zero
Q) Given
class AC4 implements AutoCloseable {
@Override
public void close() throws Exception {
throw new SQLException();
}
}
And:
try (
var one = new AC4();
var two = new AC4();
){
throw new FileNotFoundException();
}
Which are true?
A) Compilation fails
B) A FileNotFoundException is thrown to the caller
C) An SQLException is thrown to the caller
D) close methods of resources "one" and "two" are called
E) close methods of resources "one" and "two" are not both called
Q) Given
public int getValue() {return 1;}
public CharSequence getText() {return null;}
which of these methods may individually be added to a subclass of this class?
A) public int getValue(int x) {return 1;}
B) public String getValue() {return "Hello";}
C) public String getValue(int x) {return "Hello";}
D) public StringBuilder getText() { return null; }
E) public Object getText() { return ""; }
Q) Given:
void doStuff(int x, long y) {} // Method B
void doStuff(long x, int y) {} // Method C
void doStuff(int ... x) {} // Method D
void doStuff(Integer x, Integer y) {} // Method E
and:
doStuff(1, 2);
What happens:
A) compilation fails
B) method B is called
C) method C is called
D) method D is called
E) method E is called
Q)Given:
enum Day {
MONDAY(1), TUESDAY;
public Day(int d) {}
public Day() {}
}
and:
sout(Day.MONDAY)
What is the result?
A) 1
B) MONDAY
C) Day.MONDAY
D) Exception at runtime
E) Compilation fails
Q) Given:
public void doStuff() {
int len = 3;
Predicate<String> ps = new Predicate<String>() {
@Override
public boolean test(String s) {
return s.length() > len; // line n1
}
};
// line n2
}
Which are true?
A) The code compiles
B) Compilation fails due to a problem at line n1
C) adding the code
System.out.println("testing lines " + (++len));
at line n2 is OK
Q) Given:
class Outer1 {
class Inner1 {
private int y = 100;
// line n1
}
private int x = 99;
// line n2
}
and these method proposals:
void showX() { sop("x is " + x); }
void showY() { sop("y is " + y); }
void showAnotherY(Inner1 another) { sop("y is " + another.y); }
static Inner1 makeOne() { return new Inner1(); }
Which is/are true?
A) showX can be added at line n1
B) showY can be added at line n2
C) showAnotherY can be added at line n2
D) makeOne can be added at line n1
E) makeOne can be added at line n2
Q) Given:
record Client(String name, int creditLimit) {
line n1
}
Which can be added:
A) At line n1
public Client(String name) {
this(name, 0);
}
B) at line n1
public Client(String name, int creditLimit) {
super();
this.name = name;
this.creditLimit = creditLimit;
}
C) as a separate declaration:
record SpecialClient(String name, int creditLimit, String greeting)
extends Client(name, creditLimit) {}
D) at line n1
private Client() {
this("Unknown", 0);
}
E) at line n1
Client {}
Q)
interface Int1 {
default void doStuff() {
System.out.println("Int1.doStuff()");
}
}
class Cl1 implements Int1 {
void doStuff() {
System.out.println("Cl1.doStuff");
}
}
What is the result of a call:
new Cl1().doStuff();
A) Compilation fails
B) RuntimeException
C) Int1.doStuff()
D) Cl1.doStuff()
E) Int1.doStuff()
Cl1.doStuff()
Q) Given:
interface IntX {
default void doStuff() {
System.out.println("IntX.doStuff()");
}
}
interface IntY {
default void doStuff() {
System.out.println("IntY.doStuff()");
}
}
class ClQ implements IntX, IntY {}
What is the result of a call:
new ClQ().doStuff();
A) Compilation fails
B) RuntimeException
C) IntX.doStuff()
D) IntY.doStuff()
E) C1QdoStuff()
Q) Given:
class Parent {
Parent(int x) { y += x; out.print(", P-c: y is " + y); }
static { out.print(", P-si: x is " + Parent.x); }
static int x = 99;
int y = 100;
{ y++; out.print(", P-i: y is " + y); }
}
class Child extends Parent {
int x = 200;
static int y = 300;
Child() { super(x); }
Child(int x) { this(); }
{ out.print(", C-i x: is " + x); }
static { out.print(", C-si: y is " + y); }
}
class InitOrder3 {
public static void main(String[] args) {
out.print("Hello!");
new Child(-1);
out.println(" Goodbye!");
}
}
What is the result?
A) Compilation fails
B) Exception at runtime
C) Hello!, P-si: x is 0, C-si: y is 300, P-i: y is 101, P-c: y is 401, C-i x: is 200 Goodbye!
D) , P-si: x is 0, C-si: y is 300Hello!, P-i: y is 101, P-c: y is 401, C-i x: is 200 Goodbye!
E) Hello!, P-si: x is 99, C-si: y is 300, P-i: y is 101, P-c: y is 100Goodbye!
Q) Given:
class Parent {
Parent(int x) { y += x; out.print(", P-c: y is " + y); }
static { out.print(", P-si: x is " + Parent.x); }
static int x = 99;
int y = 100;
{ y++; out.print(", P-i: y is " + y); }
}
class Child extends Parent {
int x = 200;
static int y = 300;
Child() { super(y); }
Child(int x) { this(); }
{ out.print(", C-i x: is " + x); }
static { out.print(", C-si: y is " + y); }
}
class InitOrder3 {
public static void main(String[] args) {
out.print("Hello!");
new Child(-1);
out.println(" Goodbye!");
}
}
What is the result?
A) Compilation fails
B) Exception at runtime
C) Hello!, C-si: y is 300, P-i: y is 101, P-c: y is 401, C-i x: is 200 Goodbye!
D) , P-si: x is 0, C-si: y is 300Hello!, P-i: y is 101, P-c: y is 401, C-i x: is 200 Goodbye!
E) Hello!, P-si: x is 99, C-si: y is 300, P-i: y is 101, P-c: y is 100Goodbye!
Q) Given:
interface StudentCriterion {
boolean test(Student s);
}
and:
class Student {
public int getGpa() { return 0; }
public List<String> getCourses() { return null; }
}
Which are legal (individually):
A) StudentCriterion sc = Student s -> { return s.getGpa() > 3;};
B) StudentCriterion sc = s -> { s.getGpa() > 3 };
C) Object sc = s -> {return s.getGpa() > 3;};
D) StudentCriterion sc = (s) -> s.getGpa() > 3;
E) StudentCriterion sc = s -> s.getCourses().size();
Q) Which are legal individually:
A) Function<String, String> fss = s -> { System.out.println(s); };
B) Consumer<String> cs = s -> { System.out.println(s); };
C) Supplier<String> ss = (s1, s2) -> s1 + s2;
D) BiPredicate<String> bps = (String s) -> { return s.length() > 0; };
E) ToIntFunction<String> tofs = s -> s.length();
Q) Given:
???? (s, t) -> s.length() - t.length();
Which are correct inserted in place of ????
A) BiFunction<String, String, Integer> f =
B) BiPredicate<String> f =
C) Comparator<String> f =
D) Supplier<String, Integer> f =
E) BinaryOperator<String> f =
Q) Given, in a single file X.java:
interface ArbInter {}
final class Y implements X {}
sealed interface X /* point x */{}
// line n1
Which is/are true?
A) the code compiles as it stands
B) the code compiles if the following is added at point x
permits Y, Z
AND this is added at line 1
class Z implements X {}
C) the code compiles if the following is added at point x
permits Y, Z
AND this is added at line 1
record Z(String name) implements X {}
D) the code compiles if the following is added at point x
permits Y extends ArbInter
E) the code compiles if the following is added at line n1
non-sealed abstract class Z implements X permits Q {}
final class Q extends Z {}
Q) Given, rooted on the base of the classpath:
---file: localization/MyResources.properties
name=Fred
-----------------------------------------------
---file: localization/MyResources_de.properties
name=Helmut
-----------------------------------------------
and:
Locale.setDefault(Locale.FRENCH);
ResourceBundle bundle = PropertyResourceBundle.getBundle(
"MyResources");
System.out.println("Name is " + bundle.getString("name"));
What is the result?
A) Name is Helmut
B) Name is Fred
C) Name is null
D) an exception is thrown
E) compilation fails
Q) Given:
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
Q) Given:
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) No objects are eligible for GC
Q) Given:
static String doStuff(String s) {
s = new String("Hello");
return new String(s);
}
and:
String y = doStuff("Hello");
// line n1
how many String objects are eligible for GC at line n1
A) 0
B) 1
C) 2
D) 3