Templates & STL Question Bank for C-CAT
Topic-wise Templates & STL MCQs for CDAC C-CAT preparation with answers and explanations.
Show Answer & Explanation
Correct Answer: B - Generic programming
Templates enable generic programming - write once, use with any type.
Show Answer & Explanation
Correct Answer: C - template<typename T>
Template syntax: template<typename T> or template<class T>.
Show Answer & Explanation
Correct Answer: C - Standard Template Library
STL is Standard Template Library.
Show Answer & Explanation
Correct Answer: B - printf
printf is a C function, not an STL container.
Show Answer & Explanation
Correct Answer: B - Dynamic array
vector is a dynamic array that can grow in size.
Show Answer & Explanation
Correct Answer: B - Key-value pairs
map stores key-value pairs with unique sorted keys.
Show Answer & Explanation
Correct Answer: B - Traverse containers
Iterators are used to traverse STL containers.
Show Answer & Explanation
Correct Answer: D - Stores unique sorted elements
set stores unique elements in sorted order.
Show Answer & Explanation
Correct Answer: B - LIFO
STL stack follows LIFO (Last In First Out).
Show Answer & Explanation
Correct Answer: D - Specific implementation for a type
Template specialization provides specific implementation for particular types.
Show Answer & Explanation
Correct Answer: B - Sorted order
STL set stores unique elements in sorted order using balanced BST (usually Red-Black tree).
Show Answer & Explanation
Correct Answer: D - Key-value pairs in sorted order
STL map stores key-value pairs sorted by key, with unique keys.
Show Answer & Explanation
Correct Answer: C - Hash table
unordered_map uses hash table for O(1) average access time.
Show Answer & Explanation
Correct Answer: B - Insertion at both ends efficiently
Deque (double-ended queue) allows efficient insertion/deletion at both front and back.
Show Answer & Explanation
Correct Answer: C - Max-heap (largest first)
STL priority_queue is a max-heap by default; largest element has highest priority.
Show Answer & Explanation
Correct Answer: D - Generalized pointers to elements
Iterators are objects that point to elements in containers, providing uniform access.
Show Answer & Explanation
Correct Answer: B - O(n log n)
STL sort() uses Introsort with O(n log n) average and worst-case complexity.
Show Answer & Explanation
Correct Answer: A - Variable number of template parameters
Variadic templates accept variable number of template arguments using ... syntax.
Show Answer & Explanation
Correct Answer: A - List faster for middle insertion
List has O(1) insertion anywhere with iterator, vector has O(n) for middle insertion.
Show Answer & Explanation
Correct Answer: C - Deduces type automatically
auto keyword deduces type from initializer, simplifying iterator declarations.
Show Answer & Explanation
Correct Answer: D - A blueprint for creating functions that work with different data types
A function template is a blueprint that allows creating functions that can work with any data type. The compiler generates specific functions for each type used.
Show Answer & Explanation
Correct Answer: C - template <class T> T func(T a) {}
The correct syntax starts with 'template <class T>' or 'template <typename T>' followed by the function definition using T as the type parameter.
What will be the output?
template <typename T>
T getMax(T a, T b) {
return (a > b) ? a : b;
}
int main() {
cout << getMax(10, 20);
}Show Answer & Explanation
Correct Answer: B - 20
The template function getMax compares 10 and 20, returning the larger value 20. The compiler deduces T as int from the arguments.
Show Answer & Explanation
Correct Answer: D - They are interchangeable in most contexts
In template declarations, 'class' and 'typename' are interchangeable and can be used with any data type. 'typename' is slightly preferred in modern C++ for clarity.
Show Answer & Explanation
Correct Answer: C - A blueprint for creating classes that work with different data types
A class template is a blueprint for creating classes that can operate on different data types. For example, vector<int> and vector<string> are instantiations of the vector class template.
What will be the output?
template <class T>
class Pair {
T first, second;
public:
Pair(T a, T b) : first(a), second(b) {}
T getMax() { return (first > second) ? first : second; }
};
int main() {
Pair<int> p(15, 25);
cout << p.getMax();
}Show Answer & Explanation
Correct Answer: D - 25
Pair<int> instantiates the template with T = int. getMax() returns the larger of 15 and 25, which is 25.
Show Answer & Explanation
Correct Answer: D - Providing a specific implementation for a particular data type
Template specialization allows providing a custom implementation for a specific data type, overriding the generic template behavior for that particular type.
What is the correct syntax for full template specialization?
Given: template <class T> class MyClass { ... };Show Answer & Explanation
Correct Answer: A - template <> class MyClass<int> { ... };
Full template specialization uses 'template <>' before the class definition with the specific type in angle brackets: template <> class MyClass<int> { ... };
Show Answer & Explanation
Correct Answer: A - Standard Template Library
STL stands for Standard Template Library. It provides a collection of template-based containers (vector, list, map), algorithms (sort, find), and iterators.
Show Answer & Explanation
Correct Answer: B - template
'template' is a C++ keyword, not an STL container. STL containers include vector, list, deque, set, map, array, unordered_map, etc.
What will be the output?
template <typename T>
T add(T a, T b) { return a + b; }
int main() {
cout << add(3.5, 2.5);
}Show Answer & Explanation
Correct Answer: C - 6
The template deduces T as double. add(3.5, 2.5) returns 6.0. When printed with cout, 6.0 displays as '6' by default (no trailing zeros).
Show Answer & Explanation
Correct Answer: D - Yes, using comma-separated parameters
Templates can have multiple type parameters separated by commas: template <class T1, class T2>. This allows functions and classes to work with two or more independent types.
What will be the output?
template <class T1, class T2>
void display(T1 a, T2 b) {
cout << a << " " << b;
}
int main() {
display(10, 3.14);
}Show Answer & Explanation
Correct Answer: C - 10 3.14
The template has two type parameters. T1 is deduced as int (10) and T2 as double (3.14). Output: '10 3.14'.
Show Answer & Explanation
Correct Answer: C - A template parameter that is a value (like int) instead of a type
Non-type template parameters are constant values (like int, char, pointer) passed to templates. Example: template <class T, int SIZE> class Array { T arr[SIZE]; };
What will be the output?
template <class T, int N>
class FixedArray {
T arr[N];
public:
int size() { return N; }
};
int main() {
FixedArray<int, 5> a;
cout << a.size();
}Show Answer & Explanation
Correct Answer: B - 5
The non-type parameter N is set to 5 at compile time. The size() function returns N which is 5.
Show Answer & Explanation
Correct Answer: A - A dynamic array that can grow and shrink
std::vector is a dynamic array container in STL that can automatically resize. It provides random access, push_back, pop_back, and other operations.
What will be the output?
#include <vector>
int main() {
vector<int> v = {1, 2, 3, 4, 5};
cout << v.size() << " " << v[2];
}Show Answer & Explanation
Correct Answer: A - 5 3
v has 5 elements, so v.size() = 5. v[2] accesses the third element (0-indexed) which is 3. Output: '5 3'.
Show Answer & Explanation
Correct Answer: A - map
std::map stores key-value pairs with unique keys in sorted order (by key). unordered_map stores them without sorting but with O(1) average lookup.
Show Answer & Explanation
Correct Answer: D - An object that points to elements in a container and allows traversal
An iterator is an object that acts like a pointer, providing a way to access and traverse elements in STL containers. It abstracts the internal structure of the container.
What will be the output?
#include <map>
int main() {
map<string, int> m;
m["apple"] = 5;
m["banana"] = 3;
m["apple"] = 8;
cout << m["apple"] << " " << m.size();
}Show Answer & Explanation
Correct Answer: A - 8 2
map has unique keys. m["apple"] = 8 overwrites the previous value 5. The map has 2 entries (apple and banana). Output: '8 2'.
Show Answer & Explanation
Correct Answer: A - The compiler automatically determining template type parameters from function arguments
Template argument deduction is the process where the compiler automatically determines the template type parameters from the types of the function arguments, so you don't need to specify them explicitly.
What will cause a compilation error?
template <typename T>
T multiply(T a, T b) { return a * b; }Show Answer & Explanation
Correct Answer: A - multiply(3, 2.5)
multiply(3, 2.5) fails because 3 is int and 2.5 is double. The compiler cannot deduce a single T for both arguments. You'd need multiply<double>(3, 2.5) to resolve this.
Show Answer & Explanation
Correct Answer: D - Specializing a template for a subset of possible parameters
Partial template specialization provides a specialized implementation for a subset of template parameters. For example, specializing a template for all pointer types while keeping the element type generic.
Show Answer & Explanation
Correct Answer: D - stack
std::stack provides LIFO access with push(), pop(), and top() operations. std::queue provides FIFO access. Both are container adaptors built on top of other containers.
What will be the output?
#include <stack>
int main() {
stack<int> s;
s.push(10);
s.push(20);
s.push(30);
cout << s.top() << " ";
s.pop();
cout << s.top();
}Show Answer & Explanation
Correct Answer: C - 30 20
After pushing 10, 20, 30: top() returns 30. After pop() removes 30: top() returns 20. Output: '30 20'.
Show Answer & Explanation
Correct Answer: A - set stores elements in sorted order; unordered_set uses hashing for O(1) average lookup
std::set stores unique elements in sorted order using a balanced BST (O(log n) operations). std::unordered_set uses hash tables for O(1) average operations without ordering.
What will be the output?
template <typename T>
void swap(T& a, T& b) {
T temp = a;
a = b;
b = temp;
}
int main() {
int x = 5, y = 10;
swap(x, y);
cout << x << " " << y;
}Show Answer & Explanation
Correct Answer: B - 10 5
The template swap function exchanges the values of x and y using a temporary variable. After swap, x = 10 and y = 5.
Show Answer & Explanation
Correct Answer: A - Templates reduce code duplication for type-independent logic
Templates allow writing a single function/class that works with multiple types, eliminating the need to write separate overloaded functions for each type. This reduces code duplication significantly.
Show Answer & Explanation
Correct Answer: A - The process of generating a specific function or class from a template for a particular type
Template instantiation is the process where the compiler generates a specific version of a template function or class for a particular type (e.g., vector<int> instantiates the vector template with int).
Show Answer & Explanation
Correct Answer: C - std::sort
std::sort (from <algorithm>) sorts elements in a range. By default it sorts in ascending order, but a custom comparator can be provided for different ordering.