Explore key concepts in C++ templates and Standard Template Library with this beginner-friendly quiz. Enhance your understanding of template syntax, STL containers, iterators, and common usage patterns essential for modern C++ programming.
Which keyword is used in C++ to define a template function that works with multiple data types?
Explanation: The 'template' keyword is required to declare template functions or classes in C++. The distractors 'templte' and 'templete' are both misspelled and not valid keywords in C++. 'typedef' is used to create type aliases, not templates, so it does not serve this purpose.
Which header file must be included to use the C++ STL vector container in your program?
Explanation: 'u003Cvectoru003E' is the correct header needed to access the vector container in STL. 'u003Carrayu003E' provides support for fixed-size arrays, not dynamic vectors. 'u003Cqueueu003E' includes the queue container, which is unrelated to vectors. 'u003Cvectorsu003E' is a common typo and not an existing header.
When you call a template function with int arguments, what does the compiler generate?
Explanation: The compiler generates a specialized version of the template function with int substituted for the template type. If types are mismatched or unspecified, explicit specification is only required in some ambiguous cases, not always. C++ templates do not substitute void types in place of actual types. Lastly, C++ compilers do not create truly generic functions that can use any type without instantiation.
Which method would you use to access the first element of a std::vectoru003Cintu003E named numbers?
Explanation: 'numbers.front()' returns a reference to the first element in the vector. The method 'numbers.start()' does not exist in STL, and 'numbers.peek()' is not a valid vector method, as peek is sometimes used in stacks or queues. 'numbers.getFirst()' is also incorrect, as vectors do not provide this method.
What is the correct syntax for declaring a template class with one type parameter named T in C++?
Explanation: The syntax 'templateu003Cclass Tu003E' is standard and required to declare a template class with one type parameter. The other options use either a misspelled keyword, incorrect delimiters, or non-existent syntax, making them invalid C++ code.
In C++, what is the primary purpose of an STL iterator?
Explanation: STL iterators are designed to iterate or traverse through the elements of an STL container. While algorithms like sort can use iterators, iterators themselves do not perform sorting. They also do not store container sizes, nor are they responsible for managing memory allocation, so those options are incorrect.
Which property of the std::map container ensures each key appears only once?
Explanation: In std::map, every key must be unique, which means no duplicates are allowed. While std::map does keep keys sorted, sorting is not what ensures uniqueness. Allowing duplicate keys is the behavior of std::multimap, not std::map. Keys in a map can be of any comparable type, not integers exclusively.
Which of the following statements about type parameters in C++ class templates is correct?
Explanation: Type parameters in templates only need a default constructor if the template code specifically uses it. Not all template code requires a default constructor for type parameters. Template parameters can be user-defined types or built-in types and may also be pointers, so the other statements are incorrect.
What happens if you attempt to insert a duplicate value into a std::setu003Cintu003E?
Explanation: std::set does not allow duplicate values, so trying to add a duplicate simply leaves the set unchanged. The set does not throw exceptions for duplicates. Inserting a duplicate does not remove any values, and it does not affect the set’s ordering, which is automatically maintained.
Which STL algorithm would you use to find a value inside a std::vectoru003Cintu003E container?
Explanation: std::find is the appropriate STL algorithm to search for a value in a sequential container like std::vector. std::search is used for finding subsequences rather than individual values. std::locate and std::findValue are not standard C++ STL algorithms, making them incorrect options.