C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Namespaces – 2”.
Q 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
Show Answer
Answer:-C. Namespace has access specifiers like private or publicExplanation
Namespace does not have any specifiers associated with it like classes or structures.Q 2. Which operator is used for accessing a member of namespace?
A. :
B. ::
C. ->
D. .
Show Answer
Answer:-B. ::Explanation
Scope resolution operator(::) is used for accessing a member of a namespace. example: namespace A{ int var; } A::var = 5;Q 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
}
Show Answer
Answer:-C. namespace C{ int i;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.Q 4. What will be the output of the following C++ code?
include
include
using namespace std;
namespace A{
in var =1
}
namespace B{
int cout = 5;
}
int main()
{
using namespace B;
cout<<A::var;
}
A. 5
B. 10
C. 105
D. Error
Show Answer
Answer:-D. ErrorExplanation
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.Q 5. What will be the output of the following C++ code?
include
include
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
Show Answer
Answer:-A. 5Explanation
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.Q 6. What will be the output of the following C++ code?
include
include
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
Show Answer
Answer:-C. 20Explanation
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.Q 7. What will be the output of the following C++ code?
include
include
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
Show Answer
Answer:-A. 10Explanation
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.Q 8. What is the correct syntax of defining a namespace?
A. Namespace name{};
B. namespace name{}
C. namespace name{};
D. typedef namespace name{} NAME
Show Answer
Answer:-B. namespace name{}Explanation
A namespace: -Starts with keyword namespace -Followed by identifier -All members inside the braces{} -No semicolon at the end namespace identifier{}.Q 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;
Show Answer
Answer:-C.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.Q 10. What will be the output of following C++ code?
include
include
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
Show Answer
Answer:-A. 9999999999Explanation
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.Q 11. What will be the output of the following C++ code?
Content of header file h1.h
h1.h
include
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
using namespace std;
namespace B{
float func(float a){
cout<<“using namespace B”;
return 2*a;
}
}
Content of program.cpp
include
include
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++
Show Answer
Answer:-B. using namespace A20 using namespace B20Explanation
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++.Q 12. What will be the output of the following C++ code?
Content of header file h1.h
h1.h
include
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
using namespace std;
namespace B{
float func(float a){
cout<<“using namespace B”;
return 2*a;
}
}
Content of program.cpp
include
include
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
Show Answer
Answer:-B. using namespace A20 using namespace A20Explanation
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.Q 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
using namespace std;
int func(int a){
cout<<“Multiplied by 2”;
return 2*a;
}
Content of h2.h
h2.h
include
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
Leave a Reply