← all problems

Expression and Assignment Tracing 🌶️🌶️

Practice for evaluating expressions and tracing what each assignment statement actually does to a variable's value — the same skill a multiple-choice "what does this code print?" question tests, just slower and step by step first.

1. What does 17 % 5 evaluate to?

2. What does 7 / 2 evaluate to? (Both are int.)

3. What does 7.0 / 2 evaluate to?

4. What does 7 / 2.0 evaluate to?

5. What does 3 + 4 * 2 evaluate to?

6. What does (3 + 4) * 2 evaluate to?

7. Trace total and extra after each line executes.

int total = 17;int extra = total % 5;total = total / 5;total = total + extra;

If a variable hasn't been declared, write DNE for Does Not Exist. If a variable has been declared but not initialized, write ?. Otherwise, fill in the value of each variable immediately after the labeled line finishes executing.

8. Trace x and y after each line executes.

int x = 2;int y = x + 3 * 4;x = y - 10;y = x * 2;

If a variable hasn't been declared, write DNE for Does Not Exist. If a variable has been declared but not initialized, write ?. Otherwise, fill in the value of each variable immediately after the labeled line finishes executing.

9. Trace a and b after each line executes.

int a = 7;double b = a / 2;a = a + 1;b = a / 2.0;

If a variable hasn't been declared, write DNE for Does Not Exist. If a variable has been declared but not initialized, write ?. Otherwise, fill in the value of each variable immediately after the labeled line finishes executing.

10. Trace result and x after each line executes.

String result = "Total: ";int x = 5;result = result + x + 1;result = result + (x + 1);

If a variable hasn't been declared, write DNE for Does Not Exist. If a variable has been declared but not initialized, write ?. Otherwise, fill in the value of each variable immediately after the labeled line finishes executing.

11. Trace a and c after each line executes.

int a = 7;int b = 2;int c = a / b + a % b;a = c * 2;

If a variable hasn't been declared, write DNE for Does Not Exist. If a variable has been declared but not initialized, write ?. Otherwise, fill in the value of each variable immediately after the labeled line finishes executing.

12. What is the value of x at the end?

int x = 10;int y = 3;x = x % y;y = x + y;x = y - x;

13. What is the value of p at the end?

double p = 5;double q = 2;p = p / q;q = p * q;p = q - p;

14. What is the value of s at the end?

String s = "code";s = s + s.length();s = s.substring(0, 3);

Your score for your class is recorded when you click Submit all — checking individual questions doesn't save it. You can submit again to improve your score; your best one counts.