LearnFrenzy
Sign in Take a free test

Decision Making · Exercise 3 of 4

Decision Making Find the Error

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

Each program contains a decision-logic bug – conditions that are always true, guards in the wrong order, unreachable branches and illegal case labels. Choose the option that names the real problem.

Question 1 Easy

Applicants must be 18 to 60 years old. What does the program print for age 70, and why?

main.c
#include <stdio.h>

int main(void) {
    int age = 70;
    if (age >= 18 || age <= 60)
        puts("eligible");
    else
        puts("not eligible");
    return 0;
}
A not eligible
B eligible – with ||, every age satisfies one side; it must be age >= 18 && age <= 60
C Nothing
D It does not compile
Question page & discussion
Question 2 Medium

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

main.c
#include <stdio.h>

int is_admin(const char *role) {
    return role[0] == 'A' && role != NULL;
}

int main(void) {
    printf("%d\n", is_admin(NULL));
    return 0;
}
A role must not be const
B role[0] is read before role is checked for NULL; the guard must come first: role != NULL && role[0] == 'A'
C The && should be ||
D There is no bug; it prints 0
Question page & discussion
Question 3 Medium

The code should print "not five" when count is not 5. What does it print for count = 3, and why?

main.c
#include <stdio.h>

int main(void) {
    int count = 3;
    if (!count == 5)
        puts("not five");
    else
        puts("five or other");
    return 0;
}
A not five
B five or other – !count is evaluated first (0 or 1), and 0 or 1 is never 5; write count != 5
C Nothing
D It does not compile
Question page & discussion
Question 4 Easy

Students with 90 or more should get a distinction. What does the program print for 95 marks, and why?

main.c
#include <stdio.h>

int main(void) {
    int marks = 95;
    if (marks >= 40)
        puts("pass");
    else if (marks >= 90)
        puts("distinction");
    else
        puts("fail");
    return 0;
}
A distinction
B pass – every mark ≥ 90 is already ≥ 40, so the first branch wins and the distinction branch is unreachable
C fail
D pass and distinction
Question page & discussion
Question 5 Medium

45 of 60 students passed (75%). What does the program print, and why?

main.c
#include <stdio.h>

int main(void) {
    int passed = 45, total = 60;
    if (passed / total * 100 >= 50)
        puts("more than half passed");
    else
        puts("less than half passed");
    return 0;
}
A more than half passed
B less than half passed – 45 / 60 is 0 in integer division, so the percentage is 0
C It crashes
D It does not compile
Question page & discussion
Question 6 Hard

Compiled as standard C with -pedantic-errors, this program is rejected. Why?

main.c
#include <stdio.h>

int main(void) {
    int marks = 93;
    switch (marks) {
    case 90 ... 100: puts("A"); break;
    case 75 ... 89:  puts("B"); break;
    default:         puts("C or below");
    }
    return 0;
}
A switch cannot use int
B case 90 ... 100 is a GNU extension (proposed for a future standard), not standard C – use if-else for ranges
C default must come first
D There is no bug
Question page & discussion
Question 7 Medium

Access should be granted to admins or editors whose account is active. The admin's account is inactive. What does the program print, and why?

main.c
#include <stdbool.h>
#include <stdio.h>

int main(void) {
    bool is_admin = true, is_editor = false, is_active = false;
    if (is_admin || is_editor && is_active)
        puts("access granted");
    else
        puts("access denied");
    return 0;
}
A access denied
B access granted – && binds tighter, so the test is is_admin || (is_editor && is_active); brackets are needed: (is_admin || is_editor) && is_active
C Nothing
D It does not compile
Question page & discussion
Question 8 Medium

Recent compilers reject this program. What is wrong with the condition?

main.c
#include <stdio.h>

int main(void) {
    int low = 10, x = 50, high = 20;
    if (low < x < high)
        puts("in range");
    else
        puts("out of range");
    return 0;
}
A low and high must be constants
B low < x < high means (low < x) < high, comparing 0 or 1 with high – not a range test; write low < x && x < high
C The variables must be unsigned
D There is no bug; it prints out of range
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