Namespaces(C++ Programming ) Questions and Answers

Question 1.

Which keyword is used to access the variable in namespace?


  1.    using
  2.    dynamic
  3.    const
  4.    static
Explanation:-
Answer: Option A. -> using

None.



Question 2.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
namespace extra
4.
{
5.
int i;
6.
}
7.
void i()
8.
{
9.
using namespace extra;
10.
int i;
11.
i = 9;
12.
cout
  1.    9
  2.    10
  3.    compile time error
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> 9

A scope resolution operator without a scope qualifier refers to the global namespace.



Question 3.


What is the output of this program?


1.
#include
2.
using namespace std
3.
namespace space
4.
{
5.
int x = 10;
6.
}
7.
namespace space
8.
{
9.
int y = 15;
10.
}
11.
int main(int argc, char * argv[])
12.
{
13.
space::x = space::y =5;
14.
cout
  1.    1015
  2.    1510
  3.    55
  4.    compile time error
Explanation:-
Answer: Option C. -> 55

We are overriding the value at the main function and so we are getting the output as 55.
Output:
$ g++ name4.cpp
$ a.out
55



Question 4.


What is the output of this program?


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
  1.    4
  2.    13
  3.    16
  4.    compile time error
Explanation:-
Answer: Option C. -> 16

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



Question 5.


What is the output of these program?


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
  1.    11
  2.    01
  3.    00
  4.    10
Explanation:-
Answer: Option D. -> 10

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



Question 6.


What is the output of this program?


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
  1.    8.31416
  2.    8
  3.    9
  4.    compile time error
Explanation:-
Answer: Option B. -> 8

As we are getting two variables from namespace variable and we are adding that.
Output:
$ g++ name.cpp
$ a.out
8



Question 7.

Which operator is used to signify the namespace?


  1.    conditional operator
  2.    ternary operator
  3.    scope operator
  4.    none of the mentioned
Explanation:-
Answer: Option C. -> scope operator

None.



Question 8.

 What is the use of Namespace?


  1.    To encapsulate the data
  2.    To structure a program into logical units.
  3.    Both a and b
  4.    none of the mentioned
Explanation:-
Answer: Option B. -> To structure a program into logical units.

The main aim of the namespace is to understand the logical units of the program and to make

the program so robust.



Question 9.

Identify the correct statement.


  1.    Namespace is used to group class, objects and functions.
  2.    Namespace is used to mark the beginning of the program.
  3.    Namespace is used to seperate the class, objects.
  4.    None of the above
Explanation:-
Answer: Option A. -> Namespace is used to group class, objects and functions.

Namespace allow you to group class, objects and functions. It is used to divide the global 

scope into the sub-scopes.



Question 10.

What is the general syntax for accessing the namespace variable?


  1.    namespaceid::operator
  2.    namespace,operator
  3.    namespace#operator
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> namespaceid::operator

None.