Which statement correctly distinguishes scope and lifetime of a variable in C++?

Options

  • A. Scope tells where a variable can be accessed, while lifetime tells how long it exists
  • B. Scope and lifetime always mean exactly the same thing
  • C. Lifetime only applies to global variables
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

A. Scope tells where a variable can be accessed, while lifetime tells how long it exists

Detailed Explanation

Answer: (A) Scope tells where a variable can be accessed, while lifetime tells how long it exists

Scope describes the region of the program in which a name can be accessed.

Lifetime describes the period during program execution for which the object actually exists.

For example, a static local variable has local scope, so it can only be accessed from its function or block, but its lifetime normally continues until the program terminates.

This distinction is important in C++ because scope and lifetime are related concepts but are not the same thing.