File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Jxam Question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Jxam Question?" Watch "Jxam Question?" New topic
Author

Jxam Question?

JM Beauvais
Greenhorn

Joined: Dec 02, 2003
Posts: 1
The Jxam asks if the following is valid:
int q;
for (int p = 0, q = 0; p < 5; p++){
System.out.println("Val = " + p + q);
}
The provided answer is: "Answer 4 is incorrect, because you can't mix the initialisation of a local loop variable declaration in the 'for' loop, with the initialisation of another loop variable."
I know that it is illegal to mix declarations/initializations in the first part of the for loop, BUT-- it seems to me that int p=0, q=0 is a valid declaration/initialization, therefore the q in the for statement is a seperate/different more local q? is that true?
jb.
nivas rao
Greenhorn

Joined: Dec 01, 2003
Posts: 9
No it's not valid. gives compile error q already declared.
int p=0,q=0; implies declaration and intialisation of two variables p,q.
mrudul joshi
Ranch Hand

Joined: Nov 12, 2003
Posts: 54
What I think is, q is already defined and so it cannot act as local variable for the 'for'loop at the same time when it is member variable for the class.
If we try to run the above program, we get the same compile time error that variable g is already defined.
About second part of your question, I think we cannot declare the same variable as member variable and local variable at the same time.
But we can have n number of declarations in teh first part of the 'for' loop separating them from the conditional expression using ';'.
I hope this helps!
Bye.
mrudul joshi
Ranch Hand

Joined: Nov 12, 2003
Posts: 54
What I think is, q is already defined and so it cannot act as local variable for the 'for'loop at the same time when it is member variable for the class.
If we try to run the above program, we get the same compile time error that variable q is already defined.
About second part of your question, I think we cannot declare the same variable as member variable and local variable at the same time.
But we can have n number of declarations in teh first part of the 'for' loop separating them from the conditional expression using ';'.
I hope this helps!
Bye.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Jxam Question?
 
Similar Threads
Jxam
Jxam
Jxam
Jxam
jxam