What is the main purpose of a namespace in C++?

  • ATo store variables
  • BTo group identifiers and avoid name conflicts
  • CTo create new functions
  • DTo allocate memory
Show answerHide answer

Correct answer B

A namespace in C++ is used to organize code into logical groups. It helps avoid name conflicts when different parts of code use the same names. For example, two libraries can have functions with the same name but in different namespaces. The std namespace (e.g., std::cout) is a common example. Namespaces improv e code clarity and maintainability.