Given the code fragment:
What is the result:
Correct Answer:
D
The answer is D because the code fragment uses the Stream API to create two streams, s1 and s2, and then concatenates them using the concat() method. The resulting stream is then processed in parallel using the parallel() method, and the distinct() method is used to remove duplicate elements. Finally, the forEach() method is used to print the elements of the resulting stream to the console. Since the order of elements in a parallel stream is unpredictable, the output could be any of the options given, but option D is the most likely. References:
✑ Oracle Certified Professional: Java SE 17 Developer
✑ Java SE 17 Developer
✑ OCP Oracle Certified Professional Java SE 17 Developer Study Guide
✑ Parallelizing Streams
Given the content of the in. tart file: 23456789
and the code fragment:
What is the content of the out .txt file?
Correct Answer:
D
The answer is D because the code fragment reads the content of the in.txt file and writes it to the out.txt file. The content of the in.txt file is ??23456789??. The code fragment uses a char array buffer of size 8 to read the content of the in.txt file. The while loop reads the content of the in.txt file and writes it to the out.txt file until the end of the file is reached. Therefore, the content of the out.txt file will be ??0123456789??.
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:
Correct Answer:
B
The code fragment will fail to compile because the parseInt method of the Integer class is a static method, which means that it can be invoked without creating an object of the class. However, the code is trying to invoke the parseInt method on an object of type Integer, which is not allowed. The correct way to invoke the parseInt method is by using the class name, such as Integer.parseInt (s). Therefore, the code fragment will produce a compilation error. References: Integer (Java SE 17 & JDK 17) - Oracle
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