C++ Programming MCQ – Namespaces

C++ questions and puzzles focuses on “Namespaces”. One shall practice these questions and puzzles to improve their C++ programming skills needed for various interviews , entrance exams and other competitive exams. These programming puzzles can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

C++ questions and puzzles on “Namespaces” along with answers, explanations and/or solutions:

Q 1. Which operator is used to signify the namespace?
A. conditional operator
B. scope operator
C. ternary operator
D. bitwise operator

Show Answer Answer:-B. scope operator
Explanation Scope operator(::) is used in namespace syntax. General syntax: namespace X{ int a;}

Q 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

Show Answer Answer:-A. Namespace is used to group class, objects and functions
Explanation Namespace allows you to group class, objects, and functions. It is used to divide the global scope into the sub-scopes.

Q 3. What is the use of Namespace?
A. To structure a program into logical units
B. To encapsulate the data
C. Encapsulate the data & structure a program into logical units
D. It is used to mark the beginning of the program

Show Answer Answer:-A. To structure a program into logical units
Explanation The main aim of the namespace is to understand the logical units of the program and to make the program so robust.

Q 4. What is the general syntax for accessing the namespace variable?
A. namespace::operator
B. namespace,operator
C. namespace#operator
D. namespace$operator

Show Answer Answer:-A. namespace::operator
Explanation To access variables from namespace we use following syntax. namespace :: variable; General syntax: namespace X{ int a;}

Q 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
B. 8.31416
C. 9
D. compile time error

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

Q 6. What will be the output of the following C++ code?

  1. #include <iostream>
  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 << a << b;
  21. return 0;
  22. }

A. 10
B. 01
C. 00
D. 11

Show Answer Answer:-A. 10
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

Q 7. What will be the output of the following C++ code?

  1. #include <iostream>
  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 << a;
  17. return 0;
  18. }

A. 4
B. 13
C. 16
D. compile time error

Show Answer Answer:-C. 16
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

Q 8. What will be the output of the following C++ code?

  1. #include <iostream>
  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 << space::x << space::y;
  15. }

A. 55
B. 1015
C. 1510
D. compile time error

Show Answer Answer:-A. 55
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

Q 9. What will be the output of the following C++ code?

  1. #include <iostream>
  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 << i;
  13. }
  14. int main()
  15. {
  16. enum letter { i, j};
  17. class i { letter j; };
  18. ::i();
  19. return 0;
  20. }

A. 9
B. 10
C. 11
D. compile time error

Show Answer Answer:-A. 9
Explanation A scope resolution operator without a scope qualifier refers to the global namespace.

Q 10. Which keyword is used to access the variable in the namespace?
A. dynamic
B. using
C. const
D. static

Show Answer Answer:-B. using
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<

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page