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
Must be replaced the comma for a point on the answer F
D is Wrong. Should be E. Cant have import then Package.
C,E and F are the correct answers
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.
C E F
package must always be line 1 unless it’s not there only comments can
go ahead of it
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…)
Import the class and the method after.
KeyStroke class is from package handy.dandy, so line 6 must be rewritten to:
Handy.dandy.keystroke K = new handy.dandy.keystroke;
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();
}
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.
Yes, The correct answer is CEF
cef