Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about properties and command line arguements

 
Brent W Farrell
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I am taking a sun practice exam for the scjp 5. Please help me with the following question:


1. class x {
2. public static void main(String [] args) {
3. String p = System.getProperty("x");
4. if(p.equals(args[1]))
5. System.out.println("found");
6. }
7. }


Which command-line invocation will produce the output found?


A
java -Dx=y x y z
B
java -Px=y x y z
C
java -Dx=y x x y z
D
java -Px=y x x y z
E
java x x y z -Dx=y
F
java x x y z -Px=y

I thought since we are comparing the property "x" to args[1] the correct answer is A, both would be y, however the exam says the answer is c. Please provide guidance.
 
Ivan Ivanic
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in answer A, y is args[0] and z is args[1]
args is array, thus zero indexed...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Command line flags are not part of the main method arguments. You could see that by looping through the args parameter.


If you type "java" at the prompt, you'll some help, looking like that :
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)

This [-options] like -D are not part of the command line arguments. The [args...] are.

A
java [-Dx=y] x [y z] : property x = y. args[1] = z
B
java [-Px=y] x [y z] : -P ??
C
java [-Dx=y] x [x y z] : property x = y. args[1] = y
D
java [-Px=y] x x y z : -P ??
E
java x [x y z -Dx=y] : property x does not exist. args[1] = y
F
java x [x y z -Px=y] : property x does not exist. args[1] = y
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic