LearnFrenzy
Sign in Take a free test

Declarations and Initializations · Exercise 3 of 4

Declarations and Initializations Find the Error

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

Each program contains a declaration or initialisation mistake that the compiler rejects. 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) {
    total = 10;
    int total;
    printf("%d\n", total);
    return 0;
}
A total must be global
B total is used before its declaration; a name is only visible after the point where it is declared
C printf needs %ld
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>

int count;
double count;

int main(void) {
    printf("%d\n", (int) count);
    return 0;
}
A Globals cannot be redeclared
B The two declarations of count give it conflicting types
C count must be initialised
D There is no bug
Question page & discussion
Question 3 Medium

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

main.c
#include <stdio.h>

int get_limit(void) { return 50; }

int limit = get_limit();

int main(void) {
    printf("%d\n", limit);
    return 0;
}
A get_limit must be static
B Objects with static storage must be initialised with constant expressions; a function call is evaluated at run time
C limit must be const
D There is no bug
Question page & discussion
Question 4 Easy

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

main.c
#include <stdio.h>

int main(void) {
    int scores[3] = {90, 85, 70, 60};
    printf("%d\n", scores[0]);
    return 0;
}
A scores must be global
B The list has 4 values for a 3-element array – excess initializers are not allowed
C The extra value is ignored silently, so there is no bug
D Array sizes must be even
Question page & discussion
Question 5 Easy

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

main.c
#include <stdio.h>

int main(void) {
    const int max;
    max = 10;
    printf("%d\n", max);
    return 0;
}
A const must be global
B A const object can only get its value from its initializer; assigning to it later is an error
C max must be unsigned
D There is no bug
Question page & discussion
Question 6 Hard

Compiled as C17, why is this program rejected?

main.c
#include <stdio.h>

int main(void) {
    int n = 5;
    int counts[n] = {0};
    printf("%d\n", counts[0]);
    return 0;
}
A n must be unsigned
B counts is a variable length array, and a VLA cannot have an initializer (C23 allows only = {})
C {0} needs five zeros
D There is no bug
Question page & discussion
Question 7 Hard

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

main.c
#include <stdio.h>

int main(void) {
    extern int limit = 5;
    printf("%d\n", limit);
    return 0;
}
A extern cannot be used with int
B An extern declaration at block scope only refers to an object defined elsewhere; it cannot also define it with an initializer
C limit must be static
D There is no bug
Question page & discussion
Question 8 Medium

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

main.c
#include <stdio.h>

int main(void) {
    int x = 3, y = 4;
    int* p, q;
    p = &x;
    q = &y;
    printf("%d %d\n", *p, *q);
    return 0;
}
A x and y must be pointers
B Only p is a pointer; q is an int, so q = &y and *q are invalid
C p must be initialised in its declaration
D There is no bug
Question page & discussion
Up next · Exercise 4Declaration Programs8 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links