🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Dynamic Memory: all 10 questions

Quiz: Dynamic Memory · Question 10 of 10

What does this print? (code)

What does this print?

#include <stdio.h>
#include <stdlib.h>
int main(void) {
    int *a = calloc(4, sizeof(int));
    a[1] = 7;
    int s = 0;
    for (int i = 0; i < 4; i++) s += a[i];
    printf("%d\n", s);
    free(a);
    return 0;
}
  1. A.Garbage value
  2. B.7
  3. C.28
  4. D.0
Answer & explanation

Correct answer: 7

calloc sets all four ints to 0, so the only non-zero value is the 7 stored in a[1].

Can you get the rest right without peeking?

Take the timed Quiz: Dynamic Memory quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →