• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

question about properties and command line arguements

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in answer A, y is args[0] and z is args[1]
args is array, thus zero indexed...
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic