My Report

C++ Namespace Test – 2


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. 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;
}

2. Which is the correct syntax of declaring a namespace?

3. 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;
}
------------------------------------------------

4. Which operator is used for accessing a member of 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()
{
	int var = 20;
	using namespace B;
	cout<<var;
}

6. 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;
}

7. 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;
}
-----------------------------------------------

8. 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;
}

9. What is the correct syntax of defining a namespace?

10. How to print the value of the i variable inside namespace B?

namespace A{
	int var = 10;
	namespace B{
		int i = 15;
	}
}

11. 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;
}

12. Pick the incorrect statement for namespaces in C++.

13. 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;
}
------------------------------------------------

 

Start practicing “1000 MCQs on C++”, and once you are ready, you can take tests on all topics by attempting our “C++ Test Series”.

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.