Back to Practice OOP with C++

Templates & STL - Practice MCQs for CCAT

50 Questions Section B: Programming OOP with C++

Templates & STL Question Bank for C-CAT

Topic-wise Templates & STL MCQs for CDAC C-CAT preparation with answers and explanations.

Q1.
Template provides:
ARuntime polymorphism
BGeneric programming
CMultiple inheritance
DEncapsulation
Show Answer & Explanation

Correct Answer: B - Generic programming

Templates enable generic programming - write once, use with any type.

Q2.
Function template starts with:
Ageneric
Btype T
Ctemplate<typename T>
Dclass T
Show Answer & Explanation

Correct Answer: C - template<typename T>

Template syntax: template<typename T> or template<class T>.

Q3.
STL stands for:
ASimple Template Library
BSystem Template Library
CStandard Template Library
DStatic Type Library
Show Answer & Explanation

Correct Answer: C - Standard Template Library

STL is Standard Template Library.

Q4.
Which is NOT an STL container?
Avector
Bprintf
Carray
Dlist
Show Answer & Explanation

Correct Answer: B - printf

printf is a C function, not an STL container.

Q5.
vector in STL is:
AStatic array
BDynamic array
CLinked list
DStack
Show Answer & Explanation

Correct Answer: B - Dynamic array

vector is a dynamic array that can grow in size.

Q6.
map in STL stores:
AOnly keys
BKey-value pairs
COnly values
DIntegers only
Show Answer & Explanation

Correct Answer: B - Key-value pairs

map stores key-value pairs with unique sorted keys.

Q7.
Iterator is used to:
ADelete containers
BTraverse containers
CCreate containers
DSort only
Show Answer & Explanation

Correct Answer: B - Traverse containers

Iterators are used to traverse STL containers.

Q8.
set in STL:
AAllows duplicates
BUses key-value
CIs unsorted
DStores unique sorted elements
Show Answer & Explanation

Correct Answer: D - Stores unique sorted elements

set stores unique elements in sorted order.

Q9.
stack in STL uses:
AFIFO
BLIFO
CRandom
DPriority
Show Answer & Explanation

Correct Answer: B - LIFO

STL stack follows LIFO (Last In First Out).

Q10.
Template specialization is:
AGeneric version
BNot allowed
CError
DSpecific implementation for a type
Show Answer & Explanation

Correct Answer: D - Specific implementation for a type

Template specialization provides specific implementation for particular types.

Q11.
STL set stores elements in:
AInsertion order
BSorted order
CRandom order
DReverse order
Show Answer & Explanation

Correct Answer: B - Sorted order

STL set stores unique elements in sorted order using balanced BST (usually Red-Black tree).

Q12.
STL map stores:
AOnly values
BUnsorted pairs
COnly keys
DKey-value pairs in sorted order
Show Answer & Explanation

Correct Answer: D - Key-value pairs in sorted order

STL map stores key-value pairs sorted by key, with unique keys.

Q13.
unordered_map uses:
ABinary tree
BLinked list
CHash table
DArray
Show Answer & Explanation

Correct Answer: C - Hash table

unordered_map uses hash table for O(1) average access time.

Q14.
STL deque allows:
AOnly front insertion
BInsertion at both ends efficiently
COnly back insertion
DNo insertion
Show Answer & Explanation

Correct Answer: B - Insertion at both ends efficiently

Deque (double-ended queue) allows efficient insertion/deletion at both front and back.

Q15.
STL priority_queue default order:
AMin-heap (smallest first)
BFIFO
CMax-heap (largest first)
DLIFO
Show Answer & Explanation

Correct Answer: C - Max-heap (largest first)

STL priority_queue is a max-heap by default; largest element has highest priority.

Q16.
Iterators in STL are:
AInteger indices
BFunction pointers
CArray names
DGeneralized pointers to elements
Show Answer & Explanation

Correct Answer: D - Generalized pointers to elements

Iterators are objects that point to elements in containers, providing uniform access.

Q17.
STL algorithm sort() complexity:
AO(n)
BO(n log n)
CO(n²)
DO(log n)
Show Answer & Explanation

Correct Answer: B - O(n log n)

STL sort() uses Introsort with O(n log n) average and worst-case complexity.

Q18.
Template parameter pack (variadic templates):
AVariable number of template parameters
BFixed number of parameters
CNo parameters
DOnly type parameters
Show Answer & Explanation

Correct Answer: A - Variable number of template parameters

Variadic templates accept variable number of template arguments using ... syntax.

Q19.
STL list vs vector for insertion:
AList faster for middle insertion
BVector faster everywhere
CSame performance
DNeither supports insertion
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.

Q20.
auto keyword with STL:
ACreates automatic variable
BMakes variable constant
CDeduces type automatically
DCreates global variable
Show Answer & Explanation

Correct Answer: C - Deduces type automatically

auto keyword deduces type from initializer, simplifying iterator declarations.

Q21.
What is a function template in C++?
AA function that takes no parameters
BA predefined library function
CA function declared inside a template class
DA blueprint for creating functions that work with different data types
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.

Q22.
What is the correct syntax for a function template?
Afunction<T> func(T a) {}
BT template func(T a) {}
Ctemplate <class T> T func(T a) {}
Dgeneric T func(T a) {}
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.

Q23.

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);
}
A10
B20
CCompilation error
D0
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.

Q24.
What is the difference between 'class' and 'typename' in template declarations?
A'class' is for classes; 'typename' is for built-in types
B'class' cannot be used with templates
C'typename' is deprecated
DThey are interchangeable in most contexts
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.

Q25.
What is a class template?
AA class that templates cannot use
BA class defined inside a function
CA blueprint for creating classes that work with different data types
DA class with only static members
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.

Q26.

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();
}
A15
B0
CCompilation error
D25
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.

Q27.
What is template specialization?
ACreating a generic template
BUsing templates with pointers only
CInheriting from a template class
DProviding a specific implementation for a particular data type
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.

Q28.

What is the correct syntax for full template specialization?

Given: template <class T> class MyClass { ... };
Atemplate <> class MyClass<int> { ... };
Btemplate <int> class MyClass { ... };
Cspecialize class MyClass<int> { ... };
Dclass MyClass<int> { ... };
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> { ... };

Q29.
What is the STL in C++?
AStandard Template Library
BStandard Type Library
CStatic Template Loader
DSimple Type Layout
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.

Q30.
Which of the following is NOT an STL container?
Avector
Btemplate
Carray
Dmap
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.

Q31.

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);
}
A5
B6.0
C6
DCompilation error
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).

Q32.
Can a template have multiple type parameters?
ANo, only one type parameter is allowed
BOnly with specialization
COnly class templates can have multiple parameters
DYes, using comma-separated parameters
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.

Q33.

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);
}
ACompilation error
B10 3
C10 3.14
D3.14 10
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'.

Q34.
What is a non-type template parameter?
AA template parameter that cannot be used
BA template without parameters
CA template parameter that is a value (like int) instead of a type
DA template parameter that is always void
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]; };

Q35.

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();
}
A0
B5
CCompilation error
DGarbage value
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.

Q36.
What is the purpose of std::vector in STL?
AA dynamic array that can grow and shrink
BA fixed-size array
CA linked list
DA binary tree
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.

Q37.

What will be the output?

#include <vector>
int main() {
  vector<int> v = {1, 2, 3, 4, 5};
  cout << v.size() << " " << v[2];
}
A5 3
B5 2
C4 3
D4 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'.

Q38.
Which STL container stores key-value pairs with unique keys in sorted order?
Amap
Bset
Cvector
Dlist
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.

Q39.
What is an iterator in STL?
AA type of loop
BA template parameter
CA sorting algorithm
DAn object that points to elements in a container and allows traversal
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.

Q40.

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();
}
A8 2
B5 3
C5 2
D8 3
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'.

Q41.
What is template argument deduction?
AThe compiler automatically determining template type parameters from function arguments
BManually specifying template arguments
CRemoving template arguments
DConverting template to non-template
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.

Q42.

What will cause a compilation error?

template <typename T>
T multiply(T a, T b) { return a * b; }
Amultiply(3, 2.5)
Bmultiply(2.5, 3.5)
Cmultiply(3, 4)
Dmultiply('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.

Q43.
What is partial template specialization?
ASpecializing a template for one specific type
BAn incomplete template
CA template with no parameters
DSpecializing a template for a subset of possible parameters
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.

Q44.
Which STL container provides LIFO (Last In First Out) access?
Aqueue
Bvector
Cdeque
Dstack
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.

Q45.

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();
}
A10 20
B30 10
C30 20
D20 10
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'.

Q46.
What is the difference between std::set and std::unordered_set?
Aset stores elements in sorted order; unordered_set uses hashing for O(1) average lookup
Bset uses hashing; unordered_set uses trees
CThey are identical
Dset allows duplicates; unordered_set doesn't
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.

Q47.

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;
}
A5 10
B10 5
CCompilation error
D10 10
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.

Q48.
What is the advantage of templates over function overloading?
ATemplates reduce code duplication for type-independent logic
BTemplates are faster at runtime
CTemplates use less memory
DTemplates cannot cause errors
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.

Q49.
What is template instantiation?
AThe process of generating a specific function or class from a template for a particular type
BDeclaring a template
CDestroying a template object
DInheriting from a template
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).

Q50.
Which STL algorithm is used to sort elements in a container?
Astd::find
Bstd::search
Cstd::sort
Dstd::order
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.

Showing 1-10 of 50 questions