LearnFrenzy
Sign in Take a free test

Strings · Exercise 3 of 4

Strings Find the Error

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

Each program contains a string bug – overflows, missing terminators, literals that cannot be changed, and inputs that are read wrongly. Choose the option that names the real problem.

Question 1 Easy

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

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

int main(void) {
    char path[8] = "/usr";
    strcat(path, "/local");
    printf("%s\n", path);
    return 0;
}
A strcat() cannot append a string that starts with '/'
B path needs 11 bytes ("/usr/local" + '\0') but has 8, so strcat() writes past the end
C path must be a pointer for strcat()
D There is no error; it prints /usr/local
Question page & discussion
Question 2 Easy

The user typed "yes". What does this program print, and why?

main.c
#include <stdio.h>

int main(void) {
    char answer[8] = "yes";
    if (answer == "yes")
        puts("confirmed");
    else
        puts("cancelled");
    return 0;
}
A confirmed – the strings are equal
B cancelled – == compares the array's address with the literal's address, which differ
C It does not compile
D Nothing – the program crashes
Question page & discussion
Question 3 Medium

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

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

int main(void) {
    char *csv = "red,green,blue";
    for (char *t = strtok(csv, ","); t != NULL; t = strtok(NULL, ","))
        puts(t);
    return 0;
}
A The delimiter must be a char, not a string
B strtok() writes '\0' into csv, but csv points to a read-only string literal
C strtok(NULL, ",") is invalid
D There is no error; it prints the three colours
Question page & discussion
Question 4 Easy

The user types Asha Rao and presses Enter. What does the program print, and why?

main.c
#include <stdio.h>

int main(void) {
    char name[32];
    if (scanf("%31s", name) != 1)
        return 1;
    printf("Hello, %s!\n", name);
    return 0;
}
A Hello, Asha Rao!
B Hello, Asha! – %s stops reading at the first whitespace
C Hello, ! – %s skips letters
D It overflows name
Question page & discussion
Question 5 Medium

The input is programming. What does this program print, and why?

main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    char *buf = malloc(64);
    if (buf == NULL)
        return 1;
    if (fgets(buf, sizeof buf, stdin) == NULL)
        return 1;
    buf[strcspn(buf, "\n")] = '\0';
    printf("%s\n", buf);
    free(buf);
    return 0;
}
A programming
B program – sizeof buf is the pointer size (8), so fgets() reads at most 7 characters
C progra – fgets() reads sizeof buf - 2 characters
D It does not compile
Question page & discussion
Question 6 Medium

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

main.c
#include <stdio.h>

int main(void) {
    char src[] = "code";
    char dst[10];
    for (int i = 0; i < 4; i++)
        dst[i] = src[i];
    printf("%s\n", dst);
    return 0;
}
A dst is too small
B The loop copies only the 4 letters; dst never gets a '\0', so printf() reads past them
C src[i] should be *src++
D There is no error; it prints code
Question page & discussion
Question 7 Hard

This program counts input characters and works for the input abc. What is still wrong with it?

main.c
#include <stdio.h>

int main(void) {
    char c;
    int n = 0;
    while ((c = getchar()) != EOF)
        n++;
    printf("%d\n", n);
    return 0;
}
A c must be an int: getchar() returns int so EOF can be told apart from a real 0xFF byte
B getchar() needs stdin as an argument
C n must be a long
D Nothing is wrong
Question page & discussion
Question 8 Easy

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

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

int main(void) {
    char s[5];
    strcpy(s, "Hello");
    printf("%s\n", s);
    return 0;
}
A strcpy() cannot copy a literal
B "Hello" needs 6 bytes with its '\0', but s has only 5
C s must be initialised first
D There is no error; it prints Hello
Question page & discussion
Up next · Exercise 4String Programs8 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Popular