• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

OCA Study Guide Jeanne Boyarsky and Scott Selikoff Chapter 2 Review Question 16

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

Right now I am studying for the OCA Java 8 exam and yesterday I did the review questions of chapter 2 again (book OCA Oracle Certified Associate Java SE 8 Programmer I by Jeanne Boyarsky and Scott Selikoff).
While I was making these review questions I noticed something at question number 16.
I don't know if anyone else noticed this:

the question says:

what is the output of the following code snippet?


so after making the questions, I checked at the answers of chapter 2 review questions < 15 questions answered good of 20 and 5 questions wrong answered of 20.
But when I looked at the answer for question 16 I saw this:

After the first execution of the loop, i is decrememted to 9 and result to 13.
But according to the book, a value is decremented by 1.
How is it then in this case that int result is decremented with 2 instead of 1?
Can somebody please help me with this?

 
Saloon Keeper
Posts: 7601
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case 'variable' is decremented by whatever N is, not necessarily 1. Also, if N is negative it will actually increment 'variable'.
 
Ashana Hurkens
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Carey,

Thank you very much for the explanation
I see now that I made a mistake in my topic.
It was number 17 of the review questions of chapter 2 and not number 16 (Sorry guys).

But coming back @ this topic.
I was wondering why the value of int Result was after decrementing changed from 15 to 13?
Is it because there are two variable at the same line (int result, i)?
Or is their another reason why the value of int result changed from 15 to 13 after decremented?




 
Carey Brown
Saloon Keeper
Posts: 7601
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Ashana Hurkens
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The decrement operator on line 6. According to the answer review questions chapter 2 the value of int result would change after this line
From 15 to 13.
 
Carey Brown
Saloon Keeper
Posts: 7601
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ashana Hurkens wrote:I was wondering why the value of int Result was after decrementing changed from 15 to 13?
Is it because there are two variable at the same line (int result, i)?

No.

Or is their another reason why the value of int result changed from 15 to 13 after decremented?

Yes
 
Carey Brown
Saloon Keeper
Posts: 7601
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ashana Hurkens wrote:The decrement operator on line 6. According to the answer review questions chapter 2 the value of int result would change after this line
From 15 to 13.

After the first execution of the loop

The loop doesn't end at line 6.
 
reply
    Bookmark Topic Watch Topic
  • New Topic