LearnFrenzy
Sign in Take a free test

C Input / Output · Exercise 2 of 4

C Input / Output 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 (with the input shown, where it reads any), so the answers are the real output.

Question 1 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    printf("%6.2f|%-4d|%04d|%+d\n", 3.14159, 7, 42, 5);
    return 0;
}
A 3.14|7 |0042|+5
B 3.14 | 7|0042|+5
C 3.14|7 |4200|5
D 3.14|7|42|+5
Question page & discussion
Question 2 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int n = printf("C23\n");
    printf("%d\n", printf("Hi "));
    printf("%d\n", n);
    return 0;
}
A C23 Hi 3 4
B C23 Hi 2 3
C C23 3Hi 4
D C23 Hi 3 3
Question page & discussion
Question 3 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    printf("[%*d][%-*d][%.*f]\n", 5, 42, 4, 7, 2, 2.71828);
    return 0;
}
A [ 42][7 ][2.72]
B [42 ][ 7][2.72]
C [5 42][4 7][2 2.71828]
D [ 42][7 ][2.71]
Question page & discussion
Question 4 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    const char *s = "LearnFrenzy";
    printf("[%.3s][%8.3s][%-8.5s][%2s]\n", s, s, s, "C");
    return 0;
}
A [Lea][ Lea][Learn ][ C]
B [Lea][Lea ][ Learn][C ]
C [LearnFrenzy][ Lea][Learn ][ C]
D [Lea][ Lea][Learn ][C]
Question page & discussion
Question 5 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    printf("%c %d %x %#o %#X\n", 66, 'a', 255, 8, 255);
    return 0;
}
A B 97 ff 010 0XFF
B 66 a ff 10 FF
C B 97 FF 010 0xff
D B a ff 8 0XFF
Question page & discussion
Question 6 Medium

What is the output of the following C program when the input is 5⏎x⏎?

main.c
#include <stdio.h>

int main(void) {
    int n;
    char c;
    scanf("%d", &n);
    scanf("%c", &c);
    printf("%d [%d]\n", n, c);
    return 0;
}
A 5 [10]
B 5 [120]
C 5 [0]
D 5 [32]
Question page & discussion
Question 7 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a = -1, b = -1;
    int r = sscanf("12 apples", "%d %d", &a, &b);
    printf("%d %d %d\n", r, a, b);
    return 0;
}
A 1 12 -1
B 2 12 0
C 0 -1 -1
D 1 12 0
Question page & discussion
Question 8 Easy

What is the output of the following C program when the input is hi⏎?

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

int main(void) {
    char buf[10];
    fgets(buf, sizeof buf, stdin);
    printf("%zu [%s]\n", strlen(buf), buf);
    return 0;
}
A 3 [hi ]
B 2 [hi]
C 3 [hi]
D 2 [hi ]
Question page & discussion
Question 9 Hard

What is the output of the following C program when the input is 20261111abcdef⏎?

main.c
#include <stdio.h>

int main(void) {
    int a, b;
    char s[4];
    scanf("%2d%3d%3s", &a, &b, s);
    printf("%d %d %s\n", a, b, s);
    return 0;
}
A 20 261 111
B 2026 1111 abc
C 20 261 abc
D 20 26 111
Question page & discussion
Question 10 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    FILE *fp = tmpfile();
    if (fp == NULL)
        return 1;
    fputs("Learn by doing", fp);
    fseek(fp, -5L, SEEK_END);
    char word[8];
    fscanf(fp, "%7s", word);
    printf("%s %ld\n", word, ftell(fp));
    fclose(fp);
    return 0;
}
A doing 14
B doing 9
C by 8
D Learn 5
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links