What will be the result of the following program execution?

public class Test {

    static boolean foo(char c) {
        System.out.print(c);
        return true;
    }
    public static void main(String[] args) {
        int i = 0;
        for ( foo('A'); foo('B') && (i < 2); foo('C')) {
            i++;
            foo('D');
        }
    }
}
Explanation

Loop initialization block is executed first and only once. Therefore, an 'A' symbol will be displayed first, and it will not be displayed any more.

At each loop iteration a condition checks ('B') is executed first, a body of loop ('D') is executed after that, and variables modification block ('C') is executed then.

Since two loop iterations are performed, then symbols 'BDC' will be displayed twice.

Loop terminates when the cycle continuation condition becomes false. Thus symbol 'B' will be displayed last


Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Продолжайте изучать
тесты по Java
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты