LearnFrenzy
Sign in Take a free test

Control Instructions · Exercise 2 of 4

Control Instructions 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 <stdio.h>

int main(void) {
    int x = 0;
    if (x = 5)
        printf("yes %d\n", x);
    else
        printf("no %d\n", x);
    return 0;
}
A yes 5
B no 0
C no 5
D yes 0
Question page & discussion
Question 2 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a = 5, b = -1;
    if (a > 0)
        if (b > 0)
            printf("A\n");
    else
        printf("B\n");
    printf("done\n");
    return 0;
}
A B done
B done
C A done
D A B done
Question page & discussion
Question 3 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int n = 2;
    switch (n) {
    case 1: printf("one ");
    case 2: printf("two ");
    case 3: printf("three ");
    default: printf("other");
    }
    printf("\n");
    return 0;
}
A two three other
B two
C two other
D one two three other
Question page & discussion
Question 4 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int n = 5;
    switch (n) {
    default: printf("D ");
    case 1:  printf("1 ");
             break;
    case 2:  printf("2 ");
    }
    printf("\n");
    return 0;
}
A D 1
B D
C D 1 2
D 1
Question page & discussion
Question 5 Hard

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int i = 0, j = 0, k = 0;
    if (i++ || j++)
        puts("first");
    if (++i || ++k)
        puts("second");
    printf("%d %d %d\n", i, j, k);
    return 0;
}
A second 2 1 0
B first second 2 1 1
C second 2 0 0
D second 2 1 1
Question page & discussion
Question 6 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    for (int x = 9; x <= 15; x++) {
        if (x % 15 == 0)
            printf("FizzBuzz ");
        else if (x % 3 == 0)
            printf("Fizz ");
        else if (x % 5 == 0)
            printf("Buzz ");
        else
            printf("%d ", x);
    }
    printf("\n");
    return 0;
}
A Fizz Buzz 11 Fizz 13 14 FizzBuzz
B Fizz Buzz 11 Fizz 13 14 Fizz
C 9 10 11 12 13 14 15
D Fizz Buzz 11 Fizz 13 14 Buzz
Question page & discussion
Question 7 Hard

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int flag = 1;
    printf("%zu\n", sizeof(flag ? 1 : 2.0));
    printf("%.1f\n", flag ? 1 : 2.0);
    return 0;
}
A 8 1.0
B 4 1
C 4 1.0
D 8 2.0
Question page & discussion
Question 8 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    const char *s = "Apple pie";
    int vowels = 0;
    for (const char *p = s; *p; p++) {
        switch (*p) {
        case 'a': case 'e': case 'i': case 'o': case 'u':
        case 'A': case 'E': case 'I': case 'O': case 'U':
            vowels++;
            break;
        default:
            break;
        }
    }
    printf("%d\n", vowels);
    return 0;
}
A 4
B 3
C 5
D 2
Question page & discussion
Question 9 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int i = 1;
again:
    printf("%d ", i);
    i *= 2;
    if (i < 20)
        goto again;
    printf("\n");
    return 0;
}
A 1 2 4 8 16
B 1 2 4 8 16 32
C 2 4 8 16
D 1 2 4 8
Question page & discussion
Question 10 Hard

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a = 7;
    if (a = 0, a + 1)
        printf("true %d\n", a);
    else
        printf("false %d\n", a);
    return 0;
}
A true 0
B false 0
C true 7
D false 7
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links