This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OCAJP Level 1 SE8 - Operators and Statements - page 86 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Byron Fielies
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey All,

I am in the process of preparing for the OCAJP Se8 Level 1... I absolutely love the study guide, it's fully loaded with comprehensive information.

Perhaps it's all the late nights and early mornings but can anyone explain why the following code outputs -5, 10, ?



 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Byron Fielies wrote:




Welcome to the Ranch.

Please use code tags for displaying code, which makes it more legible.

First does it compile? Looks like missing an "i" in the for loop declaration

Ok let dissect the code a bit.
values.length is the size of the array values = 3
the for loop starts at 1 and ends at 3, incrementing 1 each time
so what's i? 1 in the first iteration => values[1] is 5 and values[1-1] is 10 => 5-10=-5 (i now becomes 2)
2nd iteration, i now is 2 => values[2] is 15 and values[2-1] is 5 => 15-5=10 (i now becomes 3)
3rd iteration i now is 3, 3<3 exit loop

Voila your output -5, 10
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Byron Fielies,

First of all, a warm welcome to CodeRanch!

Byron Fielies wrote:Perhaps it's all the late nights and early mornings but can anyone explain why the following code outputs -5, 10, ?

The code snippet you have provided does not compile! There are two compiler errors:
  • on line4 the (non-existing) array variable Values is used; as you know Java is case-sensitive and it should have been values with lower case v
  • as already mentioned by K. Tsang, in the initialization section of the for statement int=1 should be replaced with int i=1

  • I guess both are nothing more than typos. But you should always be very meticulous when posting code snippets and make sure your code snippets compile successfully (unless it's intended to have a compiler error). Not only because otherwise ranchers who want to answer your question (or clear your doubts) might be confused, but certainly because on the actual exam having an eye for detail is one of the very important aspects to pass the exam. You should definitely put some effort and have great care when posting code snippets, so you learn to pay attention to little details.

    Although K. Tsang has already provided an excellent explanation about why the code snippet results in the given output, you can also add a few additional println statements to provide information about how the code will be executed. Let's have a look at a code snippet with a few additional println statementsAnd the output of this code snippet is very similar to K. Tsang's explanation:
    i=1 values[i]=5 values[i-1]=10
    -5
    i=2 values[i]=15 values[i-1]=5
    10


    And now it's time for a little nitpick. The code snippet as-is in the study guide will never print "-5, 10, ", it will print "-510". In order to get the output mentioned in the study guide, the code snippet should be updated to

    Hope it helps!
    Kind regards,
    Roel

    PS. Always use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers. Jeanne already added the code tags for you. See how much easier the code is to read?
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic