Hi,
I've been going through the OCA/OCP Java SE 7 Study Guide, and have arrived at the first self test section. I'm now struggling to understand the answer to question 3:
Given that the for loop's syntax is correct, and given:
And the command line:
java _ - A .
What is the result?
A. -A
B. A.
C. -A.
D. _A.
E. _-A.
F. Compilation fails
G. An exception is thrown at runtime
My answer: C
Correct answer: B, with explanation
The question is using valid (but inappropriate and weird) identifiers, static imports, main(), and pre-incrementing logic.
Unless I misunderstand something, this does not seem to cover why C is
not correct. I assumed that the hyphen (-) is not seen as a
java parameter because it gets interpreted by the java executable as a
command line parameter.
However, putting this theory to the test disappointed me: I just added a line
to my Coffee class (pasted below for reference) and recompiled. Here's my command line output:
This seems to dispel my theory: in each case, the hyphen is clearly seen as a "Java argument". Why, then, is the answer B rather than C (the hyphen does not get printed)? Thanks!
FOR REFERENCE: Coffee class