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]
# 1
A
jiayinhua at 2007-7-10 3:40:28 > top of Java-index,Java Essentials,New To Java...
# 2
I want the detailed explantation.
ardmorea at 2007-7-10 3:40:29 > top of Java-index,Java Essentials,New To Java...
# 3

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

TuringPesta at 2007-7-10 3:40:29 > top of Java-index,Java Essentials,New To Java...
# 4
> 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.
jverda at 2007-7-10 3:40:29 > top of Java-index,Java Essentials,New To Java...
# 5

> 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.

jverda at 2007-7-10 3:40:29 > top of Java-index,Java Essentials,New To Java...
# 6
-- deleted --
rym82a at 2007-7-10 3:40:29 > top of Java-index,Java Essentials,New To Java...
# 7
how? the second statement foo("B") which is true but i<2 fails so the statement is not true at all.
fastmikea at 2007-7-10 3:40:29 > top of Java-index,Java Essentials,New To Java...
# 8
oh lord i am stumped the answer is A. grrr
fastmikea at 2007-7-10 3:40:29 > top of Java-index,Java Essentials,New To Java...
# 9
fastmike, are you just talking to yourself or do you have a question?
jverda at 2007-7-10 3:40:29 > top of Java-index,Java Essentials,New To Java...
# 10

> 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

fastmikea at 2007-7-10 3:40:29 > top of Java-index,Java Essentials,New To Java...
# 11
Um... okay. I'll take that as, "No, I don't have a question at the moment."
jverda at 2007-7-10 3:40:30 > top of Java-index,Java Essentials,New To Java...
# 12
well ok here is my question how the answer is A?
fastmikea at 2007-7-10 3:40:30 > top of Java-index,Java Essentials,New To Java...
# 13

> 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.

TuringPesta at 2007-7-10 3:40:30 > top of Java-index,Java Essentials,New To Java...
# 14
That is how For loop work (I believe).for (intialise ; Check ; Statement) {Content}Initalise -> Check -> Content -> Statement -> Check -> Content -> Statement -> Check -> End
rym82a at 2007-7-10 3:40:30 > top of Java-index,Java Essentials,New To Java...
# 15

> > 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

lrngjavaa at 2007-7-21 18:27:10 > top of Java-index,Java Essentials,New To Java...
# 16
lol ok i agree.
fastmikea at 2007-7-21 18:27:10 > top of Java-index,Java Essentials,New To Java...
# 17
A
foolboya at 2007-7-21 18:27:10 > top of Java-index,Java Essentials,New To Java...
# 18
Love it, love it, love it.What a fantastic question. ;-)
CSAngela at 2007-7-21 18:27:10 > top of Java-index,Java Essentials,New To Java...
# 19
> Love it, love it, love it.> > What a fantastic question. ;-)Sorry, but it's pathetic!
qUesT_foR_knOwLeDgea at 2007-7-21 18:27:10 > top of Java-index,Java Essentials,New To Java...