My Report

C++ Namespace Test – 1


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

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

3. Identify the correct statement.

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

5. Which operator is used to signify the namespace?

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

7. What is the general syntax for accessing the namespace variable?

8. What is the use of Namespace?

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

10. Which keyword is used to access the variable in the namespace?


 

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.