1. | Given these classes in different files: package xcom; public class Useful { int increment(int x) { return ++x; } } import xcom.*; // line 1 class Needy3 { public static void main(String[] args) { xcom.Useful u = new xcom.Useful(); // line 2 System.out.println(u.increment(5)); } } Which statements are true? (Choose all that apply.) -
The output is 0. -
The output is 5. -
The output is 6. -
Compilation fails. -
The code compiles if line 1 is removed. -
The code compiles if line 2 is changed to read Useful u = new Useful( ); | |
2. | Given the following directory structure: org | -- Robot.class | | -- ex |-- Pet.class | |-- why |-- Dog.class And the following source file: class MyClass { Robot r; Pet p; Dog d; } Which statement(s) must be added for the source file to compile? (Choose all that apply.) -
package org; -
import org.*; -
package org.*; -
package org.ex; -
import org.ex.*; -
package org.ex.why; -
package org.ex.why.Dog; | |
3. | Given: 1. // insert code here 2. class StatTest { 3. public static void main(String[] args) { 4. System.out.println(Integer.MAX VALUE); 5. } 6. } Which, inserted independently at line 1, compiles? (Choose all that apply.) -
import static java.lang; -
import static java.lang.Integer; -
import static java.lang.Integer.*; -
import static java.lang.Integer.* VALUE; -
import static java.lang.Integer.MAX_VALUE; -
None of the above statements are valid import syntax. | |
4. | Given: import static java.lang.System.*; class _ { static public void main(String... __A_V_) { String $ = ""; for(int x=0; ++x < __A_V_.length; ) $ += __A_V_ [x]; out.println($); } } And the command line: java _ - A . What is the result? -
-A -
A. -
-A. -
-A. -
_-A. -
Compilation fails. -
An exception is thrown at runtime. | |
5. | Given the default classpath: /foo And this directory structure: foo | test | xcom |--A.class |--B.java And these two files: package xcom; public class A { } package xcom; public class B extends A { } Which allows B.java to compile? (Choose all that apply.) -
Set the current directory to xcom then invoke javac B.java -
Set the current directory to xcom then invoke javac -classpath . B.java -
Set the current directory to test then invoke javac -classpath . xcom/B.java -
Set the current directory to test then invoke javac -classpath xcom B.java -
Set the current directory to test then invoke javac -classpath xcom:. B.java | |
6. | Given two files: package xcom; public class Stuff { public static final int MY_CONSTANT 5; public static int doStuff(int x) ( return (x++)*x; } } impost xcom.Stuff.*; import java.lang.System.out; class User { public static void main(String[] args) { new User().go(); } void go() { out.println(doStuff(MY_CONSTANT)); } } What is the result? -
25 -
30 -
36 -
Compilation fails. -
An exception is thrown at runtime. | |
7. | Given two files: a=b.java c_d.class Are in the current directory, which command-line invocation(s) could complete without error? (Choose all that apply.) -
java -Da=b c_d -
java -D a=b c_d -
javac -Da=b c_d -
javac -D a=b c_d | |
8. | Given three files: package xcom; public class A { // insert code here } package xcom; public class B extends A {public void doB() { System.out.println("B.doB"); } } import xcom.B; class TestXcom { public static void main(String[] args) { B b = new B(); b.doB(); b.go(); } } Which, inserted at // insert code here will allow all three files to compile? (Choose all that apply.) -
void go() { System.out.println("a.go"); } -
public void go() { System.out.println("a.go"); } -
private void go() { System.out.println("a.go"); } -
protected void go() { System.out.println("a.go"); } -
None of these options will allow the code to compile. | |
9. | Given: class TestProps { public static void main(String[] args) { String s = System.getProperty("aaa","bbb"); } } And the command-line invocation: Java -Daaa=ccc TestProps What is always true? (Choose all that apply.) -
The value of property aaa is aaa. -
The value of property aaa is bbb. -
The value of property aaa is ccc. -
The value of property bbb is aaa. -
The value of property bbb is ccc. -
The invocation will not complete without error. | |
10. | If three versions of Myclass.java exist on a file system: 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? -
/foo/MyClass.java -
/foo/bar/MyClass.java -
/foo/bar/baz/MyClass.java -
/foo/bar/baz/bing/MyClass.java -
The result is not predictable. | |
11. | Which are true? (Choose all that apply.) -
The java command can access classes from more than one package, from a single JAR file. -
JAR files can be used with the java command but not with the javac command. -
In order for JAR files to be used by java, they MUST be placed in the /jre/lib/ext sub-directory within the J2SE directory tree. -
In order to specify the use of a JAR file on the command line, the JAR file's path and filename MUST be included. -
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. | |
12. | 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.) -
java UseKit b c -
Java UseKit a b c -
java -classpath com UseKit b c -
Java -classpath com:. UseKit b c -
java -classpath com/KitJar.jar UseKit b c -
java -classpath com/Kit Jar. jar UseKit a b c -
java -classpath com/KitJar.jar:. UseKit b c -
java -classpath com/KitJar.jar:. UseKit a b c | |
1. | þ | D is correct. The increment() method must be marked public to be accessed outside of the package. If increment() was public, C, E, and F would be correct. | ý | A and B are incorrect output, even if increment () is public. (Objective 7.1) | |
2. | þ | B, E, and F are required. The only way to access class Dog is via F, which is a package statement. Since you can have only one package statement in a source file, you have to get access to class Robot and class Pet using import statements. Option B accesses Robot, and option E accesses Pet. | ý | A, C, D, and G are incorrect based on the above, Also, C and G are incorrect syntax. (Objective 7.1) | |
3. | þ | C and E are correct syntax for static imports. Line 4 isn't making use of static imports, so the code will also compile with none of the imports. | ý | A, B, D, and F are incorrect based on the above. (Objective 7.1) | |
4. | þ | B is correct, This question is using valid (but inappropriate and weird) identifiers, static imports, var-args in main(), and pre-incrementing logic. | ý | A, C, D, E, F, and G are incorrect based on the above, (Objective 7.2) | |
5. | þ | C is correct. In order for B.java to compile, the compiler first needs to be able to find B.java. Once it's found B.java it needs to find A.class. Because A.class is in the xcom package the compiler won't find A.class if it's invoked from the xcom directory. Remember that the -classpath isn't looking for B.java, it's looking for whatever classes B.java needs (in this case A.class). | ý | A, B, and D are incorrect based on the above. E is incorrect because the compiler can't find B.java. (Objective 7.2) | |
6. | þ | D is correct. To import static members, an import statement must begin: import static. | ý | A, B, C, and E are incorrect based on the above. (Objective 7.1) | |
7. | þ | A is correct. The -D flag is NOT a compiler flag, and the name=value pair that is associated with the -D must follow the -D with no spaces. | ý | B, C, and D are incorrect based on the above. (Objective 7.2) | |
8. | þ | B is correct. The public access modifier is the only one that allows code from outside a package to access methods in a package—regardless of inheritance. | ý | A, B, D, and E are incorrect based on the above. (Objective 7.1) | |
9. | þ | C is correct. The value of aaa is.set at the command line, If aaa had no value when getProperty was invoked, then, aaa would have been set to bbb. | ý | A, B, D, E, and F are incorrect based on the above. (Objective 7.2) | |
10. | þ | 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. (Objective 7,5) | |
11. | ý | 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. (Objective 7.5) | |
12. | ý | 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 classpath must include the name of the JAR file, not just its directory. (Objective 7.5) | |