LearnFrenzy
Sign in Take a free test

Operators and Expressions · Exercise 3 of 4

Operators and Expressions Find the Error

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

Each program misuses an operator – undefined expressions, type errors, overflow and precedence traps. Choose the option that names the real problem.

Question 1 Medium

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

main.c
#include <stdio.h>

int main(void) {
    int count = 0;
    for (int i = 0; i < 3; i++)
        count = count++;
    printf("%d\n", count);
    return 0;
}
A The loop runs the wrong number of times
B count = count++ modifies count twice without a sequence point – undefined behaviour; write count++;
C count must be unsigned
D There is no bug; it prints 3
Question page & discussion
Question 2 Medium

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

main.c
#include <stdio.h>

int main(void) {
    int i = 1;
    printf("%d %d\n", i++, i++);
    return 0;
}
A printf cannot take expressions
B Both arguments modify i, and argument evaluation is unsequenced – undefined behaviour
C i must be initialised to 0
D There is no bug; it always prints 1 2
Question page & discussion
Question 3 Easy

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

main.c
#include <stdio.h>

int main(void) {
    double minutes = 135.5;
    double rest = minutes % 60;
    printf("%.1f\n", rest);
    return 0;
}
A 60 must be 60.0
B The % operator requires integer operands; use fmod(minutes, 60) from <math.h>
C rest must be an int
D There is no bug
Question page & discussion
Question 4 Medium

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

main.c
#include <stdio.h>

int average(int a, int b) {
    return (a + b) / 2;
}

int main(void) {
    printf("%d\n", average(2000000000, 2100000000));
    return 0;
}
A The division should be by 2.0
B a + b exceeds INT_MAX – signed overflow is undefined; use a + (b - a) / 2 or a wider type
C average() must return long
D There is no bug
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) {
    int a = 2, b = 3;
    int c = (a + b)++;
    printf("%d\n", c);
    return 0;
}
A c must be declared first
B ++ needs a modifiable lvalue (a variable); a + b is just a value
C Brackets cannot be used with ++
D There is no bug; it prints 5
Question page & discussion
Question 6 Hard

The function should add 1 to the caller's counter three times. What does the program print, and why?

main.c
#include <stdio.h>

void tick(int *count) {
    *count++;
}

int main(void) {
    int clicks = 0;
    tick(&clicks);
    tick(&clicks);
    tick(&clicks);
    printf("%d\n", clicks);
    return 0;
}
A 3
B 0 – postfix ++ binds tighter than *, so *count++ moves the pointer and leaves the counter unchanged
C 1
D It crashes
Question page & discussion
Question 7 Medium

The program should print 12 (double of 3, times 2). What does it print, and why?

main.c
#include <stdio.h>

#define DOUBLE(x) x + x

int main(void) {
    printf("%d\n", DOUBLE(3) * 2);
    return 0;
}
A 12
B 9 – the expansion is 3 + 3 * 2, and * binds before +
C 6
D It does not compile
Question page & discussion
Question 8 Medium

The programmer expected the counter to be 3. What does the program print, and why?

main.c
#include <stdio.h>

int main(void) {
    int counter = 0;
    size_t total = 0;
    for (int i = 0; i < 3; i++)
        total += sizeof(counter++);
    printf("%d %zu\n", counter, total);
    return 0;
}
A 3 12
B 0 12 – sizeof only looks at the type of its operand and never executes counter++
C 3 3
D It does not compile
Question page & discussion
Up next · Exercise 4Operator Programs8 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links