0
0
C++programming~10 mins

Namespace concept and std usage in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to use the standard namespace prefix for cout.

C++
#include <iostream>
int main() {
    [1]::cout << "Hello, world!" << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
Anamespace
Busing
Ccout
Dstd
Attempts:
3 left
💡 Hint
Common Mistakes
Writing 'using::cout' instead of 'std::cout'.
Forgetting the namespace prefix and writing just 'cout'.
2fill in blank
medium

Complete the code to bring all standard names into the current scope.

C++
#include <iostream>
[1] std;
int main() {
    cout << "Welcome!" << endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
Ausing namespace
Bnamespace
Cusing
Dinclude
Attempts:
3 left
💡 Hint
Common Mistakes
Writing only 'using std;' which is incomplete.
Writing 'namespace std;' which declares a namespace, not bring it into scope.
3fill in blank
hard

Fix the error in the code to correctly use the standard vector.

C++
#include <vector>
int main() {
    std::[1]<int> numbers = {1, 2, 3};
    return 0;
}
Drag options to blanks, or click blank then click option'
AVector
Bvector
Cvect
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Vector' with uppercase V which is not defined in std.
Using 'vect' which is not a standard container.
4fill in blank
hard

Fill both blanks to define and use a custom namespace with a function call.

C++
#include <iostream>
namespace [1] {
    void greet() {
        std::cout << "Hi from namespace!" << std::endl;
    }
}

int main() {
    [2]::greet();
    return 0;
}
Drag options to blanks, or click blank then click option'
Acustom
Bstd
Dmain
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the namespace definition and call.
Calling greet without the namespace prefix.
5fill in blank
hard

Fill all three blanks to create a namespace alias and use it to print a message.

C++
#include <iostream>
namespace [1] = std;

int main() {
    [2]::cout << "Alias works!" << [3];
    return 0;
}
Drag options to blanks, or click blank then click option'
As
Cstd::endl
Dendl
Attempts:
3 left
💡 Hint
Common Mistakes
Using different alias names in definition and usage.
Using 'endl' without the namespace or alias prefix.