LearnFrenzy
Sign in Take a free test

C Library Functions · Exercise 2 of 4

C Library Functions Predict the Output

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

Read each program and choose what it prints. Every program was compiled and run, so the answers are the real output.

Question 1 Easy

What is the output of the following C program?

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

int main(void) {
    printf("%.0f %.0f %.1f\n", pow(3, 4), sqrt(169), fabs(-0.5));
    printf("%.1f %.1f\n", ceil(-2.5), floor(-2.5));
    return 0;
}
A 81 13 0.5 -2.0 -3.0
B 81 13 0.5 -3.0 -2.0
C 12 13 0.5 -2.0 -3.0
D 81 13 -0.5 -2.0 -3.0
Question page & discussion
Question 2 Medium

What is the output of the following C program?

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

int main(void) {
    div_t a = div(17, 5), b = div(-17, 5);
    printf("%d %d %d %d\n", a.quot, a.rem, b.quot, b.rem);
    return 0;
}
A 3 2 -3 -2
B 3 2 -4 3
C 3 2 -3 2
D 3 2 -4 -2
Question page & discussion
Question 3 Easy

What is the output of the following C program?

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

int main(void) {
    printf("%d %d %d %d\n", strcmp("apple", "apricot") < 0, strcmp("b", "a") > 0,
           strcmp("Zoo", "apple") < 0, strcmp("same", "same"));
    return 0;
}
A 1 1 1 0
B 1 1 0 0
C 0 1 1 0
D 1 1 1 1
Question page & discussion
Question 4 Medium

What is the output of the following C program?

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

int main(void) {
    const char *s = "learnfrenzy";
    printf("%td %td %td %d\n", strchr(s, 'n') - s, strrchr(s, 'n') - s, strstr(s, "fren") - s, strchr(s, 'q') == NULL);
    return 0;
}
A 4 8 5 1
B 5 9 6 1
C 4 8 5 0
D 4 4 5 1
Question page & discussion
Question 5 Hard

What is the output of the following C program?

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

int main(void) {
    int a[3];
    memset(a, 1, sizeof a);
    printf("%d\n", a[0]);
    memset(a, 0, sizeof a);
    printf("%d\n", a[2]);
    return 0;
}
A 16843009 0
B 1 0
C 111 0
D 16843009 16843009
Question page & discussion
Question 6 Medium

What is the output of the following C program?

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

int main(void) {
    char csv[] = "red,green,,blue";
    int n = 0;
    for (char *t = strtok(csv, ","); t != NULL; t = strtok(NULL, ",")) {
        printf("[%s]", t);
        n++;
    }
    printf(" %d\n", n);
    return 0;
}
A [red][green][blue] 3
B [red][green][][blue] 4
C [red,green,,blue] 1
D [red][green] 2
Question page & discussion
Question 7 Medium

What is the output of the following C program?

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

int main(void) {
    char s[] = "abcdef";
    memmove(s + 2, s, 4);
    printf("%s\n", s);
    return 0;
}
A ababcd
B ababab
C abcdab
D cdefef
Question page & discussion
Question 8 Easy

What is the output of the following C program?

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

int main(void) {
    const char *s = "c23 rocks!";
    int digits = 0;
    for (int i = 0; s[i]; i++) {
        if (isdigit((unsigned char) s[i]))
            digits++;
        putchar(toupper((unsigned char) s[i]));
    }
    printf(" %d\n", digits);
    return 0;
}
A C23 ROCKS! 2
B C23 ROCKS! 0
C c23 rocks! 2
D C ROCKS! 2
Question page & discussion
Question 9 Medium

What is the output of the following C program?

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

int desc(const void *a, const void *b) {
    int x = *(const int *) a, y = *(const int *) b;
    return (y > x) - (y < x);
}

int main(void) {
    int v[] = {5, -2, 9, 0, 9, 3};
    qsort(v, 6, sizeof v[0], desc);
    for (int i = 0; i < 6; i++)
        printf("%d ", v[i]);
    printf("\n");
    return 0;
}
A 9 9 5 3 0 -2
B -2 0 3 5 9 9
C 9 5 3 0 -2
D 5 -2 9 0 9 3
Question page & discussion
Question 10 Medium

What is the output of the following C program?

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

int cmp(const void *a, const void *b) {
    int x = *(const int *) a, y = *(const int *) b;
    return (x > y) - (x < y);
}

int main(void) {
    int sorted[] = {2, 4, 8, 16, 32, 64};
    int k1 = 16, k2 = 10;
    int *p = bsearch(&k1, sorted, 6, sizeof(int), cmp);
    int *q = bsearch(&k2, sorted, 6, sizeof(int), cmp);
    printf("%td %d\n", p - sorted, q == NULL);
    return 0;
}
A 3 1
B 4 1
C 3 0
D 16 1
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links