| Author |
Dan's exam
|
Sagarika nair
Ranch Hand
Joined: Aug 13, 2003
Posts: 39
|
|
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
|
 |
mrudul joshi
Ranch Hand
Joined: Nov 12, 2003
Posts: 54
|
|
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++)
|
 |
Dan Chisholm
Ranch Hand
Joined: Jul 02, 2002
Posts: 1865
|
|
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.
|
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
|
 |
 |
|
|
subject: Dan's exam
|
|
|