LearnFrenzy
Sign in Take a free test

C Variables · Exercise 2 of 4

C Variables Predict the Output

  • 12 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each program and choose what it prints. Every program was compiled and run, so the answers are the real output.

Question 1 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    for (int i = 0; i < 3; i++) {
        static int s = 0;
        int a = 0;
        s++;
        a++;
        printf("%d%d ", s, a);
    }
    printf("\n");
    return 0;
}
A 11 21 31
B 11 11 11
C 11 22 33
D 01 11 21
Question page & discussion
Question 2 Hard

What is the output of the following C program?

main.c
#include <stdio.h>

int n = 5;

void bump(void) { n++; }

int main(void) {
    int n = 1;
    bump();
    bump();
    {
        extern int n;
        printf("%d ", n);
    }
    printf("%d\n", n);
    return 0;
}
A 7 1
B 1 1
C 7 7
D 5 1
Question page & discussion
Question 3 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    static int counts[5];
    counts[2] += 3;
    printf("%d %d\n", counts[2], counts[4]);
    return 0;
}
A 3 0
B 3 garbage
C garbage garbage
D 0 0
Question page & discussion
Question 4 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int f(void) {
    static int x = 10;
    return x++;
}

int main(void) {
    printf("%d ", f());
    printf("%d ", f());
    printf("%d\n", f());
    return 0;
}
A 10 11 12
B 10 10 10
C 11 12 13
D 0 1 2
Question page & discussion
Question 5 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int i = 100;
    for (int i = 0; i < 3; i++) {
    }
    printf("%d\n", i);
    return 0;
}
A 100
B 3
C 2
D Compilation error
Question page & discussion
Question 6 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    for (int k = 0; k < 3; k++) {
        int total = 0;
        total += k;
        printf("%d ", total);
    }
    printf("\n");
    return 0;
}
A 0 1 2
B 0 1 3
C 3 3 3
D 0 0 0
Question page & discussion
Question 7 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a = 1, b = a + 1, c = a + b;
    printf("%d %d %d\n", a, b, c);
    return 0;
}
A 1 2 3
B 1 1 2
C 1 garbage garbage
D Compilation error
Question page & discussion
Question 8 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a = 4, b = 9;
    a = a + b;
    b = a - b;
    a = a - b;
    printf("%d %d\n", a, b);
    return 0;
}
A 9 4
B 4 9
C 13 4
D 9 13
Question page & discussion
Question 9 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int depth(int n) {
    static int calls = 0;
    calls++;
    if (n > 0)
        depth(n - 1);
    return calls;
}

int main(void) {
    printf("%d\n", depth(3));
    return 0;
}
A 4
B 1
C 3
D 0
Question page & discussion
Question 10 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int count = 100;

void tick(void) {
    static int count = 0;
    count++;
    printf("%d ", count);
}

int main(void) {
    tick();
    tick();
    printf("%d\n", count);
    return 0;
}
A 1 2 100
B 101 102 102
C 1 2 2
D 101 102 100
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links