C++ Templates and STL Fundamentals Quiz Quiz

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.

  1. Basic Template Syntax

    Which keyword is used in C++ to define a template function that works with multiple data types?

    1. templte
    2. templete
    3. typedef
    4. template

    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.

  2. STL Vector Usage

    Which header file must be included to use the C++ STL vector container in your program?

    1. u003Cvectoru003E
    2. u003Cvectorsu003E
    3. u003Cqueueu003E
    4. u003Carrayu003E

    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.

  3. Instantiating Template Functions

    When you call a template function with int arguments, what does the compiler generate?

    1. A function using void types
    2. A generic function using any type
    3. A new function with int as the parameter type
    4. An error unless specified explicitly

    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.

  4. Accessing Elements in STL Containers

    Which method would you use to access the first element of a std::vectoru003Cintu003E named numbers?

    1. numbers.front()
    2. numbers.start()
    3. numbers.peek()
    4. numbers.getFirst()

    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.

  5. Template Classes

    What is the correct syntax for declaring a template class with one type parameter named T in C++?

    1. typeu003Cclass Tu003E
    2. templateu003Cclass Tu003E
    3. template{class T}
    4. templte(class T)

    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.

  6. STL Iterator Purpose

    In C++, what is the primary purpose of an STL iterator?

    1. To traverse container elements
    2. To sort elements directly
    3. To store container sizes
    4. To manage memory allocation

    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.

  7. Map Key Uniqueness

    Which property of the std::map container ensures each key appears only once?

    1. Keys must be integers
    2. All keys must be unique
    3. Keys must be sorted
    4. Keys can be duplicated

    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.

  8. Default Constructor Requirement

    Which of the following statements about type parameters in C++ class templates is correct?

    1. Type parameters do not have to provide a default constructor unless required by template code
    2. Type parameters cannot be pointers
    3. Type parameters must be built-in types
    4. All type parameters must always provide a default constructor

    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.

  9. STL Set Uniqueness

    What happens if you attempt to insert a duplicate value into a std::setu003Cintu003E?

    1. The duplicate value is ignored and the set remains unchanged
    2. All existing values are removed
    3. The set throws an exception
    4. The set becomes unordered

    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.

  10. Finding Elements in std::vector

    Which STL algorithm would you use to find a value inside a std::vectoru003Cintu003E container?

    1. std::find
    2. std::findValue
    3. std::search
    4. std::locate

    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.