Challenge - 5 Problems
Namespace Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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;
}Attempts:
2 left
💡 Hint
Look at which namespace is brought into the global scope and how the qualified call works.
✗ Incorrect
The 'using namespace A;' brings A::print into the global scope, so calling print() calls A::print. Calling B::print() explicitly calls B's version. So output is 'A::print' then 'B::print'.
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Check how 'using namespace std;' affects the name lookup for vector.
✗ Incorrect
The first vector is declared with std::vector explicitly. After 'using namespace std;', vector can be used without std::. Both vectors have 3 elements, so output is '3 3'.
❓ Predict Output
advanced2: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;
}Attempts:
2 left
💡 Hint
Look at how 'using' brings a nested namespace function into scope.
✗ Incorrect
The 'using outer::inner::greet;' brings the greet function into the global scope, so calling greet() prints 'Hello from inner'.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
Check the scope of 'using namespace std;' and variable s.
✗ Incorrect
Inside the inner block, 'using namespace std;' allows using cout and string without std::. The variable s is declared inside the block and printed. The output is 'Hello'.
🧠 Conceptual
expert2:00remaining
Why avoid 'using namespace std;' in header files?
Which is the main reason to avoid placing 'using namespace std;' in header files?
Attempts:
2 left
💡 Hint
Think about how header files are included in multiple source files.
✗ Incorrect
Putting 'using namespace std;' in headers forces all files including that header to have std namespace imported globally, which can cause name conflicts and unexpected behavior.