nerdexam
Oracle

1Z0-803 · Question #169

Given: package handy.dandy; public class Keystroke { public void typeExclamation() { System.out.println("!"); } } And 1. package handy; 2. public class Greet { 3. public static void main(String[] args

The correct answer is C. line 6 replaced with handy.dandy.Keystroke stroke = new handy.dandy.Keystroke(); E. import handy.dandy.*; added after line 1 F. import handy.dandy.Keystroke; added after line 1. This question tests the understanding of Java package and import mechanisms for accessing classes defined in different packages.

Java Basics

Question

Given: package handy.dandy; public class Keystroke { public void typeExclamation() { System.out.println("!"); } } And 1. package handy; 2. public class Greet { 3. public static void main(String[] args) { 4. String greeting = "Hello"; 5. System.out.print(greeting); 6. Keystroke stroke = new Keystroke(); 7. stroke.typeExclamation(); 8. } 9. } What three modifications, made independently, enable the code to compile and run?

Options

  • Aline 6 replaced with handy.dandy.Keystroke stroke = new Keystroke();
  • Bline 6 replaced with handy.*.Keystroke stroke = new Keystroke();
  • Cline 6 replaced with handy.dandy.Keystroke stroke = new handy.dandy.Keystroke();
  • Dimport handy.*; added before line 1
  • Eimport handy.dandy.*; added after line 1
  • Fimport handy.dandy.Keystroke; added after line 1
  • Gimport handy.dandy.Keystroke.typeExclamation(); added before line 1

How the community answered

(42 responses)
  • A
    7% (3)
  • B
    2% (1)
  • C
    74% (31)
  • D
    14% (6)
  • G
    2% (1)

Why each option

This question tests the understanding of Java package and import mechanisms for accessing classes defined in different packages.

Aline 6 replaced with handy.dandy.Keystroke stroke = new Keystroke();

While the type `handy.dandy.Keystroke` is qualified, the constructor call `new Keystroke()` remains unqualified and would look for `Keystroke` within the `handy` package, leading to a compilation error.

Bline 6 replaced with handy.*.Keystroke stroke = new Keystroke();

The syntax `handy.*.Keystroke` is an invalid package or class reference; the wildcard `*` is only permitted in `import` statements, not in class declarations.

Cline 6 replaced with handy.dandy.Keystroke stroke = new handy.dandy.Keystroke();Correct

Fully qualifying both the class type and the constructor call with its complete package path `handy.dandy.Keystroke` resolves the class location explicitly, allowing the code to compile.

Dimport handy.*; added before line 1

Importing `handy.*` only makes classes directly within the `handy` package available, not classes in its subpackages like `handy.dandy`, thus `Keystroke` remains unresolvable.

Eimport handy.dandy.*; added after line 1Correct

Adding `import handy.dandy.*;` after the package declaration makes all classes within the `handy.dandy` package, including `Keystroke`, accessible by their simple names, enabling compilation.

Fimport handy.dandy.Keystroke; added after line 1Correct

Adding `import handy.dandy.Keystroke;` after the package declaration specifically imports the `Keystroke` class, making it accessible by its simple name without needing full qualification.

Gimport handy.dandy.Keystroke.typeExclamation(); added before line 1

The syntax `import handy.dandy.Keystroke.typeExclamation();` is invalid for importing classes; imports are for types (classes, interfaces) not individual methods.

Concept tested: Java package and import statements

Source: https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html

Topics

#packages#import statement#fully qualified name#compilation error

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice