Self Test


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.)

  1. The output is 0.

  2. The output is 5.

  3. The output is 6.

  4. Compilation fails.

  5. The code compiles if line 1 is removed.

  6. The code compiles if line 2 is changed to read

     Useful u = new Useful( ); 

image from book

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.)

  1. package org;

  2. import org.*;

  3. package org.*;

  4. package org.ex;

  5. import org.ex.*;

  6. package org.ex.why;

  7. package org.ex.why.Dog;

image from book

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.)

  1. import static java.lang;

  2. import static java.lang.Integer;

  3. import static java.lang.Integer.*;

  4. import static java.lang.Integer.* VALUE;

  5. import static java.lang.Integer.MAX_VALUE;

  6. None of the above statements are valid import syntax.

image from book

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?

  1. -A

  2. A.

  3. -A.

  4. -A.

  5. _-A.

  6. Compilation fails.

  7. An exception is thrown at runtime.

image from book

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.)

  1. Set the current directory to xcom then invoke

     javac B.java 

  2. Set the current directory to xcom then invoke

     javac -classpath . B.java 

  3. Set the current directory to test then invoke

     javac -classpath . xcom/B.java 

  4. Set the current directory to test then invoke

     javac -classpath xcom B.java 

  5. Set the current directory to test then invoke

     javac -classpath xcom:. B.java 

image from book

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?

  1. 25

  2. 30

  3. 36

  4. Compilation fails.

  5. An exception is thrown at runtime.

image from book

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.)

  1. java -Da=b c_d

  2. java -D a=b c_d

  3. javac -Da=b c_d

  4. javac -D a=b c_d

image from book

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.)

  1. void go() { System.out.println("a.go"); }

  2. public void go() { System.out.println("a.go"); }

  3. private void go() { System.out.println("a.go"); }

  4. protected void go() { System.out.println("a.go"); }

  5. None of these options will allow the code to compile.

image from book

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.)

  1. The value of property aaa is aaa.

  2. The value of property aaa is bbb.

  3. The value of property aaa is ccc.

  4. The value of property bbb is aaa.

  5. The value of property bbb is ccc.

  6. The invocation will not complete without error.

image from book

10. 

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?

  1. /foo/MyClass.java

  2. /foo/bar/MyClass.java

  3. /foo/bar/baz/MyClass.java

  4. /foo/bar/baz/bing/MyClass.java

  5. The result is not predictable.

image from book

11. 

Which are true? (Choose all that apply.)

  1. The java command can access classes from more than one package, from a single JAR file.

  2. JAR files can be used with the java command but not with the javac command.

  3. 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.

  4. In order to specify the use of a JAR file on the command line, the JAR file's path and filename MUST be included.

  5. 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.

image from book

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.)

  1. java UseKit b c

  2. Java UseKit a b c

  3. java -classpath com UseKit b c

  4. Java -classpath com:. UseKit b c

  5. java -classpath com/KitJar.jar UseKit b c

  6. java -classpath com/Kit Jar. jar UseKit a b c

  7. java -classpath com/KitJar.jar:. UseKit b c

  8. java -classpath com/KitJar.jar:. UseKit a b c

image from book

Answers

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. 

þ  

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. (Objective 7.5)

12. 

þ  

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 classpath must include the name of the JAR file, not just its directory. (Objective 7.5)




SCJP Sun Certified Programmer for Java 5 Study Guide Exam 310-055
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) (Certification Press)
ISBN: 0072253606
EAN: 2147483647
Year: 2006
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net