LearnFrenzy
Sign in Take a free test

Complicated Declarations · Exercise 2 of 4

Complicated Declarations 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 on a 64-bit system, so the answers are the real output.

Question 1 Easy

What is the output of the following C program on a 64-bit system?

main.c
#include <stdio.h>

int main(void) {
    int *a[4];
    int (*b)[4];
    printf("%zu %zu %zu\n", sizeof a, sizeof b, sizeof *b);
    return 0;
}
A 32 8 16
B 8 32 16
C 32 8 8
D 16 8 32
Question page & discussion
Question 2 Medium

What is the output of the following C program on a 64-bit system?

main.c
#include <stdio.h>

int main(void) {
    int m[3][2] = {{1, 2}, {3, 4}, {5, 6}};
    int (*p)[2] = m;
    p++;
    printf("%d %d %d\n", **p, (*p)[1], p[1][0]);
    return 0;
}
A 3 4 5
B 2 3 4
C 3 4 6
D 1 2 3
Question page & discussion
Question 3 Medium

What is the output of the following C program on a 64-bit system?

main.c
#include <stdio.h>

int main(void) {
    const char *langs[] = {"C", "Rust", "Go"};
    const char **pp = langs + 1;
    printf("%s %c %s %zu\n", *pp, **pp, pp[1], sizeof langs);
    return 0;
}
A Rust R Go 24
B C C Rust 24
C Rust R Go 9
D Rust u Go 24
Question page & discussion
Question 4 Easy

What is the output of the following C program on a 64-bit system?

main.c
#include <stdio.h>

int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }

int main(void) {
    int (*ops[])(int, int) = {add, sub, mul};
    int r = 10;
    for (int i = 0; i < 3; i++)
        r = ops[i](r, 3);
    printf("%d %zu\n", r, sizeof ops / sizeof ops[0]);
    return 0;
}
A 30 3
B 39 3
C 30 24
D 10 3
Question page & discussion
Question 5 Medium

What is the output of the following C program on a 64-bit system?

main.c
#include <stdio.h>

int half(int x) { return x / 2; }
int triple(int x) { return x * 3; }

int (*choose(int n))(int) {
    return n % 2 ? triple : half;
}

int main(void) {
    int n = 10;
    for (int i = 0; i < 3; i++)
        n = choose(n)(n);
    printf("%d\n", n);
    return 0;
}
A 45
B 15
C 24
D 7
Question page & discussion
Question 6 Hard

What is the output of the following C program on a 64-bit system?

main.c
#include <stdio.h>

int square(int x) { return x * x; }
int cube(int x) { return x * x * x; }

int main(void) {
    int (*fp)(int) = square;
    int (**pfp)(int) = &fp;
    printf("%d ", (*pfp)(4));
    *pfp = cube;
    printf("%d %d\n", fp(2), (**pfp)(3));
    return 0;
}
A 16 8 27
B 16 4 9
C 16 8 9
D 8 8 27
Question page & discussion
Question 7 Medium

What is the output of the following C program on a 64-bit system?

main.c
#include <stdio.h>

int main(void) {
    int a = 1, b = 2;
    const int *p = &a;
    int *const q = &b;
    p = q;
    *q += 5;
    printf("%d %d %d\n", *p, a, b);
    return 0;
}
A 7 1 7
B 1 1 7
C 7 6 7
D 2 1 7
Question page & discussion
Question 8 Hard

What is the output of the following C program on a 64-bit system?

main.c
#include <stdio.h>

int main(void) {
    int arr[5] = {10, 20, 30, 40, 50};
    int (*whole)[5] = &arr;
    int *last = (int *) (whole + 1) - 1;
    printf("%d %d %td\n", (*whole)[1], *last, (char *) (whole + 1) - (char *) arr);
    return 0;
}
A 20 50 20
B 20 10 4
C 20 50 4
D 10 50 20
Question page & discussion
Question 9 Medium

What is the output of the following C program on a 64-bit system?

main.c
#include <stdio.h>

typedef int triple[3];

int main(void) {
    triple t = {4, 5, 6};
    triple *p = &t;
    triple grid[2] = {{1, 2, 3}, {7, 8, 9}};
    printf("%zu %d %zu %d\n", sizeof t, (*p)[2], sizeof grid, grid[1][0]);
    return 0;
}
A 12 6 24 7
B 8 6 16 7
C 12 5 24 7
D 12 6 6 7
Question page & discussion
Question 10 Easy

What is the output of the following C program on a 64-bit system?

main.c
#include <stdio.h>

void each(const int *a, int n, void (*visit)(int)) {
    for (int i = 0; i < n; i++)
        visit(a[i]);
}

void show_even(int x) {
    if (x % 2 == 0)
        printf("%d ", x);
}

int main(void) {
    int v[] = {3, 8, 5, 12, 6};
    each(v, 5, show_even);
    printf("\n");
    return 0;
}
A 8 12 6
B 3 5
C 3 8 5 12 6
D 8 12
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links