LearnFrenzy
Sign in Take a free test

C Library Functions · Exercise 3 of 4

C Library Functions Find the Error

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

Each program misuses a library function: some are rejected by the compiler, others corrupt memory or give wrong results. 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) {
    double side = 49;
    printf("%.1f\n", sqrt(side));
    return 0;
}
A side must be an int
B sqrt is used without its declaration – <math.h> is not included
C %.1f cannot print a double
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>
#include <string.h>

int main(void) {
    char city[6];
    strcpy(city, "Bengaluru");
    printf("%s\n", city);
    return 0;
}
A strcpy needs a length
B "Bengaluru" needs 10 bytes but city has 6, so strcpy writes past the array – a buffer overflow
C city must be a pointer
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>
#include <stdlib.h>

int cmp(int *a, int *b) {
    return *a - *b;
}

int main(void) {
    int v[] = {3, 1, 2};
    qsort(v, 3, sizeof v[0], cmp);
    printf("%d %d %d\n", v[0], v[1], v[2]);
    return 0;
}
A qsort cannot sort ints
B The comparator must have the type int (*)(const void *, const void *); a function taking int * is an incompatible pointer type
C sizeof v[0] should be sizeof v
D There is no bug
Question page & discussion
Question 4 Medium

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

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

int main(void) {
    char code[3] = {'A', 'B', 'C'};
    printf("%zu\n", strlen(code));
    return 0;
}
A strlen returns int
B The array has no terminating '\0', so strlen reads past its end looking for one
C char arrays cannot be initialised with braces
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>
#include <string.h>

int main(void) {
    char text[] = "hello world";
    memcpy(text + 1, text, 5);      /* shift right by one */
    printf("%s\n", text);
    return 0;
}
A memcpy needs strlen
B Source and destination overlap, which is undefined behaviour for memcpy – memmove must be used
C text is read-only
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 <ctype.h>
#include <stdio.h>

int main(void) {
    char word[] = "hello";
    printf("%c\n", toupper(word));
    return 0;
}
A toupper needs stdlib.h
B toupper converts one character (an int); passing the array (a char pointer) is invalid – loop over the characters
C %c should be %s
D There is no bug
Question page & discussion
Question 7 Easy

This program prints "access denied" although the input is "admin". Why?

main.c
#include <stdio.h>

int main(void) {
    char role[16] = "admin";
    if (role == "admin")
        puts("welcome");
    else
        puts("access denied");
    return 0;
}
A role is too long
B == compares the addresses of the array and the literal, not their characters; use strcmp(role, "admin") == 0
C puts needs a newline
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>
#include <string.h>

int main(void) {
    char tag[4];
    strncpy(tag, "urgent", sizeof tag);
    printf("[%s]\n", tag);
    return 0;
}
A sizeof tag is 8
B The source is longer than 4, so strncpy copies "urge" without a '\0' and printf reads past the array
C strncpy cannot copy literals
D There is no bug
Question page & discussion
Up next · Exercise 4Library Function Programs8 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links