IAE Academy

GATE 2026 CS2 Q.17: solution and answer

GATE 2026 · CS2Q.17 · 1 mark · MCQ

In C runtime environment, which one of the following is stored in heap?

  1. (A)

    A static variable declared inside a function

  2. (B)

    An array of integers declared inside a function

  3. (C)

    A dynamically allocated array of integers created using malloc() function call

  4. (D)

    Return address of a function

See the question in the paper

Answer and solution

ANSWER

The correct answer is (C) — a dynamically allocated array of integers created using malloc() is stored in the heap.

Official answer: CSource: Official answer key, GATE 2026 CS2 (IIT Guwahati)

  1. A static local variable is stored in the data (or BSS) segment, not the heap — it retains its value between calls because it has static storage duration, not because it resides on the heap.

  2. A plain array of integers declared inside a function (without static) is a local/automatic variable, allocated in the function's stack frame; it is destroyed when the function returns.

  3. A dynamically allocated array created using malloc() explicitly requests memory from the heap at runtime; this memory persists until explicitly freed with free() — this is the defining example of heap storage.

  4. The return address of a function is pushed as part of the function call's activation record on the stack, used to resume execution after the call returns — it is stack-resident, not heap-resident.

  5. Only option (C) describes memory obtained via dynamic allocation, so it is the one stored in the heap.

KEY IDEA

A C program's runtime memory layout has four regions: code/text, static/global (data+BSS) segment, stack (activation records: locals, parameters, return addresses), and heap (memory obtained via malloc/calloc/realloc, released only by free or process exit).

WATCH OUT FOR

Assuming any "array" or anything that "lives beyond a single line" must be on the heap, or confusing static storage duration with heap allocation — a static local variable is in the data/BSS segment, not the heap, even though it also outlives the function call.

From the GATE 2026 CS2 paper. Every question of this paper · Download the full paper · All 7 Functions & scope questions, with notes

More Functions & scope questions