1Z0-808 · Question #51
QUESTION 62 Given the code fragment from three files: SalesMan.java: package sales; public class SalesMan { } Product.java: package sales.products; public class Product { } Market.java: 1. package…
The correct answer is E. import sales.*; import sales.products.*. Option E works because Java's wildcard import (.) covers only the exact package named - not its subpackages. import sales. brings in SalesMan (from sales), and import sales.products. separately brings in Product (from sales.products), satisfying both field declarations. Why the…
Question
- package market;
- // insert code here
- public class USMarket {
- SalesMan sm;
- Product p;
- } Which code fragment, when inserted at line 2, enables the code to compile?
Options
- Aimport sales.*;
- Bimport java.sales.products.*;
- Cimport sales; import sales.products;
- Dimport sales.; import products.;
- Eimport sales.; import sales.products.;
How the community answered
(29 responses)- A14% (4)
- B7% (2)
- D3% (1)
- E76% (22)
Explanation
Option E works because Java's wildcard import (.*) covers only the exact package named - not its subpackages. import sales.* brings in SalesMan (from sales), and import sales.products.* separately brings in Product (from sales.products), satisfying both field declarations.
Why the distractors fail:
- A -
import sales.*only imports thesalespackage;sales.productsis a distinct package and must be imported separately, leavingProductunresolved. - B - There is no
java.sales.productspackage;javais reserved for the JDK standard library, not user-defined packages. - C -
import sales;andimport sales.products;are illegal syntax; you must import either a fully-qualified class name or use.*- you cannot import a bare package name. - D -
import products.*refers to a nonexistent top-levelproductspackage; the correct fully-qualified package path issales.products.
Memory tip: Think of Java packages like nested folders - a * import only sweeps one folder, never its subfolders. Whenever a class lives in a subpackage, you need a second, explicit import for that subpackage.
Topics
Community Discussion
No community discussion yet for this question.