LearnFrenzy
Sign in Take a free test

Structures, Unions, Enums · Exercise 2 of 4

Structures, Unions, Enums 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>

struct point {
    int x, y, z;
};

int main(void) {
    struct point p = {.z = 3, .x = 1};
    printf("%d %d %d\n", p.x, p.y, p.z);
    return 0;
}
A 1 0 3
B 3 1 0
C 1 garbage 3
D Compilation error
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>

enum { A, B = 5, C, D = B + 10, E };

int main(void) {
    printf("%d %d %d %d %d\n", A, B, C, D, E);
    return 0;
}
A 0 5 6 15 16
B 0 5 6 15 7
C 1 5 6 15 16
D 0 5 2 15 4
Question page & discussion
Question 3 Hard

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

main.c
#include <stdio.h>

struct a { char c; double d; };
struct b { char c1; char c2; int i; };
struct c { char c[5]; };

int main(void) {
    printf("%zu %zu %zu\n", sizeof(struct a), sizeof(struct b), sizeof(struct c));
    return 0;
}
A 16 8 5
B 9 6 5
C 16 8 8
D 16 12 5
Question page & discussion
Question 4 Hard

What is the output on a little-endian 64-bit machine (x86-64 or ARM64)?

main.c
#include <stdio.h>

union u {
    int i;
    unsigned char bytes[4];
};

int main(void) {
    union u x = {.i = 0x01020304};
    printf("%d %d\n", x.bytes[0], sizeof x == 4);
    return 0;
}
A 4 1
B 1 1
C 4 0
D 3 1
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>

struct box {
    int *p;
    int v;
};

int main(void) {
    int shared = 1;
    struct box a = {&shared, 1};
    struct box b = a;
    *b.p = 50;
    b.v = 99;
    printf("%d %d %d\n", *a.p, a.v, b.v);
    return 0;
}
A 50 1 99
B 1 1 99
C 50 99 99
D 1 99 99
Question page & discussion
Question 6 Medium

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

main.c
#include <stdio.h>

struct item {
    const char *name;
    int qty;
};

int main(void) {
    struct item list[] = {{"pen", 3}, {"book", 1}, {"bag", 2}};
    struct item *p = list;
    p++;
    printf("%s %d %s\n", p->name, (p + 1)->qty, list[0].name + 1);
    return 0;
}
A book 2 en
B pen 1 en
C book 1 pen
D book 2 pen
Question page & discussion
Question 7 Easy

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

main.c
#include <stdio.h>

struct date {
    int d, m, y;
};

struct student {
    const char *name;
    struct date dob;
};

int main(void) {
    struct student s = {"Asha", {15, 8, 2004}};
    struct student *ps = &s;
    printf("%s %02d/%02d/%d\n", ps->name, ps->dob.d, ps->dob.m, s.dob.y);
    return 0;
}
A Asha 15/08/2004
B Asha 15/8/2004
C Asha 08/15/2004
D Compilation error
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>

struct flags {
    unsigned mode : 2;
};

int main(void) {
    struct flags f = {0};
    f.mode = 5;
    printf("%u\n", f.mode);
    return 0;
}
A 5
B 1
C 3
D 0
Question page & discussion
Question 9 Easy

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

main.c
#include <stdio.h>

union num {
    char c;
    int i;
    double d;
};

enum flag { OFF, ON };

int main(void) {
    printf("%zu %zu\n", sizeof(union num), sizeof(enum flag));
    return 0;
}
A 8 4
B 13 4
C 8 1
D 13 1
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>

struct counter {
    int n;
};

void inc_copy(struct counter c) { c.n++; }
void inc_ptr(struct counter *c) { c->n++; }

int main(void) {
    struct counter c = {0};
    inc_copy(c);
    inc_ptr(&c);
    inc_ptr(&c);
    printf("%d\n", c.n);
    return 0;
}
A 2
B 3
C 1
D 0
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links