An interview question
publicclass Test{
staticboolean foo(char c){
System.out.print(c);
returntrue;
}
publicstaticvoid main( String[] argv ){
int i =0;
for ( foo('A'); foo('B')&&(i<2); foo('C')){
i++ ;
foo('D');
}
}
}
What is the result?
A. ABDCBDCB
B. ABCDABCD
C. Compilation fails.
D. An exception is thrown at runtime.
[1149 byte] By [
ardmorea] at [2007-11-26 21:49:10]

I want the detailed explantation.
Wow...
Why does
for ( foo('A'); foo('B')&&(i<2); foo('C')) {
work?
if i replace it with
for ( true; true&&(i<2); true) {
it doesnt work
> I want the detailed explantation.Yeah, well, I want you to think for yourself a little bit and be an active participant in solving your problem. Life's full of these little disappointments.
> Wow...
> Why does
> >for ( foo('A'); foo('B')&&(i<2); foo('C')) {
>
> work?
> if i replace it with
> > for ( true; true&&(i<2); true) {
>
> it doesnt work
Define "work." It works exactly as it's supposed to.
how? the second statement foo("B") which is true but i<2 fails so the statement is not true at all.
oh lord i am stumped the answer is A. grrr
fastmike, are you just talking to yourself or do you have a question?
> fastmike, are you just talking to yourself or do you
> have a question?
mr jverd i agree with turingpest before that it will not work but later on i ran the program to see if it works or not and i saw A was the correct answer. can't believe looks like need to improve more of my java skills still figuring it out that how the answer is A
Um... okay. I'll take that as, "No, I don't have a question at the moment."
well ok here is my question how the answer is A?
> well ok here is my question how the answer is A?
Come on bobo...
for ( foo('A'); foo('B')&&(i<2); foo('C')) {
i++ ;
foo('D');
A. ABDCBDCB
fooA() // for part 1
fooB() // for part 2 - check condition
fooD() // do whats in the for loop
fooC() // for part 3 - increment counter
fooB() // for part 2 - check condition
fooD() // do whats in the for loop
fooC() // for part 3 - increment counter
fooB() // for part 2 - check condition
break out of for loop
just think about how a for loop works.
first part 1 is run to initialize the counter.
then part 2 - the run condition - is tested
if true then the code inside the for loop runs
then part 3 - the increment code runs...
the run condition is retested... etc.
That is how For loop work (I believe).for (intialise ; Check ; Statement) {Content}Initalise -> Check -> Content -> Statement -> Check -> Content -> Statement -> Check -> End
> > well ok here is my question how the answer is A?
> Come on bobo...
> fooA() // for part 1
> fooB() // for part 2 - check condition
> fooD() // do whats in the for loop
> fooC() // for part 3 - increment counter
> fooB() // for part 2 - check condition
> fooD() // do whats in the for loop
> fooC() // for part 3 - increment counter
> fooB() // for part 2 - check condition
> break out of for loop
>
> just think about how a for loop works.
> first part 1 is run to initialize the counter.
> then part 2 - the run condition - is tested
> if true then the code inside the for loop runs
> then part 3 - the increment code runs...
> the run condition is retested... etc.
Message was edited by:
lrngjava
Message was edited by:
lrngjava
Love it, love it, love it.What a fantastic question. ;-)
> Love it, love it, love it.> > What a fantastic question. ;-)Sorry, but it's pathetic!