This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Modern JavaScript for the Impatient and have Cay Horstmann on-line!
See this thread for details.
Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Dan's exam

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends!
I have a doubt about an answer explanation in Dan's site.For the question number 12.
1)A single local variable declaration statement may contain a comma separated list of variable declarators: "int i=0, j=0;".
2)Alternatively, the same two variables could be declared using two local variable declaration statements: "int i=0; int j=0;". Please note that the two statements are separated by a semicolon, and each declares the type of the variable
DanChisholm
The point no.1 is fine but the point no 2 stating that int i=0;int j=0; is what I am not clear about coz when I tried it, it gives a compiler error but Dan states that it is an alternate way to declare in a for loop.
for(int i=0;int j=0;i<2;i++,j++)
According to me the above for loop stmt gives an error.
The only correct way should be
for(int i=0,j=0;i<2;i++,j++)
Am I right?
Please help.
Thanks.
Sagarika
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Yes, I think you are right. The structure of the loop is,
for(initialization; boolean expression;step statement)and we can have as many initialization and step portions as we want to have provoded that they are separated by a comma.
For example,
for(int i =0, int j=12;j<12;i++)
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is as follows.

The Answers are d and e.
The explanation is as follows.


A compile-time error occurs at line 2 as a result of the attempt to shadow the method local variable, k, within the for-loop. A variable declared within the for-loop can not shadow a local variable of the enclosing method. At line 3, the declaration of variable j causes a compile-time error. A single local variable declaration statement may contain a comma separated list of variable declarators: "int i=0, j=0;". Alternatively, the same two variables could be declared using two local variable declaration statements: "int i=0; int j=0;". Please note that the two statements are separated by a semicolon, and each declares the type of the variable. The initialization part of a for statement may use a single local variable declaration statement to declare more than one local variable. Each of the new variables is declared in a comma separated list of variable declarators. In this program, the local variables should have been declared as follows: "int i=0, j=0;". Instead, the type of variable j has been incorrectly added to the declarator: "int i=0, int j=0;". The result is a compile-time error.


The key sentence in this explanation is the following.

The initialization part of a for statement may use a single local variable declaration statement to declare more than one local variable. Each of the new variables is declared in a comma separated list of variable declarators.


The sentences that you have quoted are as follows.


A single local variable declaration statement may contain a comma separated list of variable declarators: "int i=0, j=0;". Alternatively, the same two variables could be declared using two local variable declaration statements: "int i=0; int j=0;".


Those statements were not intended to refer to the initialization part of a for statement. Instead, they were intended to describe alternative forms of declarations of local variables. After reading your comments, I can see that the intent of the above quote is not clear. I now see that the entire explanation would be more clear if I just delete the confusing sentences. The next version of the exam will not contain those sentences.
Thank you for using my exam, and thank you for the feedback.
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic