What three modifications, made independently, made to class greet, enable the code to compile and run?

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

Given:

#1
package handy.dandy;
public class KeyStroke {
public void typeExclamation() {
System.out.println("!")
}
}
#2
package handy; /* Line 1 */
public class Greet { /* Line 2 */
public static void main(String[] args) { /* Line 3 */
String greeting = "Hello"; /* Line 4 */
System.out.print(greeting); /* Line 5 */
Keystroke stroke = new Keystroke; /* Line 6 */
stroke.typeExclamation(); /* Line 7 */
} /* Line 8 */
} /* Line 9 */

What three modifications, made independently, made to class greet, enable the code to compile and run?

A.
Line 6 replaced with handy.dandy.keystroke stroke = new KeyStroke ( );

B.
Line 6 replaced with handy.*.KeyStroke = new KeyStroke ( );

C.
Line 6 replaced with handy.dandy.KeyStroke Stroke = new handy.dandy.KeyStroke();

D.
import handy.*; added before line 1

E.
import handy.dandy.*; added after line 1

F.
import handy.dandy,KeyStroke; added after line 1

G.
import handy.dandy.KeyStroke.typeException(); added before line 1

Explanation:

Three separate solutions:
C: the full class path to the method must be stated (when we have not imported the package)
D: We can import the hold dandy class
F: we can import the specific method



Leave a Reply 12

Your email address will not be published. Required fields are marked *


Diego Parra

Diego Parra

Must be replaced the comma for a point on the answer F

S

S

D is Wrong. Should be E. Cant have import then Package.

S

S

C,E and F are the correct answers

Kalil Peixoto

Kalil Peixoto

I agree with S, it is C,E,F. Although at F the comma has to be replaced by point (.)..

D is wrong because when importing all classes from a package, you have to name the full class path but the classes, so it would be:

import handy.dandy.*

This way all classes on the package handy.dandy are imported.

sully

sully

C E F
package must always be line 1 unless it’s not there only comments can
go ahead of it

Oene Bakker

Oene Bakker

Should be C E F

D is wrong: You will get a Keystroke cannot be resolved to a type error
You should import handy.dandy.* or handy.dandy.KeyStroke.

I agree that the comma should be replaced by a point (I saw this in another exam so I think this is a typo…)

noir

noir

Import the class and the method after.

noir

noir

KeyStroke class is from package handy.dandy, so line 6 must be rewritten to:

Handy.dandy.keystroke K = new handy.dandy.keystroke;

noir

noir

Let us look at an example that creates a package called animals. It is a good practice to use names of packages with lower case letters to avoid any conflicts with the names of classes, interfaces.

Below given package example contains interface named animals:

/* File name : Animal.java */
package animals;
interface Animal {
public void eat();
public void travel();
}

noir

noir

Note: A class file can contain any number of import statements.

The import statements must appear after the package statement and before the class declaration.

Kurt

Kurt

Yes, The correct answer is CEF