jobs4timesLogo jobs4timesLogo

Development

Q: 01 Click the Exhibit button.

Given the fully-qualified class names:
com.foo.bar.Dog
com.foo.bar.blatz.Book
com.bar.Car
com.bar.blatz.Sun

Which graph represents the correct directory structure for a JAR file from which those classes can be used by the compiler and JVM ?


development
  1. Jar A
  2. Jar B
  3. Jar C
  4. Jar D
  5. Jar E
Answer: A

Q: 02

A class games.cards.Poker is correctly defined in the jar file Poker.jar.
A user wants to execute the main method of Poker on a UNIX system using the command: java games.cards.Poker What allows the user to do this ?
  1. put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java
  2. put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java/*.jar
  3. Put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java/Poker.jar
  4. put Poker.jar in directory /stuff/java/games/cards, and set the CLASSPATH to include /stuff/java
  5. put Poker.jar in directory /stuff/java/games/cards, and set the CLASSPATH to include /stuff/java/*.jar
  6. put Poker.jar in directory /stuff/java/games/cards, and set the CLASSPATH to include /stuff/java/Poker.jar
Answer: C

Q: 03 Given:

11. public class Commander { 
12. public static void main(String[] args) { 
13. String myProp = /* insert code here */ 
14. System.out.println(myProp); 
15. } 
16. }
and the command line:
java -Dprop.custom=gobstopper Commander
Which two, placed on line 13, will produce the output gobstopper? (Choose two.)
  1. System.load("prop.custom");
  2. System.getenv("prop.custom");
  3. System.property("prop.custom");
  4. System.getProperty("prop.custom");
  5. System.getProperties().getProperty("prop.custom");
Answer: D, E

Q: 04

A developer is creating a class Book, that needs to access class Paper.
The Paper class is deployed in a JAR named myLib.jar. Which three, taken independently,
will allow the developer to use the Paper class while compiling the Book class? (Choose three.)

  1. The JAR file is located at $JAVA_HOME/jre/classes/myLib.jar.
  2. The JAR file is located at $JAVA_HOME/jre/lib/ext/myLib.jar..
  3. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that includes /foo/myLib.jar/Paper.class.
  4. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that includes /foo/myLib.jar.
  5. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -cp /foo/myLib.jar/Paper Book.java.
  6. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -d /foo/myLib.jar Book.java
  7. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -classpath /foo/myLib.jar Book.java
Answer: B, D, G

Q: 05 Click the Task button.

Solution:
1. package com.bar;
2. import com.foo.bar.*;
3. import com.foo.bar.blatz.*;

Q: 06 Given:

1. package com.company.application;
2.
3. public class MainClass {
4. public static void main(String[] args) {}
5. }

And MainClass exists in the /apps/com/company/application directory.
Assume the CLASSPATH environment variable is set to "." (current directory).
Which two java commands entered at the command line will run MainClass ? (Choose two.)

  1. java MainClass if run from the /apps directory
  2. java com.company.application.MainClass if run from the /apps directory
  3. java -classpath /apps com.company.application.MainClass if run from any directory
  4. java -classpath . MainClass if run from the /apps/com/company/application directory
  5. java -classpath /apps/com/company/application:. MainClass if run from the /apps directory
  6. java com.company.application.MainClass if run from the /apps/com/company/application directory
Answer: B, C

Q: 07

Given a correctly compiled class whose source code is:
1. package com.sun.sjcp; 
2. public class Commander { 
3. public static void main(String[] args) { 
4. // more code here 
5. } 
6. } 

Assume that the class file is located in /foo/com/sun/sjcp/, the current directory is /foo/, and that the classpath contains "." (current directory).
Which command line correctly runs Commander ?

  1. java Commander
  2. java com.sun.sjcp.Commander
  3. java com/sun/sjcp/Commander
  4. java -cp com.sun.sjcp Commander
  5. java -cp com/sun/sjcp Commander
Answer: B

Q: 08

A UNIX user named Bob wants to replace his chess program with a new one, but he is not sure where the old one is installed.
Bob is currently able to run a Java chess program starting from his home directory /home/bob using the command:
java -classpath /test:/home/bob/downloads/*.jar games.
Chess Bob's CLASSPATH is set (at login time) to: /usr/lib:/home/bob/classes:/opt/java/lib:/opt/java/lib/*.jar
What is a possible location for the Chess.class file ?

  1. /test/Chess.class
  2. /home/bob/Chess.class
  3. /test/games/Chess.class
  4. /usr/lib/games/Chess.class
  5. /home/bob/games/Chess.class
  6. inside jarfile /opt/java/lib/Games.jar (with a correct manifest)
  7. inside jarfile /home/bob/downloads/Games.jar (with a correct manifest)
Answer: C

Q: 09

Given the following directory structure:

And the following command line invocation:
javac -d classes source/Utils.java
Assume the current directory is bigProject, what is the result ?

  1. If the compile is successful, Utils.class is added to the source directory.
  2. The compiler returns an invalid flag error.
  3. If the compile is successful, Utils.class is added to the classes directory.
  4. If the compile is successful, Utils.class is added to the bigProject directory.
Answer: C

10. 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();
Answer:
  • 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.

11. Given the following directory structure:

dev
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;
Answer:
  • 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.

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

13. Given the default classpath:

/foo And this directory structure:
img
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
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.

14. Given two files:

package xcom; 
public class Stuff { 
public static final int MY_CONSTANT = 5; 
public static int doStuff(int x) { 
 return (x++)*x; 
} 
} 

import 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.
Answer:
  • 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.

15. 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.
Answer:
  • 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.

16. 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.
Answer:
  • 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.

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

18. 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 subdirectory 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.
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.

19. 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: dev

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/KitJar.jar UseKit a b c
  7. java -classpath com/KitJar.jar:. UseKit b c
  8. 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 classpath must include the name of the JAR file, not just its directory.
BACK