LearnFrenzy
Sign in Take a free test

Arrays · Exercise 2 of 4

Arrays 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. Assume a 64-bit system (int 4 bytes, pointers 8 bytes).

Question 1 Hard

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a[] = {[2] = 30, [0] = 10, 20};
    printf("%zu %d %d %d\n", sizeof a / sizeof a[0], a[0], a[1], a[2]);
    return 0;
}
A 3 10 20 30
B 4 10 20 30
C 3 10 0 30
D 2 10 20 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, 6, 7, 8};
    printf("%d %d %d\n", 2[a], *(a + 3), *a + 3);
    return 0;
}
A 7 8 8
B 7 8 6
C 6 7 8
D Compilation error
Question page & discussion
Question 3 Hard

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a[6];
    printf("%td %td\n", (&a + 1) - &a, (char *) (&a + 1) - (char *) a);
    return 0;
}
A 6 6
B 1 24
C 1 6
D 24 24
Question page & discussion
Question 4 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

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

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int m[2][3] = {{1}, {4, 5}};
    for (int r = 0; r < 2; r++)
        for (int c = 0; c < 3; c++)
            printf("%d ", m[r][c]);
    printf("\n");
    return 0;
}
A 1 4 5 0 0 0
B 1 0 0 4 5 0
C 1 1 1 4 5 5
D Garbage values
Question page & discussion
Question 6 Hard

What is the output of the following C program?

main.c
#include <stdio.h>

struct box {
    int v[3];
};

int main(void) {
    struct box a = {{1, 2, 3}}, b;
    b = a;              /* struct assignment */
    b.v[0] = 99;
    printf("%d %d\n", a.v[0], b.v[0]);
    return 0;
}
A 99 99
B 1 99
C 1 1
D Compilation error
Question page & discussion
Question 7 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int *p = (int[]){10, 20, 30};
    p[1] += 5;
    printf("%d %zu\n", p[1], sizeof (int[]){10, 20, 30} / sizeof(int));
    return 0;
}
A 25 3
B 20 3
C 25 2
D Compilation error
Question page & discussion
Question 8 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int m[4][5];
    printf("%zu %zu %zu\n", sizeof m, sizeof m[0], sizeof m[0][0]);
    return 0;
}
A 80 20 4
B 20 5 4
C 80 8 4
D 80 20 8
Question page & discussion
Question 9 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a[] = {1, 2, 3, 4, 5};
    int *p = a + 5;
    while (p > a)
        printf("%d", *--p);
    printf("\n");
    return 0;
}
A 54321
B 4321
C 5432
D Undefined behaviour
Question page & discussion
Question 10 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int n = 7;
    int v[n];
    n = 100;            /* does this change v? */
    printf("%zu\n", sizeof v);
    return 0;
}
A 28
B 400
C 8
D Compilation error
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links