Java SE 17 Developer Questions and Answers
Given the course table:
Given the code fragment:
Options:
2
false
true
1
Answer:
CExplanation:
The code fragment will execute the update statement and set the course fee of the course with ID 1021 to 5000. The executeUpdate method returns an int value that indicates the number of rows affected by the SQL statement. In this case, only one row will be updated, so the result variable will be 1. The if statement will check if the result is greater than 0, which is true, and print “Updated successfully”. Therefore, the output of the code fragment is true. References:
Which statement is true about modules?
Options:
Automatic and unnamed modules are on the module path.
Only unnamed modules are on the module path.
Automatic and named modules are on the module path.
Only named modules are on the module path.
Only automatic modules are on the module path.
Answer:
CExplanation:
A module path is a sequence of directories that contain modules or JAR files. A named module is a module that has a name and a module descriptor (module-info.class) that declares its dependencies and exports. An automatic module is a module that does not have a module descriptor, but is derived from the name and contents of a JAR file. Both named and automatic modules can be placed on the module path, and they can be resolved by the Java runtime. An unnamed module is a special module that contains all the classes that are not in any other module, such as those on the class path. An unnamed module is not on the module path, but it can read all other modules.
Given:
What is the result?
Options:
1 RAINY
Compilation fails
1 Snowy
0 CLOUDY
0 Snowy
Answer:
EExplanation:
The code is defining an enum class called Forecast with three values: SUNNY, CLOUDY, and RAINY. The toString() method is overridden to always return “SNOWY”. In the main method, the ordinal value of SUNNY is printed, which is 0, followed by the value of CLOUDY converted to uppercase, which is “CLOUDY”. However, since the toString() method of Forecast returns “SNOWY” regardless of the actual value, the output will be “0 SNOWY”. References: Enum (Java SE 17 & JDK 17), Enum.EnumDesc (Java SE 17 & JDK 17)
Given the code fragments:
Which is true?
Options:
The program prints t1 : 1: t2 : 1: t1 : t2 : 2 : in random order.
The program prints t1 : 1 : t2: 1 : t1 : 2 : t2: 2:
The program prints t1 : 1: t2 : 1: t1 : 1 : t2 : 1 : indefinitely
The program prints an exception
Answer:
BExplanation:
The code creates two threads, t1 and t2, and starts them. The threads will print their names and the value of the Atomic Integer object, x, which is initially set to 1. The threads will then increment the value of x and print their names and the new value of x. Since the threads are started at the same time, the output will be in random order. However, the final output will always be t1 : 1 : t2: 1 : t1 : 2 : t2: 2: References: AtomicInteger (Java SE 17 & JDK 17) - Oracle
Given:
What is the result?
Options:
Software Game Chess 0
Software Game Software Game Chese 2
Software game write error
Software Game Software Game chess 0
Software Game Chess 2
Software Game read error
Answer:
BExplanation:
The answer is B because the code uses the writeObject and readObject methods of the ObjectOutputStream and ObjectInputStream classes to serialize and deserialize the Game object. These methods use the default serialization mechanism, which writes and reads the state of the object’s fields, including the inherited ones. Therefore, the title field of the Software class is also serialized and deserialized along with the players field of the Game class. The toString method of the Game class calls the toString method of the Software class using super.toString(), which returns the value of the title field. Hence, when the deserialized object is printed, it shows “Software Game Software Game Chess 2”. References:
- Oracle Certified Professional: Java SE 17 Developer
- Java SE 17 Developer
- OCP Oracle Certified Professional Java SE 17 Developer Study Guide
- Serialization and Deserialization in Java with Example
Assume you have an automatic module from the module path display-ascii-0.2. jar. Which name is given to the automatic module based on the given JAR file?
Options:
Display.ascii
Display-ascii-0.2
Display-ascii
Display-ascii-0
Answer:
CExplanation:
An automatic module name is derived from the name of the JAR file when it does not contain a module-info.class file. If the JAR file has an “Automatic-Module-Name” attribute in its main manifest, then its value is the module name. Otherwise, the module name is derived from the JAR file’s name by removing any version numbers and converting it to lower case. Therefore, for a JAR named display-ascii-0.2.jar, the automatic module name would be display-ascii, following these rules.
Given the code fragment:
What is the result?
Options:
False true true optional (Newyear)
0110
True true false NewYear
010 optional (Newyear)
Answer:
AExplanation:
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 the code fragment:
What is the result:
Options:
ADEABCB // the order of element is unpredictable
ABCE
ABCDE // the order of elements is unpredictable
ABBCDE // the order of elements is unpredictable
Answer:
DExplanation:
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 code fragment:
Which code fragment invokes all callable objects in the workers set?
A)
B)
C)
D)
Options:
Option A
Option B
Option C
Option D
Answer:
CExplanation:
The code fragment in Option C invokes all callable objects in the workers set by using the ExecutorService’s invokeAll() method. This method takes a collection of Callable objects and returns a list of Future objects representing the results of the tasks. The other options are incorrect because they either use the wrong method (invokeAny() or submit()) or have syntax errors (missing parentheses or semicolons). References: AbstractExecutorService (Java SE 17 & JDK 17) - Oracle
Given:
Options:
Hello
Compilation fails
A NumberFormatException is thrown
there
Answer:
BExplanation:
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
Which two code fragments compile?
A)
B)
C)
D)
E)
Options:
Option A
Option B
Option C
Option D
Option E
Answer:
B, EExplanation:
The two code fragments that compile are B and E. These are the only ones that use the correct syntax for declaring and initializing a var variable. The var keyword is a reserved type name that allows the compiler to infer the type of the variable based on the initializer expression. However, the var variable must have an initializer, and the initializer must not be null or a lambda expression. Therefore, option A is invalid because it does not have an initializer, option C is invalid because it has a null initializer, and option D is invalid because it has a lambda expression as an initializer. Option B is valid because it has a String initializer, and option E is valid because it has an int initializer.
Given:
What is the result?
Options:
Tablechair Tablechair
Wtablechair tableChair
A RuntimeException is thrown
wTableChair TableChair
Compilation fails
Answer:
EExplanation:
The code fragment will fail to compile because the class name and the constructor name do not match. The class name is Furniture, but the constructor name is Wtable. This will cause a syntax error. The correct way to define a constructor is to use the same name as the class name. Therefore, the code fragment should change the constructor name to Furniture or change the class name to Wtable.
Given the code fragment:
Which code fragment returns different values?
Options:
int sum = listOfNumbers. parallelStream () reduce (5, Integer:: sum) ;
int sum = listOfNumbers. Stream () reduce (5, (a, b) -> a + b) ;
int sum = listOfNumbers. Stream () reduce ( Integer:: sum) ; +5;
int sum = listOfNumbers. parallelStream () reduce ({m, n) -> m +n) orElse (5) +5;
int sum = listOfNumbers. Stream () reduce (0, Integer:: sum) + 5
Answer:
CExplanation:
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:
Which two should the module-info file include for it to represent the service provider interface?
Options:
Requires cm.transport.vehicle,cars:
Provides.com.transport.vehicle.cars.Car with com.transport.vehicle.cars. impt, CatImpI;
Requires cm.transport.vehicle,cars:
Provides.com.transport.vehicle.cars.Car impl,CarImp1 to com.transport.vehicle.cars. Cars
exports com.transport.vehicle.cars.Car;
Exports com.transport.vehicle.cars;
Exports com.transport.vehicle;
Answer:
B, EExplanation:
The answer is B and E because the module-info file should include a provides directive and an exports directive to represent the service provider interface. The provides directive declares that the module provides an implementation of a service interface, which is com.transport.vehicle.cars.Car in this case. The with clause specifies the fully qualified name of the service provider class, which is com.transport.vehicle.cars.impl.CarImpl in this case. The exports directive declares that the module exports a package, which is com.transport.vehicle.cars in this case, to make it available to other modules. The package contains the service interface that other modules can use.
Option A is incorrect because requires is not the correct keyword to declare a service provider interface. Requires declares that the module depends on another module, which is not the case here.
Option C is incorrect because it has a typo in the module name. It should be com.transport.vehicle.cars, not cm.transport.vehicle.cars.
Option D is incorrect because it has a typo in the keyword provides. It should be provides, not Provides. It also has a typo in the service interface name. It should be com.transport.vehicle.cars.Car, not com.transport.vehicle.cars.Car impl. It also has an unnecessary to clause, which is used to limit the accessibility of an exported package to specific modules.
Option F is incorrect because it exports the wrong package. It should export com.transport.vehicle.cars, not com.transport.vehicle.cars.impl. The impl package contains the service provider class, which should not be exposed to other modules.
Option G is incorrect because it exports the wrong package. It should export com.transport.vehicle.cars, not com.transport.vehicle. The vehicle package does not contain the service interface or the service provider class. References:
- Oracle Certified Professional: Java SE 17 Developer
- Java SE 17 Developer
- OCP Oracle Certified Professional Java SE 17 Developer Study Guide
- Java Modules - Service Interface Module - GeeksforGeeks
- Java Service Provider Interface | Baeldung
Given:
What is the result
Options:
Marketing
Finance
Technical
Marketing
Undefined
UnDefined
Marketing
Answer:
CExplanation:
The code fragment is using the switch statement with the new Java 17 syntax. The switch statement checks the value of the variable desig and executes the corresponding case statement. In this case, the value of desig is “CTO”, which does not match any of the case labels. Therefore, the default case statement is executed, which prints “Undefined”. The other case statements are not executed, because there is no fall through in the new syntax. Therefore, the output of the code fragment is:
Undefined