0
0
C++programming~20 mins

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

Choose your learning style9 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.