LearnFrenzy
Sign in Take a free test

Control Instructions · Exercise 3 of 4

Control Instructions Find the Error

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

Each program contains a decision-making or jump bug – some will not compile, others compile and quietly do the wrong thing. 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 char *cmd = "start";
    switch (cmd) {
    case "start": puts("starting"); break;
    case "stop":  puts("stopping"); break;
    }
    return 0;
}
A The cases need default
B switch needs an integer expression and integer constant labels; strings must be compared with strcmp()
C cmd must be a char array
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>

#define START 1
#define RESUME 1

int main(void) {
    int code = 1;
    switch (code) {
    case START:  puts("start");  break;
    case RESUME: puts("resume"); break;
    }
    return 0;
}
A Macros cannot be case labels
B START and RESUME both expand to 1, and two case labels may not have the same value
C code must be a char
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 main(void) {
    int limit = 10, n = 10;
    switch (n) {
    case limit: puts("at the limit"); break;
    default:    puts("below");
    }
    return 0;
}
A n and limit must be unsigned
B A case label must be a constant expression; limit is a variable
C default must come first
D There is no bug
Question page & discussion
Question 4 Easy

The status code is 404. What does this program print, and why?

main.c
#include <stdio.h>

int main(void) {
    int code = 404;
    switch (code) {
    case 200: puts("OK");
    case 404: puts("Not Found");
    case 500: puts("Server Error");
              break;
    default:  puts("Unknown");
    }
    return 0;
}
A Not Found
B Not Found and Server Error – case 404 has no break, so it falls through into case 500
C OK, Not Found and Server Error
D Unknown
Question page & discussion
Question 5 Easy

The user has 3 attempts left. What does this program print, and why?

main.c
#include <stdio.h>

int main(void) {
    int attempts = 3;
    if (attempts = 0)
        puts("locked");
    else
        printf("%d attempts left\n", attempts);
    return 0;
}
A 3 attempts left
B 0 attempts left – attempts = 0 assigns 0 (false), so the else runs with the changed value
C locked
D It does not compile
Question page & discussion
Question 6 Easy

The balance is positive. What does this program print, and why?

main.c
#include <stdio.h>

int main(void) {
    int balance = 250;
    if (balance < 0);
        puts("Account overdrawn!");
    puts("Done");
    return 0;
}
A Done
B Account overdrawn! and Done – the ; is the if's body, so the next line always runs
C Nothing
D It does not compile
Question page & discussion
Question 7 Hard

Compiled as C17, this program is rejected. Why?

main.c
#include <stdio.h>

int main(void) {
    int n = 1;
    switch (n) {
    case 1:
        int doubled = n * 2;
        printf("%d\n", doubled);
        break;
    default:
        puts("other");
    }
    return 0;
}
A doubled must be declared before main()
B Before C23 a label must be followed by a statement, and a declaration is not a statement; wrap the case in braces
C printf cannot be used in a case
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 n = 3;
    goto fill;
    int values[n];
fill:
    for (int i = 0; i < n; i++)
        values[i] = i;
    printf("%d\n", values[2]);
    return 0;
}
A n must be a constant
B The goto jumps into the scope of the variable length array values, past its declaration, which C forbids
C A label cannot be followed by a for loop
D There is no bug
Question page & discussion
Up next · Exercise 4Decision Making Programs8 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links