• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

struggling to understand the correct answer for Chapter 1 - Question 3 self test

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Pieter-Jan Remaut
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

After thinking about this some more (which is hard to avoid once you've invested the effort of making a descriptive post about your problem), I'm guessing the answer is going to lie in the syntax of - I imagine that's what the answer calls pre-incrementing logic - I see that the third 'part' I'd expect to be present in a for statement is missing.

This raises another, more general and hence more important question about the exam - was I supposed to know about this syntax? To clarify what I mean:
* I'm pretty sure this was not explicitly explained in the scope of Chapter 1 and
* Gazing over the topics of future chapters, it seems this might be covered in Chapter 6 - not something I was assumed to have read by now.

Is this question an example of an "accidental" reliance on knowledge that was not (yet) treated in the book? My real question here is: will the exam assume technical knowledge not explicitly covered in this book or present in the Objectives, and if so, how do I best prepare for this?

To further illustrate what I mean (just so you don't think I'm being pedantic about the order of topics in the book): in the same self test series I also answered questions 2 and 5 incorrectly, simply because I'd never seen or used System.out.print(String) before (as opposed to println), including in the first chapter of the study guide. In hindsight, it seems pretty obvious that the writers just assumed everyone would have at least seen print(String) before.
However, when answering the question I simply assumed it was a sneaky way of introducing a compilation error. Is there a list out there of "Java common assumed knowledge" - or should I just have gone through Java's documentation for System.out beforehand?


Apologies for the mini-rant! If the correct answer does not depend on the used for syntax, it would be nice if we could pretend this reply doesn't exist - thanks!
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pieter-Jan Remaut wrote:
After thinking about this some more (which is hard to avoid once you've invested the effort of making a descriptive post about your problem), I'm guessing the answer is going to lie in the syntax of - I imagine that's what the answer calls pre-incrementing logic - I see that the third 'part' I'd expect to be present in a for statement is missing.



It is not hard to guess here. The "pre-incrementing logic" is referring to what is happening with the pre-increment operator. Do you see the operator in that statement? And do you see what kinda of side effect that it has?

Henry
 
Pieter-Jan Remaut
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

Thanks for the reply. I assume you're talking about the ++x occuring in the second 'part' (correct terminology?) of the for loop, and I'd guess this just means that before executing 'this iteration of the for loop' the x variable is already incremented.

However, that's not my core problem by now (although I thank you for confirming the solution!) - see also my previous reply: the fact that me not knowing precisely this syntactical structure caused me to answer incorrectly makes me worry about how I'll do on the exam.

Cheers,
Pieter-Jan
 
He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic