LearnFrenzy
Sign in Take a free test

C Const · Exercise 3 of 4

C Const Find the Error

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

Each program misuses const or a macro – the compiler refuses some, others crash or compute the wrong value. Choose the option that names the real problem.

Question 1 Easy

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

int main(void) {
    const int max_attempts = 3;
    max_attempts = 5;
    printf("%d\n", max_attempts);
    return 0;
}
A const variables must be global
B max_attempts is const, so it cannot be assigned after its initialisation
C printf cannot print a const
D There is no bug
Question page & discussion
Question 2 Easy

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

void reset(const int *counter) {
    *counter = 0;
}

int main(void) {
    int clicks = 12;
    reset(&clicks);
    printf("%d\n", clicks);
    return 0;
}
A clicks must be const
B counter points to const int, so *counter = 0 is not allowed; the parameter should be int *
C &clicks is invalid
D There is no bug
Question page & discussion
Question 3 Easy

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

struct point {
    int x, y;
};

int main(void) {
    const struct point origin = {0, 0};
    origin.x = 5;
    printf("%d\n", origin.x);
    return 0;
}
A Structs cannot be const
B origin is const, so every member is read-only; copy it into a non-const struct to change it
C x must be declared const inside the struct
D There is no bug
Question page & discussion
Question 4 Hard

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

static const int LIMIT = 10;

int main(void) {
    int *p = (int *) &LIMIT;
    *p = 20;
    printf("%d\n", LIMIT);
    return 0;
}
A The cast should be (const int *)
B LIMIT was defined const and likely sits in read-only memory; writing through the cast pointer is undefined behaviour (here: a crash)
C static and const cannot be combined
D There is no bug; it prints 20
Question page & discussion
Question 5 Hard

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

const int SIZE = 10;
int buffer[SIZE];

int main(void) {
    buffer[0] = 1;
    printf("%d\n", buffer[0]);
    return 0;
}
A buffer must be static
B In C, a const int is not a constant expression, so it cannot size a file-scope array (that would be a VLA, which is not allowed there)
C SIZE must be unsigned
D There is no bug
Question page & discussion
Question 6 Hard

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

int main(void) {
    const int QUIT = 4;
    int choice = 4;
    switch (choice) {
    case QUIT:
        puts("bye");
        break;
    default:
        puts("again");
    }
    return 0;
}
A switch needs a char
B A case label must be an integer constant expression; a const int is not one in C
C QUIT must be global
D There is no bug
Question page & discussion
Question 7 Medium

The program below has a bug. Which statement correctly describes it?

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

void capitalise(char *s) {
    s[0] = (char) toupper((unsigned char) s[0]);
}

int main(void) {
    const char *msg = "hello";
    capitalise(msg);
    puts(msg);
    return 0;
}
A toupper() needs an int
B msg points to a read-only string literal; passing it to a char * parameter discards const, and the write crashes
C puts() cannot print const strings
D There is no bug; it prints Hello
Question page & discussion
Question 8 Medium

The program should print the area of a square of side 1 + 1. What does it print, and why?

main.c
#include <stdio.h>

#define AREA(r) 3 * r * r

int main(void) {
    printf("%d\n", AREA(1 + 1));
    return 0;
}
A 12
B 5 – the macro expands to 3 * 1 + 1 * 1 + 1, because the argument is pasted as text
C 6
D It does not compile
Question page & discussion
Up next · Exercise 4const Programs8 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links