200-710 · Question #25
What purpose do namespaces fulfill?
The correct answer is A. Encapsulation. Namespaces primarily serve encapsulation (A) by grouping related identifiers (classes, functions, variables) under a named scope, preventing naming collisions between different parts of a program or between libraries. If two libraries both define a Logger class, namespaces keep…
Question
Options
- AEncapsulation
- BAlternative to classes
- CImproved performance
- DAll of the above
How the community answered
(45 responses)- A76% (34)
- B7% (3)
- C4% (2)
- D13% (6)
Explanation
Namespaces primarily serve encapsulation (A) by grouping related identifiers (classes, functions, variables) under a named scope, preventing naming collisions between different parts of a program or between libraries. If two libraries both define a Logger class, namespaces keep them distinct - LibA::Logger vs. LibB::Logger.
Why the distractors are wrong:
- B (Alternative to classes): Namespaces and classes solve different problems. Classes define types with state and behavior; namespaces are purely organizational scoping mechanisms with no instantiation or inheritance.
- C (Improved performance): Namespaces are a compile-time/organizational concept - they have no runtime overhead and do not affect execution speed.
- D (All of the above): Since B and C are incorrect, D cannot be right.
Memory tip: Think of namespaces like a filing cabinet with labeled drawers - each drawer (namespace) keeps its contents enclosed and separated from the others, which is exactly what encapsulation means: containing things within a defined boundary.
Topics
Community Discussion
No community discussion yet for this question.