Page: 4
15. Development
16. If three versions of MyClass.java exist on a file system:
Version 1 is in /foo/bar
Version 2 is in /foo/bar/baz
Version 3 is in /foo/bar/baz/bing
And the system's classpath includes:
/foo/bar/baz
And this command line is invoked from /foo
javac -classpath /foo/bar/baz/bing:/foo/bar MyClass.java
Which version will be used by javac?
A. /foo/MyClass.java
B. /foo/bar/MyClass.java
C. /foo/bar/baz/MyClass.java
D. /foo/bar/baz/bing/MyClass.java
E. The result is not predictable.
Answer:
->D is correct. A -classpath included with a javac invocation overrides a system classpath.
When javac is using any classpath, it reads the classpath from left to right, and uses the
first match it finds.
->A, B, C, and E are incorrect based on the above.
17. Which are true? (Choose all that apply.)
A. The java command can access classes from more than one package, from a single JAR file.
B. JAR files can be used with the java command but not with the javac command.
C. In order for JAR files to be used by java, they MUST be placed in the /jre/lib/ext subdirectory
within the J2SE directory tree.
D. In order to specify the use of a JAR file on the command line, the JAR file's path and
filename MUST be included.
E. When a part of a directory tree that includes subdirectories with files is put into a JAR file, all of the files are saved in the JAR file, but the subdirectory structure is lost.
Answer:
-> A and D are correct.
->B is incorrect because javac can also use JAR files. C is incorrect because JARs can be located in ../jre/lib/ext, but they can also be accessed if they live in other locations.
E is incorrect, JAR files maintain directory structures.
18. Given two files:
package pkg;
public class Kit {
public String glueIt(String a, String b) { return a+b; }
}
import pkg.*;
class UseKit {
public static void main(String[] args) {
String s = new Kit().glueIt(args[1], args[2]);
System.out.println(s);
}
}
And the following sub-directory structure:
test
|--UseKit.class
|
com
|--KitJar.jar
If the current directory is test, and the file pkg/Kit.class is in KitJar.jar, which command line will produce the output bc ? (Choose all that apply.)
A. java UseKit b c
B. java UseKit a b c
C. java -classpath com UseKit b c
D. java -classpath com:. UseKit b c
E. java -classpath com/KitJar.jar UseKit b c
F. java -classpath com/KitJar.jar UseKit a b c
G. java -classpath com/KitJar.jar:. UseKit b c
H. java -classpath com/KitJar.jar:. UseKit a b c
Answer:
->H is correct.
->A, C, E, and G are incorrect if for no other reason than args[] is 0-based.
B, D, and F are incorrect because java needs a classpath that specifies two directories, one for the class file (the . directory), and one for the JAR file (the com directory). Remember, to find a JAR file, the c
Page: 4
1
2
3
4