Java OCA OCP Practice Question 2823

Question

Given:

1. import java.util.*;  
2. public class Main {  
3.   public static void main(String... arrrrgs) {  
4.     Properties p = System.getProperties();  
5.     p.setProperty("pirate", "scurvy");  
6.     String s = p.getProperty("argProp") + " ";  
7.     s += p.getProperty("pirate");  
8.     System.out.println(s);  
9. } } 

And the command-line invocation:

java Main -DargProp="dog," 

What is the result?

  • A. dog, scurvy
  • B. null scurvy
  • C. scurvy dog,
  • D. scurvy null
  • E. Compilation fails.
  • F. An exception is thrown at runtime.
  • G. An "unrecognized option" command-line error is thrown.


B is correct.

Note

While the declaration of main() could be considered a plank-walking offense, it's legal.

In order to send a system property to the program from the command line, the -D option must come before the name of the class file.

As invoked, the JVM will ignore the -D argument, and the call to getProperty() for argProp will return a null.

If the invocation was:.

java -DargProp="dog," Main then the result would be  "dog, scurvy" 



PreviousNext

Related