• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about OCA Self Test Chapter 1, Question 3 (K&B7)

 
Jose Robles
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please confirm if I am doing something wrong here, or if the answer for Self-Test question Chapter1, question 3 is wrong.
I tested the code for the class and got a different result:



My result was C (-A.)
The book's answer is B ( A. )
 
T Vergilio
Ranch Hand
Posts: 230
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the same result as the book. The '-' never gets added to the String because of the post increment operator in the for loop.

Try running this:



Output:
 
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 Jose Robles,

First of all, a warm welcome to CodeRanch!

Jose Robles wrote:Can you please confirm if I am doing something wrong here, or if the answer for Self-Test question Chapter1, question 3 is wrong.

I agree with T Vergilio: the study guide is spot-on! The code snippet you have posted looks exactly the same as in the study guide. You didn't have posted the complete java command which resulted in a different result, so it's very hard to tell what you did wrong. In this topic you'll find an excellent explanation about the exact same question (code snippet). I'm pretty sure after carefully reading this topic all your doubts (concerning this topic) will be cleared. If you still have doubts/questions after reading this topic, simply click on the "Post reply" button and let us know

Hope it helps!
Kind regards,
Roel
 
Jose Robles
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all. I saw what was wrong now.
The first string after the "java" runtime program name is the class name, which happens to be "_", and is not passed as a command line argument into the __A_V_ array. The actual contents of the __A_V_[] array is:
__A_V_[0] = "-"
__A_V_[1] = "A"
__A_V_[2] = "."


Thank you!

 
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
Jose Robles wrote:The first string after the "java" runtime program name is the class name, which happens to be "_", and is not passed as a command line argument into the __A_V_ array. The actual contents of the __A_V_[] array is:
__A_V_[0] = "-"
__A_V_[1] = "A"
__A_V_[2] = "."

You are spot-on!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic