Trace the Code
Loop Logic
If / Else Reasoning
100

int x = 5;

x = x + 3;

System.out.println(x);


What will print in this code?

It's eight.

The variable x is 5. The statement x = x + 3 adds 3 to the value of x, making it 8. The program then prints the updated value.

100

for (int i = 0; i < 3; i++) {

    System.out.println("Hello");

}

How many times will “Hello” be printed?

It’s three times.

The loop starts at i = 0 and runs while i < 3. Each loop prints once so the loop runs 3 times.

100

int score = 85;


if (score >= 60) {

    System.out.println("Pass");

} else {

    System.out.println("Fail");

}


What will it print?

It’s Pass. 

The condition checks if score is at least 60. Since 85 meets the condition, the if block runs instead of the else.

200

int a = 2;

int b = a * 4;

b = b - 3;

System.out.println(b);

What will it be printed?

It’s five

The value of a is 2 so b becomes 2 * 4 = 8. So 3 is subtracted from b, and it becomes 5.

200

int sum = 0;


for (int i = 1; i <= 4; i++) {

    sum = sum + i;

}


System.out.println(sum);


What will it print? 

It’s ten.

The loop adds numbers 1 through 4 to sum. This use accumulation logic:
 1 + 2 + 3 + 4 = 10.

200

int num = 7;


if (num % 2 == 0) {

    System.out.println("Even");

} else {

    System.out.println("Odd");

}


What will it print?

It’s Odd.

The modulus operator checks if there is a remainder when dividing by 2. Since 7 has a remainder the condition is false and the else block is removed.

300

int num = 10;


if (num > 5) {

    num = num - 2;

}


System.out.println(num);


What will it print?

It’s eight. 

The condition num > 5 is true so the code inside the if statement runs. The value of num is less by 2.

300

int count = 5;


while (count > 0) {

    System.out.print(count + " ");

    count--;

}


What will it print?

5 4 3 2 1

The loop continues while the condition count > 0 is true. Each prints the current value and then lowers it. This shows how loop conditions control repetition.

300

int x = 10;


if (x < 5) {

    System.out.println("A");

} else if (x < 15) {

    System.out.println("B");

} else {

    System.out.println("C");

}


What will it print?

It’s B

The first condition is false, but the second condition is true. The program stops checking once a true condition is found. Showing conditional branching logic.

M
e
n
u