LearnFrenzy
Sign in Take a free test

Memory Allocation · Exercise 2 of 4

Memory Allocation 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 with AddressSanitizer 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>
#include <stdlib.h>

int main(void) {
    int *p = malloc(10 * sizeof *p);
    if (p == NULL)
        return 1;
    printf("%zu %zu\n", sizeof p, sizeof *p);
    free(p);
    return 0;
}
A 8 4
B 40 4
C 40 40
D 8 40
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>
#include <stdlib.h>

int main(void) {
    int *a = calloc(3, sizeof *a);
    if (a == NULL)
        return 1;
    a[0] = 7;
    int *b = realloc(a, 5 * sizeof *a);
    if (b == NULL) {
        free(a);
        return 1;
    }
    b[4] = 9;
    printf("%d %d %d\n", b[0], b[2], b[4]);
    free(b);
    return 0;
}
A 7 0 9
B 7 garbage 9
C 0 0 9
D 7 0 0
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>
#include <string.h>

int main(void) {
    char s[] = "123456789";
    memmove(s, s + 3, 4);
    puts(s);
    return 0;
}
A 456756789
B 4567
C 123412349
D 456789
Question page & discussion
Question 4 Hard

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

main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    int *a = malloc(4 * sizeof *a);
    if (a == NULL)
        return 1;
    memset(a, 0, 4 * sizeof *a);
    a[1] = -1;
    memset(a + 2, 0xFF, sizeof *a);
    printf("%d %d %d %d\n", a[0], a[1], a[2], a[3]);
    free(a);
    return 0;
}
A 0 -1 -1 0
B 0 -1 255 0
C 0 -1 -1 -1
D 0 -1 0xFF 0
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>
#include <stdlib.h>

int make(int **out, int n) {
    *out = malloc(n * sizeof **out);
    if (*out == NULL)
        return 0;
    for (int i = 0; i < n; i++)
        (*out)[i] = i + 1;
    return 1;
}

int main(void) {
    int *v = NULL;
    if (make(&v, 3))
        printf("%d %d %d\n", v[0], v[1], v[2]);
    free(v);
    return 0;
}
A 1 2 3
B 0 1 2
C garbage
D Compilation error
Question page & discussion
Question 6 Easy

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

main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    const char *src = "heap";
    char *copy = malloc(strlen(src) + 1);
    if (copy == NULL)
        return 1;
    strcpy(copy, src);
    copy[0] = 'H';
    printf("%s %s %zu\n", src, copy, strlen(copy) + 1);
    free(copy);
    return 0;
}
A heap Heap 5
B Heap Heap 5
C heap Heap 4
D heap heap 5
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>
#include <stdlib.h>

int main(void) {
    int *p = malloc(sizeof *p);
    free(p);
    p = NULL;
    free(p);
    printf("%s\n", p == NULL ? "safe" : "dangling");
    return 0;
}
A safe
B dangling
C The program crashes with a double free
D Compilation error
Question page & discussion
Question 8 Medium

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

main.c
#include <stdio.h>

int *counter(void) {
    static int c = 0;
    c++;
    return &c;
}

int main(void) {
    int *a = counter();
    int *b = counter();
    printf("%d %d %d\n", *a, *b, a == b);
    return 0;
}
A 2 2 1
B 1 2 0
C 1 2 1
D 2 2 0
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>
#include <stdlib.h>
#include <string.h>

int main(void) {
    char *s = malloc(16);
    if (s == NULL)
        return 1;
    strcpy(s, "shrinking");
    char *t = realloc(s, 6);
    if (t == NULL) {
        free(s);
        return 1;
    }
    t[5] = '\0';
    puts(t);
    free(t);
    return 0;
}
A shrin
B shrink
C shrinking
D garbage
Question page & discussion
Question 10 Medium

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

main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    const char *words[] = {"go", "c", "java"};
    char **copies = malloc(3 * sizeof *copies);
    if (copies == NULL)
        return 1;
    size_t total = 0;
    for (int i = 0; i < 3; i++) {
        size_t n = strlen(words[i]) + 1;
        copies[i] = malloc(n);
        if (copies[i] == NULL)
            return 1;
        memcpy(copies[i], words[i], n);
        total += n;
    }
    printf("%s %zu\n", copies[2], total);
    for (int i = 0; i < 3; i++)
        free(copies[i]);
    free(copies);
    return 0;
}
A java 10
B java 7
C java 24
D go 10
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Popular