LearnFrenzy
Sign in Take a free test

Typedef · Exercise 2 of 4

Typedef 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>

typedef struct {
    char code;
    int qty;
} Item;

int main(void) {
    Item it = {'A', 5};
    printf("%zu %c %d\n", sizeof(Item), it.code, it.qty);
    return 0;
}
A 8 A 5
B 5 A 5
C 8 65 5
D 4 A 5
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>

#define STR char *
typedef char *Str;

int main(void) {
    STR a, b;
    Str c, d;
    printf("%zu %zu %zu %zu\n", sizeof a, sizeof b, sizeof c, sizeof d);
    return 0;
}
A 8 1 8 8
B 8 8 8 8
C 1 1 8 8
D 8 1 8 1
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>

typedef int Row[4];

int main(void) {
    Row grid[3];
    printf("%zu %zu %zu\n", sizeof(Row), sizeof grid, sizeof grid / sizeof grid[0]);
    return 0;
}
A 16 48 3
B 4 12 3
C 16 48 4
D 16 12 4
Question page & discussion
Question 4 Medium

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

main.c
#include <stdio.h>

typedef int (*Op)(int);

static int inc(int x) { return x + 1; }
static int dbl(int x) { return x * 2; }
static int sq(int x)  { return x * x; }

int main(void) {
    Op pipeline[] = {inc, dbl, sq};
    int v = 3;
    for (size_t i = 0; i < sizeof pipeline / sizeof pipeline[0]; i++)
        v = pipeline[i](v);
    printf("%d\n", v);
    return 0;
}
A 64
B 49
C 16
D 36
Question page & discussion
Question 5 Easy

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

main.c
#include <stdio.h>

typedef int Meters;
typedef int Seconds;

int main(void) {
    Meters distance = 100;
    Seconds time = 20;
    Meters wrong = time;
    printf("%d %d\n", distance / time, wrong);
    return 0;
}
A 5 20
B Compilation error
C 5 0
D 100 20
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>

typedef char *String;

int main(void) {
    char word[] = "abc";
    const String s = word;
    s[1] = 'B';
    printf("%s\n", s);
    return 0;
}
A aBc
B abc
C Compilation error: s is const
D Bbc
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>

typedef int Number;

int main(void) {
    printf("%zu ", sizeof(Number));
    {
        typedef double Number;
        printf("%zu ", sizeof(Number));
    }
    printf("%zu\n", sizeof(Number));
    return 0;
}
A 4 8 4
B 4 8 8
C 8 8 8
D Compilation error
Question page & discussion
Question 8 Easy

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

main.c
#include <stdio.h>

typedef struct point {
    int x, y;
} Point;

int main(void) {
    struct point a = {1, 2};
    Point b = a;
    b.x = 10;
    printf("%d %d\n", a.x, b.x);
    return 0;
}
A 1 10
B 10 10
C Compilation error
D 1 1
Question page & discussion
Question 9 Hard

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

main.c
#include <stdio.h>

typedef int Transform(int);

static int negate(int x) { return -x; }

int main(void) {
    Transform *t = negate;
    printf("%d %zu\n", t(7), sizeof(Transform *));
    return 0;
}
A -7 8
B -7 4
C 7 8
D Compilation error
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>

typedef unsigned char Byte;

int main(void) {
    Byte b = 250;
    b += 10;
    printf("%u %zu\n", b, sizeof(Byte));
    return 0;
}
A 4 1
B 260 1
C 255 1
D 4 4
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links