📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    Trace the execution of the following Java code:

        public class Flow {         public static void main(String[] args) {             int count = 0;             for (int i = 0; i < 2; i++) {                 for (int j = 0; j < 3; j++) {                     count++;                 }             }             System.out.println(count);         }     }     What is the output?
    B 2 Correct Answer Incorrect Answer
    C 3 Correct Answer Incorrect Answer
    D 5 Correct Answer Incorrect Answer
    E 6 Correct Answer Incorrect Answer

    Solution

    The outer loop runs for i = 0 and i = 1 (2 iterations).     The inner loop runs for j = 0, 1, 2 (3 iterations) for *each* iteration of the outer loop.     So, count++ executes 2 * 3 = 6 times.     Initial count = 0. After 6 increments, count = 6.

    Practice Next
    ask-question