LearnFrenzy
Sign in Take a free test

C Loops · Exercise 2 of 4

C Loops 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 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int i;
    for (i = 0; i < 5; i++);
    printf("%d\n", i);
    return 0;
}
A 5
B 01234
C 4
D 0
Question page & discussion
Question 2 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int n = 3, count = 0;
    while (n--)
        count++;
    printf("%d %d\n", count, n);
    return 0;
}
A 3 -1
B 3 0
C 2 0
D 4 -1
Question page & discussion
Question 3 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int x = 10;
    do {
        printf("%d ", x);
        x++;
    } while (x < 5);
    printf("| %d\n", x);
    return 0;
}
A 10 | 11
B | 10
C 10 11 | 12
D Nothing
Question page & discussion
Question 4 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
            if (j == 2)
                break;
            printf("%d%d ", i, j);
        }
    }
    printf("\n");
    return 0;
}
A 11 21 31
B 11
C 11 12 13
D 11 21 31 12
Question page & discussion
Question 5 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int sum = 0;
    for (int i = 1; i <= 10; i++) {
        if (i % 2)
            continue;
        sum += i;
    }
    printf("%d\n", sum);
    return 0;
}
A 30
B 25
C 55
D 20
Question page & discussion
Question 6 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int i, j;
    for (i = 0, j = 10; i < j; i += 3, j -= 2) {
    }
    printf("%d %d\n", i, j);
    return 0;
}
A 6 6
B 9 4
C 3 8
D 6 8
Question page & discussion
Question 7 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int n = 0, a = 0, b = 0;
    int t = n;
    while (t > 0) {
        t /= 10;
        a++;
    }
    t = n;
    do {
        t /= 10;
        b++;
    } while (t > 0);
    printf("%d %d\n", a, b);
    return 0;
}
A 0 1
B 1 1
C 0 0
D 1 0
Question page & discussion
Question 8 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int count = 0;
    for (int i = 0; i < 4; i++)
        for (int j = i; j < 4; j++)
            count++;
    printf("%d\n", count);
    return 0;
}
A 10
B 16
C 6
D 4
Question page & discussion
Question 9 Hard

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int steps = 0;
    for (double x = 0.0; x < 1.0; x += 0.1)
        steps++;
    printf("%d\n", steps);
    return 0;
}
A 10
B 11
C 9
D It never ends
Question page & discussion
Question 10 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int i = 0, n = 0;
    while (i++ < 3)
        n++;
    printf("%d %d\n", n, i);
    return 0;
}
A 3 4
B 3 3
C 4 4
D 2 3
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links