LearnFrenzy
Sign in Take a free test

Functions · Exercise 3 of 4

Functions Find the Error

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

Each program contains a function bug – from things modern compilers refuse to build to crashes and results that depend on the compiler. Choose the option that names the real problem.

Question 1 Easy

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

main.c
#include <stdio.h>

int sign(int x) {
    if (x > 0)
        return 1;
    if (x < 0)
        return -1;
}

int main(void) {
    printf("%d\n", sign(0));
    return 0;
}
A sign() must be declared void
B For x == 0 the function ends without a return, and main() uses the missing value – undefined behaviour
C There is no error; it prints 0
D x must be unsigned
Question page & discussion
Question 2 Easy

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

main.c
#include <stdio.h>

int main(void) {
    printf("%d\n", triple(4));
    return 0;
}

int triple(int x) {
    return 3 * x;
}
A triple() is called before any declaration; since C99 that is an error – add a prototype
B triple() must be defined inside main()
C printf cannot print a function result
D There is no error; it prints 12
Question page & discussion
Question 3 Easy

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

main.c
#include <stdio.h>

int sum_to(int n) {
    return n + sum_to(n - 1);
}

int main(void) {
    printf("%d\n", sum_to(5));
    return 0;
}
A sum_to() has no base case, so it calls itself forever and overflows the stack
B n - 1 should be n--
C A function cannot call itself
D There is no error; it prints 15
Question page & discussion
Question 4 Hard

This program compiled (with warnings) as C17. Why does it fail to compile as C23?

main.c
#include <stdio.h>

int area();

int main(void) {
    printf("%d\n", area(3, 4));
    return 0;
}

int area(int w, int h) {
    return w * h;
}
A C23 removed the * operator for ints
B In C23 int area(); means area takes no parameters, so area(3, 4) and the later definition conflict with it
C printf() needs %ld in C23
D main() must come last in C23
Question page & discussion
Question 5 Hard

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

main.c
#include <stdio.h>

int seed(void) { return 7; }

int next(void) {
    static int s = seed();
    return ++s;
}

int main(void) {
    printf("%d\n", next());
    return 0;
}
A A static variable cannot be an int
B static int s = seed(); – a static variable must be initialised with a constant, but seed() is a function call
C next() must be static too
D There is no error; it prints 8
Question page & discussion
Question 6 Medium

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

main.c
#include <stdio.h>

int fact(int n) {
    return n <= 1 ? 1 : n * fact(n - 1);
}

int main(void) {
    printf("%d\n", fact(13));
    return 0;
}
A The recursion never ends
B 13! = 6227020800 does not fit in a 32-bit int; signed overflow is undefined behaviour
C fact() needs a prototype
D There is no error; it prints 6227020800
Question page & discussion
Question 7 Hard

The author expects this program to print 1 2. What is the problem?

main.c
#include <stdio.h>

int counter = 0;

int next(void) {
    return ++counter;
}

int main(void) {
    printf("%d %d\n", next(), next());
    return 0;
}
A next() is called only once
B The evaluation order of function arguments is unspecified, so it may print 1 2 or 2 1
C counter must be static
D There is no problem; C always evaluates left to right
Question page & discussion
Question 8 Medium

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

main.c
#include <stdio.h>

int add(int a, int b) { return a + b; }

int main(void) {
    int (*f)(int) = add;
    printf("%d\n", f(2));
    return 0;
}
A f must be declared with *f(int)
B add takes two ints, but f points to a function taking one int – the types do not match
C Function pointers cannot be initialised
D There is no error; it prints 2
Question page & discussion
Up next · Exercise 4Function Programs8 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links