LearnFrenzy
Sign in Take a free test

C Input / Output · Exercise 3 of 4

C Input / Output Find the Error

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

Each program contains an input/output mistake: some are rejected by the compiler, others crash or corrupt memory when run. 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) {
    int n = 0;
    scanf("%d", n);
    printf("%d\n", n);
    return 0;
}
A %d should be %i
B scanf receives the value of n (0) instead of its address, so it writes the number to an invalid address and the program crashes
C n must be initialised to 1
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>

int main(void) {
    FILE *fp = fopen("no-such-folder/marks.txt", "r");
    int ch = fgetc(fp);
    printf("%c\n", ch);
    fclose(fp);
    return 0;
}
A The mode should be "rb"
B fopen returns NULL because the file does not exist, and the program passes NULL to fgetc and fclose
C fgetc returns char, not int
D There is no bug
Question page & discussion
Question 3 Medium

The program below is given the input Alexander. Which statement correctly describes its bug?

main.c
#include <stdio.h>

int main(void) {
    char name[5];
    scanf("%s", name);
    printf("Hello, %s\n", name);
    return 0;
}
A name needs an &
B %s has no width, so the 9 letters plus '\0' are written past the end of the 5-byte array – a buffer overflow
C printf cannot print char arrays
D There is no bug
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) {
    FILE fp = fopen("data.txt", "w");
    fprintf(&fp, "hello\n");
    fclose(&fp);
    return 0;
}
A "w" must be "wt"
B fopen returns a FILE *, which cannot initialise a FILE object; fp must be declared FILE *fp
C fprintf needs a format specifier
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 code = 404;
    fprintf("error %d\n", code);
    return 0;
}
A %d must be %i
B fprintf's first argument must be a FILE * stream, e.g. fprintf(stderr, "error %d\n", code)
C code must be a string
D There is no bug
Question page & discussion
Question 6 Easy

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

main.c
#include <stdio.h>

int main(void) {
    putchar("A");
    putchar('\n');
    return 0;
}
A putchar needs stdout as an argument
B "A" is a string (a char pointer); putchar takes one character, so it should be putchar('A')
C putchar returns void
D There is no bug
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) {
    char line[50];
    if (fgets(line, stdin) != NULL)
        fputs(line, stdout);
    return 0;
}
A fputs must be puts
B fgets takes three arguments – buffer, size and stream – so the size is missing: fgets(line, sizeof line, stdin)
C line must be a pointer
D There is no bug
Question page & discussion
Question 8 Medium

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

main.c
#include <stdio.h>

int main(void) {
    char *msg = "Score: ---";
    sprintf(msg, "Score: %d", 87);
    puts(msg);
    return 0;
}
A %d must be %s
B msg points to a string literal, which is read-only; writing into it crashes – use an array such as char msg[32]
C sprintf returns void
D There is no bug
Question page & discussion
Up next · Exercise 4Input/Output Programs8 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links