Multiple choice technology programming languages

Given: 1. public class Delta { 2. static boolean foo(char c) { 3. System.out.print(c); 4. return true; 5. } 6. public static void main( String[] argv ) { 7. int i =0; 8. for ( foo(‘A’); foo(‘B’)&&(i<2); foo(‘C’)){ 9. i++ ; 10. foo(‘D’); 12. } 13. } 14. } What is the result?

  1. ABDCBDCB

  2. ABCDABCD

  3. Compilation fails.

  4. An exception is thrown at runtime.

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

First, the loop initialization runs foo('A') (prints A). In the first check, foo('B') (prints B) and i<2 are true; loop body increments i and runs foo('D') (prints D); then update runs foo('C') (prints C). This repeats until the third check where i<2 is false, terminating after printing B.