πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English
Advertisement

Memory Trace Problems

Read each code snippet and trace how every variable's value changes. Try solving them with pen and paper first.

Tracing Variables & Assignment

T-01Variable
int a = 5, b = 10;
a = b;
b = a + 3;
a = b - a;
What are the final values of a and b?
Line 1: a=5, b=10
Line 2: a=10, b=10 (assign b's value to a)
Line 3: a=10, b=13 (10+3)
Line 4: a=3, b=13 (13-10)
Answer: a=3, b=13
T-02Variable
int x = 7;
x = x * 2;
x += 3;
x %= 5;
x++;
What is the final value of x?
x=7 β†’ x=14 (7*2) β†’ x=17 (14+3) β†’ x=2 (17%5=remainder 2) β†’ x=3 (2+1)
Answer: x=3
T-03Variable
int a = 3, b = 5, tmp;
tmp = a;
a = b;
b = tmp;
What are the final values of a, b, tmp? (What is this doing?)
tmp=3 (save a's original value) β†’ a=5 (copy b to a) β†’ b=3 (copy saved value from tmp to b)
Answer: a=5, b=3, tmp=3
This is the classic value swap pattern.
T-04Variable
int a = 10;
int b = a / 3;
int c = a % 3;
double d = (double)a / 3;
What are the values of b, c, d?
b = 10/3 = 3 (integer division truncates)
c = 10%3 = 1 (remainder)
d = 10.0/3 = 3.333... (cast promotes to double division)
Answer: b=3, c=1, d=3.333333
T-05Variable
int a = 1;
a += a++;  // undefined behavior warning
printf("%d\n", a);
What is the output? Hint: this is a "trap" question.
This is undefined behavior. Modifying and reading the same variable in a single expression is not guaranteed by the C standard.
Different compilers may produce 2, 3, or other results.
Lesson: Never mix ++ and assignment on the same variable in a single expression.
T-06Variable
int i = 0, sum = 0;
while (i < 5) {
    sum += i;
    i++;
}
Trace i and sum after each loop iteration in a table.
Iteri (before)sum += iafter i++
100+0=01
210+1=12
321+2=33
433+3=64
546+4=105
Answer: i=5, sum=10 (0+1+2+3+4)
T-07Variable
int n = 12345, rev = 0;
while (n > 0) {
    rev = rev * 10 + n % 10;
    n /= 10;
}
Trace n and rev after each loop. What does this program do?
Itern%10revn
150*10+5=51234
245*10+4=54123
3354*10+3=54312
42543*10+2=54321
515432*10+1=543210
Answer: n=0, rev=54321
This program reverses the digits of an integer.

Tracing Arrays

T-08Array
int a[5] = {3, 1, 4, 1, 5};
a[0] = a[4];
a[2] = a[0] + a[1];
a[4] = a[2] - a[3];
What is the final state of array a?
Initial: {3,1,4,1,5}
a[0]=a[4] β†’ {5,1,4,1,5}
a[2]=a[0]+a[1] β†’ {5,1,6,1,5}
a[4]=a[2]-a[3] β†’ {5,1,6,1,5}
Answer: {5, 1, 6, 1, 5}
T-09Array
int a[4] = {10, 20, 30, 40};
for (int i = 0; i < 3; i++) {
    a[i] = a[i + 1];
}
What is the final state of a? What is this code doing?
i=0: a[0]=a[1] β†’ {20,20,30,40}
i=1: a[1]=a[2] β†’ {20,30,30,40}
i=2: a[2]=a[3] β†’ {20,30,40,40}
Answer: {20, 30, 40, 40}
This removes the first element by shifting all elements left (the last element remains duplicated).
T-10Array
int a[5] = {2, 8, 3, 9, 1};
int max = a[0], idx = 0;
for (int i = 1; i < 5; i++) {
    if (a[i] > max) {
        max = a[i];
        idx = i;
    }
}
Trace how max and idx change at each step.
ia[i]a[i]>max?maxidx
init--20
188>2 Yes81
233>8 No81
399>8 Yes93
411>9 No93
Answer: max=9, idx=3
T-11Array
int a[5] = {5, 3, 8, 1, 4};
// First pass of bubble sort (bringing the min to the front)
for (int j = 4; j > 0; j--) {
    if (a[j-1] > a[j]) {
        int tmp = a[j-1]; a[j-1] = a[j]; a[j] = tmp;
    }
}
Trace the array state after one pass.
Initial: {5,3,8,1,4}
j=4: a[3]>a[4]? 1>4 No β†’ {5,3,8,1,4}
j=3: a[2]>a[3]? 8>1 Yes β†’ {5,3,1,8,4}
j=2: a[1]>a[2]? 3>1 Yes β†’ {5,1,3,8,4}
j=1: a[0]>a[1]? 5>1 Yes β†’ {1,5,3,8,4}
Answer: {1, 5, 3, 8, 4} (the minimum value 1 bubbles to the front)
T-12Array
int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int sum = 0;
for (int i = 0; i < 3; i++) {
    sum += a[i][i];
}
What is the value of sum? What does this compute?
a[0][0]=1, a[1][1]=5, a[2][2]=9
sum = 1+5+9 = 15
This computes the trace of a matrix (sum of the main diagonal elements).

Tracing Pointers

T-13Pointer
int a = 10, b = 20;
int *p = &a;
*p = 30;
p = &b;
*p = *p + 5;
What are the final values of a, b, and *p?
p=&a β†’ *p=30 β†’ a=30
p=&b β†’ *p=20+5=25 β†’ b=25
Answer: a=30, b=25, *p=25
T-14Pointer
int a = 5, b = 10;
int *p = &a, *q = &b;
*p = *q;
*q = 0;
printf("%d %d\n", a, b);
What is the output?
*p=*q β†’ a=10 (copy b's value into a)
*q=0 β†’ b=0
Answer: 10 0
T-15Pointer
int arr[4] = {10, 20, 30, 40};
int *p = arr;
printf("%d ", *p);
p += 2;
printf("%d ", *p);
printf("%d ", *(p - 1));
printf("%d\n", p[1]);
What is the output?
p=arr β†’ *p=10
p+=2 β†’ p points to arr[2] β†’ *p=30
*(p-1)=arr[1]=20
p[1]=*(p+1)=arr[3]=40
Answer: 10 30 20 40
T-16Pointer
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr + 1;
int *q = arr + 3;
printf("%d %d %ld\n", *p, *q, q - p);
What is the output?
p=&arr[1] β†’ *p=2
q=&arr[3] β†’ *q=4
q-p = 3-1 = 2 (difference in element count)
Answer: 2 4 2
T-17Pointer
int x = 100;
int *p = &x;
int **pp = &p;
**pp = 200;
printf("%d %d %d\n", x, *p, **pp);
What is the output? (Double pointer)
pp β†’ p β†’ x chain. **pp=200 modifies x to 200.
x=200, *p=200, **pp=200 (all refer to the same location)
Answer: 200 200 200
T-18Pointer
int a[] = {10, 20, 30, 40, 50};
int *p = a;
for (int i = 0; i < 5; i++) {
    *(p + i) *= 2;
}
What is the final state of a?
Double each element: {20, 40, 60, 80, 100}
*(p+i) is equivalent to a[i]. Manipulating the array via pointer arithmetic.

Tracing Function Calls

T-19Function
void add_ten(int x) { x += 10; }

int main(void) {
    int a = 5;
    add_ten(a);
    printf("%d\n", a);
}
What is the output?
Answer: 5
C uses pass-by-value. x inside add_ten is a copy of a, so changing x has no effect on a.
T-20Function
void add_ten(int *px) { *px += 10; }

int main(void) {
    int a = 5;
    add_ten(&a);
    printf("%d\n", a);
}
What is the output? (Compare with T-19.)
Answer: 15
Because we pass a pointer, *px += 10 modifies a itself.
T-21Function
int mystery(int a, int b) {
    a = a + b;
    b = a - b;
    a = a - b;
    return a * 10 + b;
}
printf("%d\n", mystery(3, 7));
What is the output? Trace a and b line by line.
a=3, b=7
a=3+7=10
b=10-7=3
a=10-3=7
return 7*10+3 = 73
This is a swap without a temporary variable. a and b are exchanged, so the result is 7*10+3=73.
T-22Function
void modify(int arr[], int n) {
    for (int i = 0; i < n; i++) arr[i] *= 3;
}
int main(void) {
    int data[] = {1, 2, 3};
    modify(data, 3);
    printf("%d %d %d\n", data[0], data[1], data[2]);
}
What is the output? (Array pass vs. int pass)
Answer: 3 6 9
Arrays are passed as pointers, so modifications inside the function affect the caller. This is different from pass-by-value for ints (T-19)!
T-23Function
int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}
printf("%d\n", factorial(5));
Trace the call stack's expansion and return.
factorial(5) β†’ 5 * factorial(4)
β†’ 5 * 4 * factorial(3)
β†’ 5 * 4 * 3 * factorial(2)
β†’ 5 * 4 * 3 * 2 * factorial(1)
β†’ 5 * 4 * 3 * 2 * 1 (base case)
= 5*4*3*2*1 = 120

Tracing Strings

T-24String
char s[] = "Hello";
s[0] = 'J';
s[4] = '!';
printf("%s\n", s);
What is the output? What are the bytes in memory?
index012345
initialHello\0
afterJell!\0
Answer: Jell!
T-25String
char s[] = "abcde";
int len = 0;
while (s[len] != '\0') len++;
printf("%d\n", len);
Trace how len changes and give the output.
s[0]='a'β‰ '\0' β†’ len=1
s[1]='b'β‰ '\0' β†’ len=2
s[2]='c'β‰ '\0' β†’ len=3
s[3]='d'β‰ '\0' β†’ len=4
s[4]='e'β‰ '\0' β†’ len=5
s[5]='\0' β†’ loop exits
Answer: 5 (equivalent to strlen)
T-26String
char s[] = "Hello";
for (int i = 0; s[i]; i++) {
    if (s[i] >= 'a' && s[i] <= 'z')
        s[i] -= 32;
}
printf("%s\n", s);
Trace the change of each character.
H: uppercase β†’ no change
e: lowercase β†’ e-32=E
l: lowercase β†’ l-32=L
l: lowercase β†’ l-32=L
o: lowercase β†’ o-32=O
Answer: HELLO (lowercase to uppercase conversion; the ASCII difference is 32)

Mixed Problems

T-27Mixed
struct Point { int x, y; };
struct Point a = {3, 7};
struct Point b = a;
b.x = 10;
printf("a=(%d,%d) b=(%d,%d)\n", a.x, a.y, b.x, b.y);
What is the output? (Is struct assignment a copy or a reference?)
Answer: a=(3,7) b=(10,7)
Struct assignment is a value copy. b is an independent copy of a, so changing b.x does not affect a.x.
T-28Mixed
struct Point { int x, y; };
struct Point p = {1, 2};
struct Point *ptr = &p;
ptr->x = 10;
ptr->y = ptr->x + 5;
printf("(%d,%d)\n", p.x, p.y);
What is the output? (Modification via arrow operator)
ptr->x=10 β†’ p.x=10
ptr->y=10+5=15 β†’ p.y=15
Answer: (10,15)
T-29Mixed
int a[3] = {1, 2, 3};
int *p = a;
for (int i = 0; i < 3; i++) {
    a[i] = a[(2-i)];
}
What is the final state of a? (Hint: it does NOT become a reversed array. Why?)
i=0: a[0]=a[2] β†’ {3,2,3}
i=1: a[1]=a[1] β†’ {3,2,3}
i=2: a[2]=a[0] β†’ {3,2,3} (a[0] is already 3!)
Answer: {3, 2, 3} (NOT {3, 2, 1})
Reason: The earlier iterations overwrite the beginning of the array, so later reads pick up already-modified values. A correct reversal needs a temporary array or two-pointer swap.
T-30Mixed
int f(int *a, int *b) {
    *a += *b;
    *b = *a - *b;
    return *a + *b;
}
int main(void) {
    int x = 4, y = 6;
    int z = f(&x, &y);
    printf("%d %d %d\n", x, y, z);
}
What is the output? (Trace modifications via pointers.)
*a=&x, *b=&y
*a += *b β†’ x=4+6=10
*b = *a-*b β†’ y=10-6=4
return 10+4=14
Answer: 10 4 14
T-31Mixed
int a[5] = {5, 2, 8, 1, 9};
for (int i = 0; i < 4; i++) {
    if (a[i] > a[i+1]) {
        int t = a[i]; a[i] = a[i+1]; a[i+1] = t;
    }
}
What is the final state of a? What is guaranteed about it?
i=0: 5>2 β†’ swap β†’ {2,5,8,1,9}
i=1: 5>8? No β†’ {2,5,8,1,9}
i=2: 8>1 β†’ swap β†’ {2,5,1,8,9}
i=3: 8>9? No β†’ {2,5,1,8,9}
Answer: {2, 5, 1, 8, 9}
It is guaranteed that the maximum value (9) ends up at the last position (one forward-scanning pass of bubble sort).
T-32Mixed
int count = 0;
for (int i = 1; i <= 100; i++) {
    if (i % 3 == 0 && i % 5 == 0)
        count++;
}
What is the final value of count? (You don't need to trace everything β€” find the pattern.)
Divisible by both 3 and 5 β†’ counts multiples of 15.
Multiples of 15 in 1..100: 15, 30, 45, 60, 75, 90 β€” 6 values.
Answer: count=6
T-33Mixed
int fib(int n) {
    if (n <= 1) return n;
    return fib(n-1) + fib(n-2);
}
printf("%d\n", fib(6));
Give the value of fib(6) and how many times fib is called in total.
fib(6)=fib(5)+fib(4)=5+3=8
Call count: expanding fib(6)β†’fib(5),fib(4)β†’... gives 25 calls.
fib(0)=0, fib(1)=1, fib(2)=1, fib(3)=2, fib(4)=3, fib(5)=5, fib(6)=8
T-34Mixed
char *p = "ABCDE";
printf("%c %c %s\n", *p, *(p+2), p+3);
What is the output? (String literal pointer operations)
*p = 'A'
*(p+2) = 'C'
p+3 = "DE" (the string starting at offset 3)
Answer: A C DE
T-35Mixed
int m[2][3] = {{1,2,3},{4,5,6}};
int result = 0;
for (int i = 0; i < 2; i++)
    for (int j = 0; j < 3; j++)
        if (m[i][j] % 2 == 0) result += m[i][j];
What is the value of result?
Even elements: 2, 4, 6
result = 2+4+6 = 12
T-36Mixed
int a = 0xAB;  // hexadecimal
int hi = (a >> 4) & 0x0F;
int lo = a & 0x0F;
printf("hi=%d lo=%d\n", hi, lo);
What is the output? (Bitwise operation trace)
0xAB = 1010 1011 (binary)
a>>4 = 0000 1010 β†’ & 0x0F β†’ 10 (0x0A)
a & 0x0F = 0000 1011 β†’ 11 (0x0B)
Answer: hi=10 lo=11 (upper 4 bits = A = 10, lower 4 bits = B = 11)
T-37Mixed
int gcd(int a, int b) {
    while (b != 0) {
        int tmp = b;
        b = a % b;
        a = tmp;
    }
    return a;
}
printf("%d\n", gcd(48, 18));
Trace a, b, and tmp in each iteration.
Iterabtmpa%b
148181848%18=12
218121218%12=6
3126612%6=0
end60--
Answer: 6 (the greatest common divisor of 48 and 18)
T-38Mixed
int *p = (int *)malloc(sizeof(int) * 3);
p[0] = 10; p[1] = 20; p[2] = 30;
int *q = p;
free(p);
// printf("%d\n", *q); ← is this safe?
What happens if we access *q after free?
Undefined behavior (dangling pointer).
q was pointing to the same location as p, but free(p) released that memory. q still holds the same address, but the memory is no longer valid.
Lesson: After free, set all related pointers to NULL too.
T-39Mixed
int a[4] = {10, 20, 30, 40};
int *p = a, *q = a + 3;
while (p < q) {
    int t = *p; *p = *q; *q = t;
    p++; q--;
}
What is the final state of a? What is it doing?
Iter 1: p=a[0], q=a[3] β†’ swap(10,40) β†’ {40,20,30,10}, p++, q--
Iter 2: p=a[1], q=a[2] β†’ swap(20,30) β†’ {40,30,20,10}, p++, q--
p>=q β†’ exit
Answer: {40, 30, 20, 10}
This is an in-place array reverse. Swap from both ends toward the center.
T-40Mixed
int binary_search(int a[], int n, int key) {
    int lo = 0, hi = n - 1;
    while (lo <= hi) {
        int mid = (lo + hi) / 2;
        if (a[mid] == key) return mid;
        else if (a[mid] < key) lo = mid + 1;
        else hi = mid - 1;
    }
    return -1;
}
// a = {2, 5, 8, 12, 16, 23, 38, 56}, key = 23
Trace lo, hi, mid and state how many iterations it takes to find the key.
Iterlohimida[mid]decision
10731212<23 β†’ lo=4
24752323==23 β†’ found!
Answer: returns index=5 on iteration 2

Want more trace practice?

Tracing problems with pen and paper is the most effective way to prepare for exams

πŸ“˜
The C Programming Language (K&R)
B. W. Kernighan, D. M. Ritchie
The classic C reference. Perfect for stepping up after you have the basics.
View on Amazon
πŸ“—
C Programming: A Modern Approach
K. N. King
A thorough modern textbook with excellent exercises, widely used at universities.
View on Amazon
πŸ“™
Understanding and Using C Pointers
Richard Reese
A focused deep dive on pointers and memory β€” the hardest part of C for most learners.
View on Amazon

* The links above are affiliate links. Your purchase helps support this site.

Share this article
Share on X (Twitter) Share on Facebook Share on LinkedIn Share on Reddit