I am studying for my first Java Cert! Hurah! Sorry, don't mean to sound so cheezy, but I can't help it :) Anywho, trying to understand the below question and answer. I don't get it. . . I don't really use the command line that much (mainly NetBeans and I am getting acquainted with Eclipse). Any pointers are greatly appreciated.
OBJECTIVE: 7.2: Given an example of a class and a command-line, determine the expected runtime behavior. 10)
Given:
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
//So the answer is C
REFERENCE:
API for java command
Option C is correct. -D sets a property and args[1] is the second argument (whose value is y)
Uhm. . . so how come args[1] is the second argument? i thought "p" would be equal to the String {y x x y z}?
Uhm. . . so how come args[1] is the second argument? i thought "p" would be equal to the String {y x x y z}?
The question is tricky. In c), we are starting the class x with arguments x y z. And the -D option will cause the x property to be set to y.
So when the class x starts:
args will be `String[]{"x", "y", "z"}, andp will be "y".We then compare p with args[1] ... and the two strings are equal.
(Of course, in the real world, no person who named their class x would survive long enough to receive their first pay check :-) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With