LearnFrenzy
Sign in Take a free test

Decision Making · Exercise 2 of 4

Decision Making 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 a = 3, b = 0;
    printf("%d %d %d %d\n", a && b, a || b, !a || b, !(a && !b));
    return 0;
}
A 0 1 0 0
B 0 3 0 1
C 0 1 1 0
D 3 3 0 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 tests[] = {5, 15, 25};
    for (int i = 0; i < 3; i++) {
        int x = tests[i];
        printf("%d%d ", !(x >= 10 && x <= 20), x < 10 || x > 20);
    }
    printf("\n");
    return 0;
}
A 11 00 11
B 10 01 10
C 11 11 11
D 00 11 00
Question page & discussion
Question 3 Hard

What is the output of the following C program?

main.c
#include <stdio.h>

static int check(const char *name, int result) {
    printf("%s ", name);
    return result;
}

int main(void) {
    if (check("A", 0) && check("B", 1)) { }
    if (check("C", 1) || check("D", 1)) { }
    if (check("E", 1) && check("F", 0) || check("G", 1)) { }
    printf("\n");
    return 0;
}
A A C E F G
B A B C D E F G
C A C E F
D A B C E F G
Question page & discussion
Question 4 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int marks = 72, attendance = 68;
    if (marks >= 40 && attendance >= 75)
        puts("pass");
    else if (marks >= 40)
        puts("detained: attendance");
    else
        puts("fail");
    return 0;
}
A detained: attendance
B pass
C fail
D pass and detained: attendance
Question page & discussion
Question 5 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    const char *p = NULL;
    const char *q = "";
    printf("%d %d %d\n", p ? 1 : 0, q ? 1 : 0, q && *q);
    return 0;
}
A 0 1 0
B 0 0 0
C 0 1 1
D 1 1 0
Question page & discussion
Question 6 Hard

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a = 4, b = 0, c = 2;
    printf("%d %d %d\n", !a != !b, !a != !c, a ^ c);
    return 0;
}
A 1 0 6
B 1 1 6
C 0 0 0
D 1 0 0
Question page & discussion
Question 7 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    double d = 0.0001;
    if (d)
        puts("double is true");
    int n = (int) d;
    if (!n)
        puts("int is false");
    return 0;
}
A double is true int is false
B double is true
C int is false
D Nothing
Question page & discussion
Question 8 Medium

What is the output of the following C program?

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

int main(void) {
    double a = 0.1 * 3, b = 0.3;
    printf("%s, %s\n", a == b ? "equal" : "not equal", fabs(a - b) < 1e-9 ? "close" : "far");
    return 0;
}
A not equal, close
B equal, close
C not equal, far
D equal, far
Question page & discussion
Question 9 Hard

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a = 10, b = -2;
    int mask = -(a < b);                 /* all ones if a < b, else 0 */
    int mn = b ^ ((a ^ b) & mask);
    int mx = a ^ ((a ^ b) & mask);
    printf("%d %d %d\n", mask, mn, mx);
    return 0;
}
A 0 -2 10
B -1 10 -2
C 0 10 -2
D 1 -2 10
Question page & discussion
Question 10 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int years[] = {1900, 2000, 2023, 2024};
    for (int i = 0; i < 4; i++) {
        int y = years[i];
        putchar(y % 400 == 0 ? 'L' : y % 100 == 0 ? 'N' : y % 4 == 0 ? 'L' : 'N');
    }
    putchar('\n');
    return 0;
}
A NLNL
B LLNL
C NLLL
D LNNL
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links