File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about properties and command line arguements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about properties and command line arguements" Watch "question about properties and command line arguements" New topic
Author

question about properties and command line arguements

Brent W Farrell
Greenhorn

Joined: Apr 10, 2008
Posts: 17
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

Joined: Oct 31, 2007
Posts: 100
in answer A, y is args[0] and z is args[1]
args is array, thus zero indexed...


<a href="http://faq.javaranch.com/java/UseRealWords" target="_blank" rel="nofollow">Use Real Words</a> <a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">!!!Use Code Tags!!!</a> <a href="http://faq.javaranch.com/java/SayThanks" target="_blank" rel="nofollow">Say Thanks</a><br />scjp6
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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


[My Blog]
All roads lead to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about properties and command line arguements
 
Similar Threads
System.getProperty question from SunExam
Doubt on KB Question #12 Chapter 5
Command line argument question
Runtime behavior after command-line invocation
Chapter 5 K&B -Doubt