Multiple choice technology programming languages

In the following code variable test1 would be allocated memory on?int func(){int test1 = 10;return test1;}void main(){ int i = 0; i = func();}

  1. Heap

  2. Stack

  3. Code Segment

  4. Data Segment

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The variable test1 is a local non-static variable declared inside the func() function, so it is allocated on the stack. Local variables (automatic storage duration) are created on the stack when the function is called and destroyed when the function returns. Heap is for dynamic allocation (malloc), code segment for instructions, and data segment for static/global variables.

AI explanation

Local variables declared inside a function, like test1 inside func(), are allocated on the call stack for the duration of that function's execution frame. Since test1 is a simple local int and not dynamically allocated (no malloc/new), it lives on the stack rather than the heap or a fixed data/code segment.