This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Namespaces – 2”.
1. Pick the incorrect statement for namespaces in C++.
a) Namespace declarations are always global scope
b) Keyword namespace is used at the starting of a namespace definition
c) Namespace has access specifiers like private or public
d) Namespace definitions can be nested
View Answer
Explanation: Namespace does not have any specifiers associated with it like classes or structures.
2. Which operator is used for accessing a member of namespace?
a) :
b) ::
c) ->
d) .
View Answer
Explanation: Scope resolution operator(::) is used for accessing a member of a namespace. example:
namespace A{ int var; } A::var = 5;
3. Which is the correct syntax of declaring a namespace?
a)
namespace A{ int i }
b)
namespace B{ int i; };
c)
namespace C{ int i; }
d)
Namespace D{ int i }View Answer
Explanation: A namespace definition always starts with the namespace keyword so definition with Namespace(capital N) is wrong. namespace does is not terminated by a semicolon hence the definition with a semicolon is wrong. every variable declaration in C++ should end with semicolon therefore namespace containing ‘int i’ without semicolon is wrong.
4. What will be the output of the following C++ code?
#include <iostream> #include <string> using namespace std; namespace A{ int var = 10; } namespace B{ int cout = 5; } int main() { using namespace B; cout<<A::var; }
a) 10
b) 5
c) Error
d) 105
View Answer
Explanation: Variable cout is defined in above defined namespace B and also in the inbuilt namespace std. So the compiler confuses and throws an error saying that cout is ambiguous i.e. which cout to use as it i available in both std and B namespace.
5. What will be the output of the following C++ code?
#include <iostream> #include <string> using namespace std; namespace A{ int var = 10; } namespace B{ int var = 5; } int main() { using namespace B; cout<<var; }
a) 5
b) 10
c) Error
d) Wrong use of namespace
View Answer
Explanation: As we have mentioned that ‘using namespace B’ so now whereever var will be used it will be from namespace B. hence the output was the value of var from namespace B.
6. What will be the output of the following C++ code?
#include <iostream> #include <string> using namespace std; namespace A{ int var = 10; } namespace B{ int var = 5; } int main() { int var = 20; using namespace B; cout<<var; }
a) 5
b) 10
c) 20
d) Error
View Answer
Explanation: As var is already declared in this scope so that gets preference over others. Therefore 20 is printed which is the value assigned to var declared in this scope.
7. What will be the output of the following C++ code?
#include <iostream> #include <string> using namespace std; namespace { int var = 10; } int main() { cout<<var; }
a) 10
b) Error
c) Some garbage value
d) Nothing but program runs perfectly
View Answer
Explanation: A namespace without name is called unnamed namespace and is valid in that scope only. So its like global scope of variable. One can access that var from main() function.
8. What is the correct syntax of defining a namespace?
a) namespace name{}
b) Namespace name{};
c) namespace name{};
d) typedef namespace name{} NAME
View Answer
Explanation: A namespace:
-Starts with keyword namespace
-Followed by identifier
-All members inside the braces{}
-No semicolon at the end
namespace identifier{}.
9. How to print the value of the i variable inside namespace B?
namespace A{ int var = 10; namespace B{ int i = 15; } }
a) cout<<A::i;
b) cout<<B::i;
c) cout<<A::B::i;
d) cout<<i;
View Answer
Explanation: Here namespace B is nested inside the namespace A. Hence to access the variable i we need to mention through B and A. So it should A::B::i, which means i belongs to namespace B which is defined inside the namespace A.
10. What will be the output of following C++ code?
#include <iostream> #include <string> using namespace std; namespace My_old_school_and_college_friends_number { long int f1 = 9999999999; long int f2 = 1111111111; } namespace contacts = My_old_school_and_college_friends_number; int main(){ cout<<contacts::f1; }
a) 9999999999
b) 1111111111
c) error
d) segmentation fault
View Answer
Explanation: C++ allows to use namespaces aliases i.e. if a namespace having a large name we can assign it to a new namespace having a small name for the convenience in coding. This assignment of namespaces is called namespace aliasing.
11. What will be the output of the following C++ code?
Content of header file h1.h ------------------------------------------------ h1.h #include <iostream> using namespace std; namespace A{ int func(int a){ cout<<"using namespace A"; return 2*a; } } ------------------------------------------------ Content of header file h2.h ------------------------------------------------ h2.h #include <iostream> using namespace std; namespace B{ float func(float a){ cout<<"using namespace B"; return 2*a; } } ------------------------------------------------ Content of program.cpp ------------------------------------------------ #include <iostream> #include <string> #include "h1.h" #include "h2.h" using namespace std; using namespace A; using namespace B; int main(int argc, char const *argv[]) { /* code */ int a = 10; float b = 10.0; cout<<func(a)<<endl; cout<<func(b); return 0; } -----------------------------------------------
a)
using namespace A10 using namespace B10
b)
using namespace A20 using namespace B20
c) Error due to clash of func()
d) This is not allwed in C++
View Answer
Explanation: Here both the func() are available in different namespaces having same name but different types which is equivalent to function overloading. So no error will be there as function overloading is allowed in C++.
12. What will be the output of the following C++ code?
Content of header file h1.h ------------------------------------------------ h1.h #include <iostream> using namespace std; namespace A{ int func(int a){ cout<<"using namespace A"; return 2*a; } } ------------------------------------------------ Content of header file h2.h ------------------------------------------------ h2.h #include <iostream> using namespace std; namespace B{ float func(float a){ cout<<"using namespace B"; return 2*a; } } ------------------------------------------------ Content of program.cpp ------------------------------------------------ #include <iostream> #include <string> #include "h1.h" #include "h2.h" using namespace std; using namespace A; using namespace B; int main(int argc, char const *argv[]) { /* code */ int a = 10; float b = 10.0; cout<<A::func(a)<<endl; cout<<A::func(b); return 0; } ------------------------------------------------
a)
using namespace A20 using namespace B20
b)
using namespace A20 using namespace A20
c)
using namespace A10 using namespace A10
d)
using namespace A10 using namespace B10
Explanation: Here we have specified that func() should be called from the namespace A, hence both the calls will use the same function from namespace A.
13. What changes you can do in the header files to avoid the redefinition that compiler will give when both the header files are included in the same program keeping the declaration of both the functions same?
Content of h1.h ------------------------------------------------ h1.h #include <iostream> using namespace std; int func(int a){ cout<<"Multiplied by 2"; return 2*a; } ------------------------------------------------ Content of h2.h ------------------------------------------------ h2.h #include <iostream> using namespace std; int func(int a){ cout<<"divided by 2"; return a/2; } ------------------------------------------------
a) Cannot be handled because C++ does not allow this
b) Declare both the function inside different namespaces
c) Include one header files where they are needed so that no clashes occur
d) Make the header files name same
View Answer
Explanation: Define both the function in different namespaces under their respective header files. So whenever we need them we can use the name of their respective namespaces to call them. This will resolve the error keeping function declarations same.
Modified Header files :
h1.h Content of h1.h ------------------------------------------------ #include <iostream> using namespace std; namespace A{ int func(int a){ cout<<"Multiplied by 2"; return 2*a; } } ------------------------------------------------ Content of h2.h ------------------------------------------------ #include <iostream> using namespace std; namespace B{ float func(float a){ cout<<"divided by 2"; return a/2; } } ------------------------------------------------
Now one can use multiplication func as A::func(int); and dividion function as B::func(int).
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.
- Check Programming Books
- Check Computer Science Books
- Practice Computer Science MCQs
- Check C++ Books
- Practice Programming MCQs