250+ TOP MCQs on Namespaces – 1 and Answers

This section on C++ questions and puzzles on “Namespaces”. One shall practice these questions and puzzles to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These programming puzzles can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ questions and puzzles on “Namespaces” along with answers, explanations and/or solutions:

1. Which operator is used to signify the namespace?
a) conditional operator
b) ternary operator
c) scope operator
d) bitwise operator
Answer: c
Clarification: Scope operator(::) is used in namespace syntax.
General syntax:
namespace X{ int a;}
cout<

2. Identify the correct statement.
a) Namespace is used to group class, objects and functions
b) Namespace is used to mark the beginning of the program
c) A namespace is used to separate the class, objects
d) Namespace is used to mark the beginning & end of the program
Answer: a
Clarification: Namespace allows you to group class, objects, and functions. It is used to divide the global scope into the sub-scopes.

3. What is the use of Namespace?
a) To encapsulate the data
b) To structure a program into logical units
c) Encapsulate the data & structure a program into logical units
d) It is used to mark the beginning of the program
Answer: b
Clarification: The main aim of the namespace is to understand the logical units of the program and to make the program so robust.

4. What is the general syntax for accessing the namespace variable?
a) namespace::operator
b) namespace,operator
c) namespace#operator
d) namespace$operator
Answer: a
Clarification: To access variables from namespace we use following syntax.
namespace :: variable;
General syntax:
namespace X{ int a;}
cout<

5. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     namespace first
  4.     {
  5.         int var = 5;
  6.     }
  7.     namespace second
  8.     {
  9.         double var = 3.1416;
  10.     }
  11.     int main ()
  12.     {
  13.         int a;
  14.         a = first::var + second::var;
  15.         cout << a;
  16.         return 0;
  17.    }

a) 8.31416
b) 8
c) 9
d) compile time error
Answer: b
Clarification: As we are getting two variables from namespace variable and we are adding that.
Output:

6. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     namespace first
  4.     {
  5.         int x = 5;
  6.         int y = 10;
  7.     }
  8.     namespace second
  9.     {
  10.         double x = 3.1416;
  11.         double y = 2.7183;
  12.     }
  13.     int main ()
  14.     {
  15.         using first::x;
  16.         using second::y;
  17.         bool a, b;
  18.         a = x > y;
  19.         b = first::y < second::x;
  20.         cout << a << b;
  21.         return 0;
  22.     }

a) 11
b) 01
c) 00
d) 10
Answer: d
Clarification: We are inter mixing the variable and comparing it which is bigger and smaller and according to that we are printing the output.
Output:

$ g++ name1.cpp
$ a.out
10

7. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     namespace Box1
  4.     {
  5.         int a = 4;
  6.     }
  7.     namespace Box2
  8.     {
  9.         int a = 13;
  10.     }
  11.     int main ()
  12.     {
  13.         int a = 16;
  14.         Box1::a;
  15.         Box2::a;
  16.         cout << a;
  17.         return 0;
  18.     }

a) 4
b) 13
c) 16
d) compile time error
Answer: c
Clarification: In this program, as there is lot of variable a and it is printing the value inside the block because it got the highest priority.
Output:

$ g++ name2.cpp
$ a.out
16