Bird
Raised Fist0
C++programming~20 mins

Namespace concept and std usage in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Namespace Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of using std namespace with conflicting function names
What is the output of this C++ code?
C++
#include <iostream>

namespace A {
    void print() { std::cout << "A::print" << std::endl; }
}

namespace B {
    void print() { std::cout << "B::print" << std::endl; }
}

using namespace A;

int main() {
    print();
    B::print();
    return 0;
}
A
A::print
B::print
B
B::print
B::print
C
A::print
A::print
DCompilation error due to ambiguous call to print()
Attempts:
2 left
💡 Hint
Look at which namespace is brought into the global scope and how the qualified call works.
❓ Predict Output
intermediate
2:00remaining
Effect of 'using namespace std;' on vector usage
What is the output of this C++ code?
C++
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v1 = {1, 2, 3};
    using namespace std;
    vector<int> v2 = {4, 5, 6};
    std::cout << v1.size() << " " << v2.size() << std::endl;
    return 0;
}
A
0 3
B
Compilation error: vector not found
C
3 0
D
3 3
Attempts:
2 left
💡 Hint
Check how 'using namespace std;' affects the name lookup for vector.
❓ Predict Output
advanced
2:00remaining
Output of nested namespaces and using declarations
What is the output of this C++ code?
C++
#include <iostream>

namespace outer {
    namespace inner {
        void greet() { std::cout << "Hello from inner" << std::endl; }
    }
}

using outer::inner::greet;

int main() {
    greet();
    return 0;
}
A
Compilation error: greet not found
B
Hello from inner
C
Hello from outer
D
No output
Attempts:
2 left
💡 Hint
Look at how 'using' brings a nested namespace function into scope.
❓ Predict Output
advanced
2:00remaining
Effect of 'using namespace std;' inside a function
What is the output of this C++ code?
C++
#include <iostream>
#include <string>

int main() {
    {
        using namespace std;
        string s = "Hello";
        cout << s << std::endl;
    }
    // cout << s << std::endl; // Uncommenting this line causes error
    return 0;
}
A
Hello
B
Compilation error: cout not found
C
No output
D
Compilation error: s not found
Attempts:
2 left
💡 Hint
Check the scope of 'using namespace std;' and variable s.
🧠 Conceptual
expert
2:00remaining
Why avoid 'using namespace std;' in header files?
Which is the main reason to avoid placing 'using namespace std;' in header files?
AIt makes the compilation slower because std namespace is large.
BIt prevents the use of other namespaces in the same project.
CIt causes namespace pollution and can lead to name conflicts in all files including that header.
DIt causes runtime errors due to ambiguous symbols.
Attempts:
2 left
💡 Hint
Think about how header files are included in multiple source files.

Practice

(1/5)
1. What is the main purpose of using namespaces in C++?
easy
A. To speed up program execution
B. To group code and avoid name conflicts
C. To allocate memory dynamically
D. To create new data types

Solution

  1. Step 1: Understand what namespaces do

    Namespaces help organize code by grouping related functions, classes, and variables.
  2. Step 2: Identify the main benefit

    This grouping prevents name conflicts when different parts of code use the same names.
  3. Final Answer:

    To group code and avoid name conflicts -> Option B
  4. Quick Check:

    Namespaces = avoid name conflicts [OK]
Hint: Namespaces group code to prevent name clashes [OK]
Common Mistakes:
  • Thinking namespaces speed up execution
  • Confusing namespaces with memory management
  • Assuming namespaces create new data types
2. Which of the following is the correct way to use cout from the std namespace without a using directive?
easy
A. cout << "Hello";
B. using std::cout; cout << "Hello";
C. std::cout << "Hello";
D. namespace std; cout << "Hello";

Solution

  1. Step 1: Recall how to access namespace members

    Without a using directive, you must prefix with the namespace and scope operator (::).
  2. Step 2: Apply to cout

    So, std::cout is the correct way to access cout from std namespace.
  3. Final Answer:

    std::cout << "Hello"; -> Option C
  4. Quick Check:

    Use std:: prefix without using directive [OK]
Hint: Use std:: before cout without using directive [OK]
Common Mistakes:
  • Omitting std:: prefix without using directive
  • Trying to declare namespace with 'namespace std;' incorrectly
  • Using using directive incorrectly inside code
3. What will be the output of the following code?
namespace first {
    int value = 5;
}
namespace second {
    int value = 10;
}
#include <iostream>
int main() {
    std::cout << first::value << ", " << second::value << std::endl;
    return 0;
}
medium
A. Compilation error
B. 10, 5
C. 5 10
D. 5, 10

Solution

  1. Step 1: Identify values in each namespace

    Namespace first has value 5, namespace second has value 10.
  2. Step 2: Check output statement

    The code prints first::value, then a comma and space, then second::value.
  3. Final Answer:

    5, 10 -> Option D
  4. Quick Check:

    first::value = 5, second::value = 10 [OK]
Hint: Use namespace::item to get correct values [OK]
Common Mistakes:
  • Mixing values from namespaces
  • Forgetting comma in output
  • Expecting compilation error due to namespaces
4. Identify the error in this code snippet:
using namespace std;
int main() {
    cout << "Hello" << endl;
    int cout = 5;
    cout << endl;
    return 0;
}
medium
A. Cannot declare variable named cout after using namespace std
B. Missing semicolon after cout declaration
C. endl is not defined
D. No error, code runs fine

Solution

  1. Step 1: Understand using namespace std effect

    Using namespace std brings std::cout into the global scope.
  2. Step 2: Check variable declaration conflict

    Declaring an int variable named cout hides std::cout, causing errors when using cout as stream.
  3. Final Answer:

    Cannot declare variable named cout after using namespace std -> Option A
  4. Quick Check:

    Variable name conflicts with std::cout [OK]
Hint: Don't name variables same as std items like cout [OK]
Common Mistakes:
  • Thinking missing semicolon causes error
  • Assuming endl is undefined
  • Believing code runs without error
5. Given two namespaces:
namespace alpha {
    int x = 1;
}
namespace beta {
    int x = 2;
    int getX() { return x; }
}
int main() {
    using namespace alpha;
    using beta::getX;
    std::cout << x << ", " << getX() << std::endl;
    return 0;
}

What is the output of this program?
hard
A. 1, 2
B. 2, 2
C. 1, 1
D. Compilation error due to ambiguous x

Solution

  1. Step 1: Analyze using directives

    Using namespace alpha brings alpha::x into scope as x.
  2. Step 2: Using beta::getX only imports getX, not beta::x

    So getX() returns beta::x which is 2, but x refers to alpha::x which is 1.
  3. Step 3: Output values

    std::cout prints x (1), then comma and space, then getX() (2).
  4. Final Answer:

    1, 2 -> Option A
  5. Quick Check:

    alpha::x = 1, beta::getX() = 2 [OK]
Hint: using namespace imports all; using specific imports one item [OK]
Common Mistakes:
  • Assuming getX uses alpha::x
  • Expecting ambiguous x error
  • Thinking beta::x is imported by using beta::getX
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard