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?

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 whole dandy package
F: we can import the specific class



Leave a Reply 5

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


Jazz

Jazz

“C,E,F”

C: The full class path to the method must be stated (when we have not imported the package).
E: We can import the hold dandy class.
F: we can import the specific method.

James

James

The Answer is C, E, F.

Note that:

import handy.*;

Only imports the class in the handy package. It doesn’t recursively import packages within the handy namespace.

Hans

Hans

Right and you cannot place an import before a package statement.

Naira

Naira

Curly braces are missing on line 6 after the new keyword.

Naira

Naira

Not curly sorry)