LearnFrenzy
Sign in Take a free test

C Const · Exercise 2 of 4

C Const 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) {
    int x = 5, y = 7;
    const int *p = &x;
    x = 6;
    printf("%d ", *p);
    p = &y;
    printf("%d\n", *p);
    return 0;
}
A 6 7
B 5 7
C 5 5
D Compilation error
Question page & discussion
Question 2 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

#define SQUARE(x) x * x

static inline int square(int x) { return x * x; }

int main(void) {
    printf("%d %d\n", SQUARE(2 + 3), square(2 + 3));
    return 0;
}
A 11 25
B 25 25
C 11 11
D 25 11
Question page & discussion
Question 3 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

#define TOTAL 10 + 5
enum { TOTAL_E = 10 + 5 };

int main(void) {
    printf("%d %d\n", TOTAL * 2, TOTAL_E * 2);
    return 0;
}
A 20 30
B 30 30
C 30 20
D 20 20
Question page & discussion
Question 4 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    for (int i = 0; i < 4; i++) {
        const int sq = i * i;
        printf("%d ", sq);
    }
    printf("\n");
    return 0;
}
A 0 1 4 9
B 0 0 0 0
C Compilation error: sq is assigned 4 times
D 0 1 2 3
Question page & discussion
Question 5 Medium

What is the output on a 64-bit system?

main.c
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *s = "const";
    const char t[] = "const";
    printf("%zu %zu %zu\n", sizeof s, sizeof t, strlen(s));
    return 0;
}
A 8 6 5
B 6 6 5
C 8 5 5
D 5 6 5
Question page & discussion
Question 6 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

static const int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main(void) {
    int first_half = 0;
    for (int m = 0; m < 6; m++)
        first_half += days[m];
    printf("%d\n", first_half);
    return 0;
}
A 181
B 182
C 184
D 180
Question page & discussion
Question 7 Easy

What is the output of the following C program?

main.c
#include <stdio.h>
#include <string.h>

int main(void) {
    static const char vowels[] = "aeiou";
    const char *word = "constant";
    int n = 0;
    for (const char *p = word; *p; p++)
        if (strchr(vowels, *p))
            n++;
    printf("%d\n", n);
    return 0;
}
A 2
B 3
C 1
D 8
Question page & discussion
Question 8 Hard

What is the output of the following C program?

main.c
#include <stdio.h>

void setup(void) {
#define LIMIT 10
}

int main(void) {
    setup();
    printf("%d\n", LIMIT);
    return 0;
}
A 10
B Compilation error: LIMIT is not in scope
C 0
D Garbage
Question page & discussion
Question 9 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

static const char *const colors[] = {"red", "green", "blue"};

int main(void) {
    printf("%s %c %zu\n", colors[1], colors[2][0], sizeof colors / sizeof colors[0]);
    return 0;
}
A green b 3
B green b 13
C red g 3
D green blue 3
Question page & discussion
Question 10 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

const char *level_name(int level) {
    static const char *const names[] = {"low", "medium", "high"};
    return names[level];
}

int main(void) {
    const char *a = level_name(1);
    const char *b = level_name(1);
    printf("%d %s\n", a == b, a);
    return 0;
}
A 1 medium
B 0 medium
C 1 low
D 0 high
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links