← 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;
}
- A.Garbage value
- B.7
- C.28
- 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 →