24 How many times is the word true printed?var s1 = "Java"; var s2 = "Java"; var s3 = s1.indent(1).strip(); var s4 = s3.intern(); var sb1 = new StringBuilder(); sb1.append("Ja").append("va"); System.out.println(s1 == s2); System.out.println(s1.equals(s2)); System.out.println(s1 == s3); System.out.println(s1 == s4); System.out.println(sb1.toString() == s1); System.out.println(sb1.toString().equals(s1));OnceTwiceThree timesFour timesFive timesThe code does not compile.
25 What is the output of the following program?1: class Deer { 2: public Deer() {System.out.print("Deer");} 3: public Deer(int age) {System.out.print("DeerAge");} 4: protected boolean hasHorns() { return false; } 5: } 6: public class Reindeer extends Deer { 7: public Reindeer(int age) {System.out.print("Reindeer");} 8: public boolean hasHorns() { return true; } 9: public static void main(String[] args) { 10: Deer deer = new Reindeer(5); 11: System.out.println("," + deer.hasHorns()); 12: } }ReindeerDeer,falseDeerAgeReindeer,trueDeerReindeer,trueDeerReindeer,falseReindeerDeer,trueDeerAgeReindeer,falseThe code will not compile because of line 4.The code will not compile because of line 12.
26 Which of the following are true? (Choose all that apply.)private static void magic(Stream<Integer> s) { Optional o = s .filter(x -> x < 5) .limit(3) .max((x, y) -> x-y); System.out.println(o.get()); }magic(Stream.empty()); runs infinitely.magic(Stream.empty()); throws an exception.magic(Stream.iterate(1, x -> x++)); runs infinitely.magic(Stream.iterate(1, x -> x++)); throws an exception.magic(Stream.of(5, 10)); runs infinitely.magic(Stream.of(5, 10)); throws an exception.The method does not compile.
27 Assuming the following declarations are top-level types declared in the same file, which successfully compile? (Choose all that apply.)record Music() { final int score = 10; } record Song(String lyrics) { Song { this.lyrics = lyrics + "Hello World"; } } sealed class Dance {} record March() { @Override String toString() { return null; } } class Ballet extends Dance {}MusicSongDanceMarchBalletNone of them compile.
28 Which of the following expressions compile without error? (Choose all that apply.)int monday = 3 + 2.0;double tuesday = 5_6L;boolean wednesday = 1 > 2 ? !true;short thursday = (short)Integer.MAX_VALUE;long friday = 8.0L;var saturday = 2_.0;None of the above
29 What is the result of executing the following application?final var cb = new CyclicBarrier(3, () -> System.out.println("Clean!")); // u1 ExecutorService service = Executors.newSingleThreadExecutor(); try { IntStream.generate(() -> 1) .limit(12) .parallel() .forEach(i -> service.submit(() -> cb.await())); // u2 } finally { service.shutdown(); }It outputs Clean! at least once.It outputs Clean! exactly four times.The code will not compile because of line u1.The code will not compile because of line u2.It compiles but throws an exception at runtime.It compiles but waits forever at runtime.
30 Which statement about the following method is true?5: public static void main(String… unused) { 6: System.out.print("a"); 7: try (StringBuilder reader = new StringBuilder()) { 8: System.out.print("b"); 9: throw new IllegalArgumentException(); 10: } catch (Exception e || RuntimeException e) { 11: System.out.print("c"); 12: throw new FileNotFoundException(); 13: } finally { 14: System.out.print("d"); 15: } }It compiles and prints abc.It compiles and prints abd.It compiles and prints abcd.One line contains a compiler error.Two lines contain a compiler error.Three lines contain a compiler error.It compiles but prints an exception at runtime.
Answers to Assessment Test
1 G. The question does not compile because line 44 and line 47 do not always return a value in the case block, which is required when a switch expression is used in an assignment operation. Line 44 is missing a yield statement when the if statement evaluates to false, while line 47 is missing a yield statement entirely. Since two lines don't compile, option G is the answer. For more information, see Chapter 3.
2 B. Initially, moon is assigned a value of 9, while star is assigned a value of 8. The multiplication operator (*) has a higher order of precedence than the addition operator (+), so it gets evaluated first. Since star is not greater than 10, sun is assigned a value of 3, which is promoted to 3.0f as part of the assignment. The value of jupiter is (3.0f + 9) - 1.0, which is 11.0f. This value is implicitly promoted to double when it is assigned. In the last assignment, moon is decremented from 9 to 8, with the value of the expression returned as 8. Since 8 less than or equal to 8 is true, mars is set to a value of 2. The final output is 3.0, 11.0, 2, making option B the correct answer. Note that while Java outputs the decimal for both float and double values, it does not output the f for float values. For more information, see Chapter 2.
3 B, C, E. The code may print 100 without any changes, but since the data class is not thread-safe, this behavior is not guaranteed. For this reason, option F is incorrect. Option A is also incorrect, as volatile does not guarantee thread-safety. Options B and C are correct, as they both cause the stream to apply the add() operation in a synchronized manner. Option D is incorrect, as serial() is not a stream method. Option E is correct. Synchronization will cause each thread to wait so that the List is modified one element at a time. For more information, see Chapter 13.
4 D. First, this mess of code does compile. However, the source is an infinite stream. The filter operation will check each element in turn to see whether any are not empty. While nothing passes the filter, the code does not terminate. Therefore, option D is correct. For more information, see Chapter 10.
5 B. The code compiles successfully, so options D and E are incorrect. The value of a cannot be changed by the addToInt() method, no matter what the method does, because only a copy of the variable is passed into the parameter x. Therefore, a does not change, and the output on line 9 is 15 which is option B. For more information, see Chapter 5.
6 C. Java will use Penguin_en.properties as the matching resource bundle on line 7. Since there is no match for French, the default locale is used. Line 8 finds a matching key in the Penguin_en.properties file. Line 9 does not find a match in the Penguin_en.properties file; therefore, it has to look higher up in the hierarchy to Penguin.properties. This makes option C the answer. For more information, see Chapter 11.
7 D, E. The array is allowed to use an anonymous initializer because it is in the same line as the declaration. The results of the binary search are undefined since the array is not sorted. Since the question asks about guaranteed output, options A and B are incorrect. Option D is correct because the compare() method returns 0 when the arrays are the same length and have the same elements. Option E is correct because the mismatch() method returns a -1 when the arrays are equivalent. For more information, see Chapter 4.
8 C, E, F. First, note that option A is incorrect because the interface should be BiPredicate and not BinaryPredicate. Line 6 requires you to know that negate() is a convenience method on Predicate. This makes option E correct. Line 7 takes zero parameters and doesn't return anything, making it a Runnable. Remember that Runnable doesn't use generics. This makes option F correct. Finally, line 8 takes two parameters and returns an int. Option C is correct. Comparable is there to mislead you since it takes only one parameter in its single abstract method. For more information, see Chapter 8.
9 D. If this were a valid module-info.java file, it would need to be placed at the root directory of the module, which is option A. However, a module is not allowed to use the public access modifier. Option D is correct because the provided file does not compile regardless of placement in the project. For more information, see Chapter 12.
10 C. The getTailLength() method in the interface is private; therefore, it must include