This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “STL Container Any – 1”.
1. What is any in C++?
a) STL container used to store a single value of any type
b) Exception class in C++
c) Fundamental type provided by C++
d) Template data type
View Answer
Explanation: Any is an STL container provided by C++ to store value or objects of any type.
2. In how many different ways any-container can be constructed?
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: There are three basic ways of constructing any variable. They are done using copy initialization, using the constructor or using an assignment operator.
3. What is the correct syntax of constructing any using copy initialization?
a) any variable_name = value;
b) any variable_name(value);
c)
any variable_name; variable_name = value;
d) any <type>variable_name = value;
View Answer
Explanation: To initialize an any variable using copy initialization we use the following syntax:
any variable_name = value;
4. What is the correct syntax of constructing any using parameterized constructor?
a) any variable_name = value;
b) any variable_name(value);
c)
any variable_name; variable_name = value;
d) any <type>variable_name = value;
View Answer
Explanation: To initialize an any variable using parameterized constructor we use the following syntax:
any variable_name(value);
5. What is the correct syntax of constructing any using assignment operator?
a) any variable_name = value;
b) any variable_name(value);
c)
any variable_name; variable_name = value;
d) any <type>variable_name = value;
View Answer
Explanation: To initialize an any variable using assignment operator we use the following syntax:
any variable_name; variable_name = value;
6. Which of the following syntax is used to convert any variable to its original type?
a) any_cast<variable_name>();
b) any_cast(variable_name);
c) <original_type>(variable_name);
d) any_cast<original_type>(variable_name);
View Answer
Explanation: The syntax used to convert the any variable to its original type is as follows:
any_cast(variable_name);
7. Which header file is required to use any container?
a) <any>
b) <stl>
c) <container-any>
d) <containers>
View Answer
Explanation: <any> header file is required to use any container and its realted functions.
8. What will be the output of the following C++ code?
#include<iostream> #include<any> using namespace std; int main() { int a = 5; any var = a; cout<<var<<endl; return 0; }
a) 5
b) Compile-time error
c) Run-time error
d) Nothing is printed
View Answer
Explanation: C++ does not allow programmer to directly print the value of any container variable. One need type cast the any variable before printing.
9. What will be the output of the following C++ code?
#include<iostream> #include<any> #include<string> using namespace std; int main() { string s = "Hello World"; any var(s); cout<<any_cast<string>(var)<<endl; return 0; }
a) Run-time error
b) Compile-time error
c) Hello World
d) Nothing is printed
View Answer
Explanation: In the above program as we have converted the value to its original type before printing therefore the program runs perfectly and outputs “Hello World”.
10. What will be the output of the following C++ code?
#include<iostream> #include<any> #include<string> using namespace std; int main() { string s = "Hello World"; any var(s); cout<<any_cast<char*>(var)<<endl; return 0; }
a) Hello World
b) Compile-time error
c) Run-time error
d) Nothing is printed
View Answer
Explanation: In this program as we are trying to convert an string into char* which is not same therefore the program gives run-time error saying bad_any_cast.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Programming MCQs
- Practice Computer Science MCQs
- Apply for C++ Internship
- Check Programming Books
- Check Computer Science Books