Given the code fragment:
What is the result?
Correct : D
The answer is E because the code fragment creates a new Pet object with the name ''Dog'' and assigns it to the variable p. Then, it assigns p to p1. Next, it changes the name of p1 to ''Cat''. Then, it assigns p1 to p. Finally, it sets p to null and prints the name of p and p1. The output will be ''Cat'' and ''null'' because p is set to null and p1 still points to the Pet object with the name ''Cat''.
Start a Discussions
Given:
Which two modifications enable the code to print Open Close?
A)
B)
C)
D)
E)
Correct : B, E
The code given is a try-with-resources statement that declares a resource of type AutoCloseable. The resource is an anonymous class that implements the AutoCloseable interface and overrides the close() method. The code also has a print() method that prints the value of the variable s. The code is supposed to print ''Open Close'', but it does not compile because of two errors.
The first error is at line n1, where the anonymous class is missing a semicolon at the end of its declaration. This causes a syntax error and prevents the code from compiling. To fix this error, option B adds a semicolon after the closing curly brace of the anonymous class.
The second error is at line n2, where the print() method is called without an object reference. This causes a compilation error because the print() method is not static and cannot be invoked without an object. To fix this error, option E adds an object reference to the print() method by using the variable t.
Therefore, options B and E are correct and enable the code to print ''Open Close''.
Start a Discussions
Given:
Which statement is true?
Correct : B
The answer is B because the code fragment contains a syntax error that prevents it from compiling. The code fragment tries to catch a StockException in line 10, but the catch block does not have a parameter of type StockException. The catch block should have a parameter of type StockException, such as:
catch (StockException e) { // handle the exception }
This is required by the Java syntax for the catch clause, which must have a parameter that is a subclass of Throwable. Without a parameter, the catch block is invalid and causes a compilation error.
Option A is incorrect because the program does not throw a StockException, as it does not compile.
Option C is incorrect because the program does not throw an OutofStockException, as it does not compile.
Option D is incorrect because the program does not throw a ClassCastException, as it does not compile.Reference:
Oracle Certified Professional: Java SE 17 Developer
OCP Oracle Certified Professional Java SE 17 Developer Study Guide
The try-with-resources Statement (The Java Tutorials > Essential Classes > Exceptions)
The catch Blocks (The Java Tutorials > Essential Classes > Exceptions)
Start a Discussions
Which statement is true about modules?
Correct : C
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.
Start a Discussions
Given:
Which two should the module-info file include for it to represent the service provider interface?
Correct : B, E
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.Reference:
Oracle Certified Professional: 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
Start a Discussions