If a variable is created inside a function (a Local Variable), what happens to…

2026

If a variable is created inside a function (a Local Variable), what happens to that variable once the function finishes its execution and returns a value?

  1. A.

    It is stored in the global memory for the rest of the program’s life.

  2. B.

    It is permanently saved to the hard drive.

  3. C.

    It is “destroyed” or removed from memory and its value is no longer accessible.

  4. D.

    It is renamed automatically to avoid confusion with other variables.

Attempted by 144 students.

Show answer & explanation

Correct answer: C

When a function is called, local variables are allocated on the stack. Once the function finishes execution and returns, these variables go out of scope and are deallocated from memory. This means they cannot be accessed outside the function.

Explore the full course: Rssb Senior Computer Instructor