PRACTICE • LEARN • GROW
Module 6 Final Quiz: Templates & Generic Programming MCQs
Solve topic-wise placement questions and improve your skills.
Module 6 Final Quiz: Templates & Generic Programming Progress
0 of 12 completed
0%
Topic Track
You are viewing MCQs in preview mode. Log in to select options, submit answers, and track your progress.
Log In to Answer
1
What is the primary benefit of templates in C++?
A.
They allow running Python code inside C++
B.
They allow writing code once that works with any data type with zero runtime overhead
C.
They disable all type checking at compile-time
D.
They reduce the size of the compiled binary executable
Explanation & Key
Correct Answer:
2
When does the C++ compiler instantiate the code for a templated function?
A.
At program startup
B.
At compile time, when the template is called with specific concrete types
C.
At runtime, when the function is first executed
D.
Never; templates are interpreted
Explanation & Key
Correct Answer:
3
In the template header 'template ', what is 'T'?
A.
A global integer variable
B.
A type placeholder conventionally used to represent whatever type is passed
C.
A pointer to a thread
D.
A macro constant
Explanation & Key
Correct Answer:
4
What is the difference between 'template ' and 'template ' in C++?
A.
typename only accepts primitive types; class only accepts custom classes
B.
There is no semantic difference in template parameter declarations; they are completely interchangeable
C.
class is faster than typename
D.
typename was deprecated in C++11
Explanation & Key
Correct Answer:
5
How does std::vector support storing different types such as vector and vector?
A.
By using void* void pointers internally
B.
Because std::vector is implemented as a Class Template
C.
Through runtime dynamic type reflection
D.
By storing ASCII character strings of numbers
Explanation & Key
Correct Answer:
6
Given: template T add(T a, T b) { return a + b; }
What happens if you invoke: add(5, 2.5)?
A.
It returns 7.5 automatically
B.
Compilation fails with a template argument deduction mismatch error because T cannot be both int and double
C.
It rounds 2.5 to 2 and returns 7
D.
It returns 0
Explanation & Key
Correct Answer:
7
What is Template Specialization in C++?
A.
Deleting unused templates
B.
Providing a customized implementation of a template for a specific data type (e.g. Specialized vector)
C.
Restricting templates to 32-bit platforms
D.
Converting templates into virtual functions
Explanation & Key
Correct Answer:
8
Why are template declarations and definitions usually placed in header (.h/.hpp) files rather than separate .cpp files?
A.
Because .cpp files cannot contain C++ code
B.
Because the compiler must see the full template definition in every translation unit where it is instantiated with concrete types
C.
To save disk space
D.
Because the linker does not support templates
Explanation & Key
Correct Answer:
9
What is a non-type template parameter?
A.
A parameter that has no type
B.
A template parameter that represents a constant compile-time value rather than a type (such as template class Array)
C.
A pointer passed at runtime
D.
An invalid syntax error
Explanation & Key
Correct Answer:
10
What is the output of the following code?
template
T printVal(T val) { return val; }
cout << printVal(65);
A.
65
B.
'A'
C.
Compilation Error
D.
0
Explanation & Key
Correct Answer:
11
How does the C++ Standard Template Library achieve zero runtime abstraction overhead?
A.
By compiling templates ahead of time into machine code for all possible data types
B.
Because templates are inlined and specialized by the compiler, allowing the optimizer to generate code as efficient as hand-written assembly
C.
By running on dedicated coprocessors
D.
By using Java JIT techniques
Explanation & Key
Correct Answer:
12
In C++20, what feature was introduced to enforce constraints and requirements on template parameters?
A.
Modules
B.
Concepts
C.
Coroutines
D.
Ranges
Explanation & Key
Correct Answer:
Question Palette
Answered
Current
Not Attempted
Wrong