Term
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.)
A. import static java.lang;
B. import static java.lang.Integer;
C. import static java.lang.Integer.*;
D. import static java.lang.Integer.*_VALUE;
E. import static java.lang.Integer.MAX_VALUE;
F. None of the above statements are valid import syntax |
|
Definition
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.)
A. import static java.lang;
B. import static java.lang.Integer;
C. import static java.lang.Integer.*;
D. import static java.lang.Integer.*_VALUE;
E. import static java.lang.Integer.MAX_VALUE;
F. None of the above statements are valid import syntax
Answer:
- 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. |
|
|
Term
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
B. A.
C. -A.
D. _A.
E. _-A.
F. Compilation fails
G. An exception is thrown at runtime
Answer:
- 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. |
|
Definition
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
B. A.
C. -A.
D. _A.
E. _-A.
F. Compilation fails
G. An exception is thrown at runtime
Answer:
- 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. |
|
|
Term
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.)
A. Set the current directory to xcom then invoke javac B.java
B. Set the current directory to xcom then invoke javac -classpath . B.java
C. Set the current directory to test then invoke javac -classpath . xcom/B.java
D. Set the current directory to test then invoke javac -classpath xcom B.java
E. Set the current directory to test then invoke javac -classpath xcom:. B.java |
|
Definition
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.)
A. Set the current directory to xcom then invoke javac B.java
B. Set the current directory to xcom then invoke javac -classpath . B.java
C. Set the current directory to test then invoke javac -classpath . xcom/B.java
D. Set the current directory to test then invoke javac -classpath xcom B.java
E. Set the current directory to test then invoke javac -classpath xcom:. B.java
Answer:
- 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.
|
|
|
Term
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.)
A. java -Da=b c_d
B. java -D a=b c_d
C. javac -Da=b c_d
D. javac -D a=b c_d |
|
Definition
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.)
A. java -Da=b c_d
B. java -D a=b c_d
C. javac -Da=b c_d
D. javac -D a=b c_d
Answer:
- 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. |
|
|
Term
If three versions of MyClass.class 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
java -classpath /foo/bar/baz/bing:/foo/bar MyClass
Which version will be used by java?
A. /foo/MyClass.class
B. /foo/bar/MyClass.class
C. /foo/bar/baz/MyClass.class
D. /foo/bar/baz/bing/MyClass.class
E. The result is not predictable. |
|
Definition
If three versions of MyClass.class 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
java -classpath /foo/bar/baz/bing:/foo/bar MyClass
Which version will be used by java?
A. /foo/MyClass.class
B. /foo/bar/MyClass.class
C. /foo/bar/baz/MyClass.class
D. /foo/bar/baz/bing/MyClass.class
E. The result is not predictable.
Answer:
- D is correct. A -classpath included with a java invocation overrides a system classpath.
When java 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. |
|
|
Term
Given two files:
1. package pkgA;
2. public class Foo {
3. int a = 5;
4. protected int b = 6;
5. }
1. package pkgB;
2. import pkgA.*;
3. public class Fiz extends Foo {
4.public static void main(String[] args) {
5. Foo f = new Foo();
6. System.out.print(" " + f.a);
7. System.out.print(" " + f.b);
8. System.out.print(" " + new Fiz().a);
9. System.out.println(" " + new Fiz().b);
10.}
11. }
What is the result? (Choose all that apply.)
A. 5 6 5 6
B. 5 6 followed by an exception
C. Compilation fails with an error on line 6
D. Compilation fails with an error on line 7
E. Compilation fails with an error on line 8
F. Compilation fails with an error on line 9 |
|
Definition
Given two files:
1. package pkgA;
2. public class Foo {
3. int a = 5;
4. protected int b = 6;
5. }
1. package pkgB;
2. import pkgA.*;
3. public class Fiz extends Foo {
4.public static void main(String[] args) {
5. Foo f = new Foo();
6. System.out.print(" " + f.a);
7. System.out.print(" " + f.b);
8. System.out.print(" " + new Fiz().a);
9. System.out.println(" " + new Fiz().b);
10.}
11. }
What is the result? (Choose all that apply.)
A. 5 6 5 6
B. 5 6 followed by an exception
C. Compilation fails with an error on line 6
D. Compilation fails with an error on line 7
E. Compilation fails with an error on line 8
F. Compilation fails with an error on line 9
Answer:
- C, D, and E are correct. Variable a (default access) cannot be accessed from outside the
package. Since variable b is protected, it can be accessed only through inheritance.
- A, B, and F are incorrect based on the above. (Objectives 1.1, 7.1) |
|
|