C Quiz: malloc, calloc & free

This C quiz checks how you use malloc(), calloc(), realloc() and free() to work with memory on the heap. You will predict what small programs print and pick the safe way to allocate, grow and release a block.

मीडियम 10 सवाल ~6 मिनट पास होने के लिए 80% C17

मोड चुनें

आप गेस्ट के तौर पर खेल रहे हैं: XP और streak सेव रखने के लिए साइन इन करें।

सैंपल सवाल

सवाल 1 / 10

What does this print?

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *a = malloc(5 * sizeof *a);
    if (a == NULL) return 1;
    for (int i = 0; i < 5; i++) a[i] = i * i;
    int sum = 0;
    for (int i = 0; i < 5; i++) sum += a[i];
    printf("%d\n", sum);
    free(a);
    return 0;
}
जवाब दिखाएं

B. 30 The block holds the squares 0, 1, 4, 9 and 16, which add up to 30. sizeof *a is the size of one int, so 5 * sizeof *a is room for exactly five of them.

इस क्विज़ में क्या है

  • Allocating arrays with malloc(n * sizeof *p)
  • calloc() vs malloc(): zeroed memory and the two arguments
  • Growing a block safely with realloc()
  • Checking for NULL and what free(NULL) does
  • Returning heap memory from a function

अक्सर पूछे जाने वाले सवाल

क्या अकाउंट ज़रूरी है?

नहीं। कोई भी खेल सकता है। XP, streak, badge और हिस्ट्री सेव रखने के लिए साइन इन करें।

क्या मैं इसे दोबारा दे सकता हूं?

हां। आपका बेस्ट स्कोर सेव रहता है, और हर बार सवाल और जवाब शफ़ल होते हैं।