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
View Answer
Explanation: Scope operator(::) is used in namespace syntax.
General syntax:
namespace X{ int a;}
cout<<X::a;
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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: To access variables from namespace we use following syntax.
namespace :: variable;
General syntax:
namespace X{ int a;}
cout<<X::a;
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
namespace first
{
int var = 5;
}
namespace second
{
double var = 3.1416;
}
int main ()
{
int a;
a = first::var + second::var;
cout << a;
return 0;
}
a) 8.31416
b) 8
c) 9
d) compile time error
View Answer
Explanation: As we are getting two variables from namespace variable and we are adding that.
Output:
$ g++ name.cpp $ a.out 8
6. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
namespace first
{
int x = 5;
int y = 10;
}
namespace second
{
double x = 3.1416;
double y = 2.7183;
}
int main ()
{
using first::x;
using second::y;
bool a, b;
a = x > y;
b = first::y < second::x;
cout << a << b;
return 0;
}
a) 11
b) 01
c) 00
d) 10
View Answer
Explanation: 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?
#include <iostream>
using namespace std;
namespace Box1
{
int a = 4;
}
namespace Box2
{
int a = 13;
}
int main ()
{
int a = 16;
Box1::a;
Box2::a;
cout << a;
return 0;
}
a) 4
b) 13
c) 16
d) compile time error
View Answer
Explanation: 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
8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
namespace space
{
int x = 10;
}
namespace space
{
int y = 15;
}
int main(int argc, char * argv[])
{
space::x = space::y =5;
cout << space::x << space::y;
}
a) 1015
b) 1510
c) 55
d) compile time error
View Answer
Explanation: 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
9. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
namespace extra
{
int i;
}
void i()
{
using namespace extra;
int i;
i = 9;
cout << i;
}
int main()
{
enum letter { i, j};
class i { letter j; };
::i();
return 0;
}
a) 9
b) 10
c) compile time error
d) 11
View Answer
Explanation: A scope resolution operator without a scope qualifier refers to the global namespace.
10. Which keyword is used to access the variable in the namespace?
a) using
b) dynamic
c) const
d) static
View Answer
Explanation: using keyword is used to specify the name of the namespace to which the variable belongs.
eg.
namespace A{ int a = 5;}
namespace B{ int a = 10;}
using namespace A;
cout<<a; // will print value of a from namespace A.
using namespace B;
cout<<a; // will print value of a from namespace B.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Programming MCQs
- Check C++ Books
- Practice Computer Science MCQs
- Check Programming Books
- Apply for Computer Science Internship