Given the code fragment:
What is the result?
Correct Answer:A
The code fragment is using the stream methods allMatch, anyMatch, noneMatch, and findFirst on a list of strings called specialDays. These methods are used to perform matching operations on the elements of a stream, such as checking if all, any, or none of the elements satisfy a given predicate, or finding the first element that matches a predicate1. The predicate in this case is that the string equals ??Labour?? or ??Halloween??. The output will be:
✑ False: because not all of the elements in specialDays are equal to ??Labour?? or ??Halloween??.
✑ true: because at least one of the elements in specialDays is equal to ??Labour?? or ??Halloween??.
✑ true: because none of the elements in specialDays are equal to both ??Labour?? and ??Halloween??.
✑ Optional[NewYear]: because the first element in specialDays that matches the predicate is ??NewYear??, and the findFirst method returns an Optional object that may or may not contain a non-null value2.
References: Stream (Java SE 17 & JDK 17), Optional (Java SE 17 & JDK 17)
Given:
Which statement is true while the program prints GC?
Correct Answer:B
Given the code fragment:
Which code fragment returns different values?
Correct Answer:C
The answer is C because the code fragment uses a different syntax and logic for the reduce operation than the other options. The reduce method in option C takes a single parameter, which is a BinaryOperator that combines two elements of the stream into one. The method returns an Optional, which may or may not contain a value depending on whether the stream is empty or not. The code fragment then adds 5 to the result of the reduce method, regardless of whether it is present or not. This may cause an exception if the Optional is empty, or produce a different value than the other options if the Optional is not empty.
The other options use a different syntax and logic for the reduce operation. They all take two parameters, which are an identity value and a BinaryOperator that combines an element of the stream with an accumulator. The method returns the final accumulator value, which is equal to the identity value if the stream is empty, or the result of applying the BinaryOperator to all elements of the stream otherwise. The code fragments then add 5 to the result of the reduce method, which will always produce a valid value.
For example, suppose listOfNumbers contains [1, 2, 3]. Then, option A will perform the following steps:
✑ Initialize accumulator to identity value 5
✑ Apply BinaryOperator Integer::sum to accumulator and first element: 5 + 1 = 6
✑ Update accumulator to 6
✑ Apply BinaryOperator Integer::sum to accumulator and second element: 6 + 2 = 8
✑ Update accumulator to 8
✑ Apply BinaryOperator Integer::sum to accumulator and third element: 8 + 3 = 11
✑ Update accumulator to 11
✑ Return final accumulator value 11
✑ Add 5 to final accumulator value: 11 + 5 = 16
Option B will perform the same steps as option A, except using a lambda expression instead of a method reference for the BinaryOperator. Option D will perform the same steps as option A, except using parallelStream instead of stream, which may change the order of applying the BinaryOperator but not the final result. Option E will perform the same steps as option A, except using identity value 0 instead of 5.
Option C, however, will perform the following steps:
✑ Apply BinaryOperator Integer::sum to first and second element: 1 + 2 = 3
✑ Apply BinaryOperator Integer::sum to previous result and third element: 3 + 3 = 6
✑ Return Optional containing final result value 6
✑ Add 5 to Optional value: Optional.of(6) + 5 = Optional.of(11)
As you can see, option C produces a different value than the other options, and also uses a different syntax and logic for the reduce operation. References:
✑ Oracle Certified Professional: Java SE 17 Developer
✑ Java SE 17 Developer
✑ OCP Oracle Certified Professional Java SE 17 Developer Study Guide
✑ Guide to Stream.reduce()
Given the code fragment:
What is the result?
Correct Answer:B
The code fragment is creating a string variable ??a?? with the value ??Hello! Java??. Then, it is printing the index of ??Java?? in ??a??. Next, it is replacing ??Hello!?? with ??Welcome!?? in ??a??. Then, it is printing the index of ??Java?? in ??a??. Finally, it is creating a new StringBuilder object ??b?? with the value of ??a?? and printing the index of ??Java?? in ??b??. The output will be 8109 because the index of ??Java?? in ??a?? is 8, the index of ??Java?? in ??a?? after replacing ??Hello!?? with ??Welcome!?? is 10, and the index of ??Java?? in ??b?? is 9. References: Oracle Java SE 17 Developer source and documents: [String (Java SE 17 & JDK 17)], [StringBuilder (Java SE 17 & JDK 17)]
Given:
What is the result?
Correct Answer:C
The answer is C because the code demonstrates the concept of method overloading and type conversion in Java. Method overloading allows different methods to have the same name but different parameters. Type conversion allows values of one data type to be assigned to another data type, either automatically or explicitly. In the code, the class Test has four methods named sum, each with different parameter types: int, float, and double. The main method creates an instance of Test and calls the sum method with different arguments. The compiler will choose the most specific method that matches the arguments, based on the following rules:
✑ If there is an exact match between the argument types and the parameter types, that method is chosen.
✑ If there is no exact match, but there is a method with compatible parameter types, that method is chosen. Compatible types are those that can be converted from one to another automatically, such as int to long or float to double.
✑ If there is more than one method with compatible parameter types, the most specific method is chosen. The most specific method is the one whose parameter types are closest to the argument types in terms of size or precision.
In the code, the following method calls are made:
✑ test.sum(10, 10.5) -> This matches the sum(int a, float b) method exactly, so it is chosen. The result is 20.5, which is converted to int and printed as 20 (B).
✑ test.sum(10) -> This does not match any method exactly, but it matches the sum(double a) method with compatible types, as int can be converted to double automatically. The result is 10.0, which is printed as 10 (A).
✑ test.sum(10.5, 10) -> This does not match any method exactly, but it matches two methods with compatible types: sum(float a, float b) and sum(double a, double b). The latter is more specific, as double is closer to the argument types than float. The result is 20.5, which is printed as 20 (D).
Therefore, the output is B A D. References:
✑ Oracle Certified Professional: Java SE 17 Developer
✑ Java SE 17 Developer
✑ OCP Oracle Certified Professional Java SE 17 Developer Study Guide
✑ Method Overloading in Java
✑ Type conversion in Java with Examples
✑ Java Method Overloading with automatic type conversions