LearnFrenzy
Sign in Take a free test

C Loops · Exercise 3 of 4

C Loops Find the Error

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

Each program contains a loop bug – some do not compile, some loop forever, some crash, and some quietly give the wrong answer. 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 a[3] = {1, 2, 3};
    for (unsigned i = 2; i >= 0; i--)
        printf("%d ", a[i]);
    printf("\n");
    return 0;
}
A a must be declared unsigned
B i >= 0 is always true for an unsigned i; after 0 it wraps to 4294967295 and a[i] is far out of bounds
C printf cannot print inside a loop
D There is no bug; it prints 3 2 1
Question page & discussion
Question 2 Easy

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

main.c
#include <stdio.h>

int main(void) {
    int i = 0;
    while (i < 3);
    {
        printf("%d\n", i);
        i++;
    }
    return 0;
}
A The braces cannot follow a while
B The ; after while (i < 3) is the loop body, so the loop repeats an empty statement forever and never reaches the block
C i must start at 1
D There is no bug; it prints 0 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) {
    int a[] = {4, 8, 15, 16};
    for (int i = 0; i < 4; i++)
        if (a[i] == 15)
            break;
    printf("found at %d\n", i);
    return 0;
}
A break cannot be used in a for loop
B i is declared in the for header, so it does not exist after the loop
C a must have a size
D There is no bug; it prints found at 2
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 n = 3;
    do {
        printf("%d ", n);
        n--;
    } while (n > 0)
    printf("\n");
    return 0;
}
A do-while needs a for header
B A do-while statement must end with a semicolon after the condition
C n-- must come before printf
D There is no bug
Question page & discussion
Question 5 Medium

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

main.c
#include <stdio.h>

int main(void) {
    int sum = 0;
    for (int i = 1; i <= 2000; i++)
        sum += i * i * i;
    printf("%d\n", sum);
    return 0;
}
A The loop runs one time too many
B The sum (about 4 × 10¹²) far exceeds INT_MAX, and i * i * i itself overflows for large i – signed overflow is undefined
C i * i * i must be written pow(i, 3)
D There is no bug
Question page & discussion
Question 6 Hard

The loop should stop when x reaches 1.0 (after 10 steps). A safety limit stops it at 100 steps. What does the program print, and why?

main.c
#include <stdio.h>

int main(void) {
    int steps = 0;
    for (float x = 0.0f; x != 1.0f && steps < 100; x += 0.1f)
        steps++;
    printf("%d\n", steps);
    return 0;
}
A 10 – x becomes exactly 1.0
B 100 – the sum of 0.1f steps never equals 1.0f exactly, so only the safety limit stops the loop
C 11 – one step too many
D It does not compile
Question page & discussion
Question 7 Easy

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

main.c
#include <stdio.h>

int main(void) {
    int age = 15;
    if (age < 18) {
        puts("minor");
        break;
    }
    puts("checked");
    return 0;
}
A puts cannot be used in an if
B break is only allowed inside a loop or a switch, not to leave an if
C age must be unsigned
D There is no bug
Question page & discussion
Question 8 Hard

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

main.c
#include <stdio.h>

int main(void) {
    int count = 0;
    for (signed char c = 0; c < 200; c++)
        count++;
    printf("%d\n", count);
    return 0;
}
A signed char cannot be used in a loop
B A signed char holds at most 127, so c < 200 is always true and the loop never ends
C count overflows
D There is no bug; it prints 200
Question page & discussion
Up next · Exercise 4Loop Programs8 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links