nerdexam
Oracle

1Z0-829 · Question #48

Given: ``java package com.transport.vehicle.cars; public interface Car { int getSpeed(); } ` and `java package com.transport.vehicle.cars.impl; import com.transport.vehicle.cars.Car; public class…

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…

Working with Modules

Question

Given:
package com.transport.vehicle.cars;

public interface Car {
 int getSpeed();
}
and
package com.transport.vehicle.cars.impl;

import com.transport.vehicle.cars.Car;

public class CarImpl implements Car {
 private int speed;

 public CarImpl() {
 this(10);
 }

 public CarImpl(int speed) {
 this.speed = speed;
 }

 @Override
 public int getSpeed() {
 return speed;
 }
}
Which two should the module-info file include for it to represent the service provider interface?

Explanation

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.

Topics

#module-info#service provider interface#provides directive#uses directive

Community Discussion

No community discussion yet for this question.

Full 1Z0-829 Practice