Q.1 What is the main purpose of using templates in C++?
To reduce memory usage
To write type-independent code
To improve runtime speed only
To enforce type safety at runtime
Explanation - Templates allow writing functions and classes that can work with any data type without code duplication.
Correct answer is: To write type-independent code
Q.2 Which keyword is used to define a template in C++?
define
template
typename
generic
Explanation - The 'template' keyword is used in C++ to define generic templates for functions and classes.
Correct answer is: template
Q.3 In C++ templates, what does the keyword 'typename' represent?
It defines a function name
It specifies a type parameter
It indicates a variable name
It creates a new namespace
Explanation - 'typename' tells the compiler that the argument passed to the template is a type.
Correct answer is: It specifies a type parameter
Q.4 Which of the following is true about function templates?
They can only accept int types
They allow multiple data types without rewriting code
They are slower than normal functions
They cannot be overloaded
Explanation - Function templates allow defining one function that works with different data types, avoiding duplication.
Correct answer is: They allow multiple data types without rewriting code
Q.5 Which feature in Java is most similar to C++ templates?
Abstract classes
Interfaces
Generics
Annotations
Explanation - Java generics serve a similar purpose to C++ templates, enabling type parameterization.
Correct answer is: Generics
Q.6 What is a major difference between Java generics and C++ templates?
Generics are faster
Templates use type erasure
Generics use type erasure
Templates only work with classes
Explanation - Java generics use type erasure at compile-time, while C++ templates generate separate code for each type.
Correct answer is: Generics use type erasure
Q.7 Which of the following is a valid syntax for a function template in C++?
template <typename T> T func(T a, T b);
template function<T>(T a, T b);
generic <T> T func(T a, T b);
template function func<T>(T a, T b);
Explanation - Correct syntax requires 'template <typename T>' before the function definition.
Correct answer is: template <typename T> T func(T a, T b);
Q.8 In C++, what happens when you instantiate a template with a type?
Compiler generates a specialized version of the code
The program uses a generic runtime function
The type is ignored at compile-time
The code is not compiled until runtime
Explanation - C++ templates are resolved at compile-time, generating specific functions or classes for the type used.
Correct answer is: Compiler generates a specialized version of the code
Q.9 Which statement about class templates is correct?
They allow defining multiple classes with the same logic but different data types
They cannot have constructors
They cannot have member functions
They only support primitive types
Explanation - Class templates let you create a generic blueprint for classes that can handle multiple types.
Correct answer is: They allow defining multiple classes with the same logic but different data types
Q.10 In C++ template specialization, what does it mean?
Overloading a function with templates
Providing a custom implementation for a specific data type
Using default arguments in templates
Limiting templates to primitive types only
Explanation - Template specialization allows writing specific behavior for certain data types while using templates.
Correct answer is: Providing a custom implementation for a specific data type
Q.11 Which is an example of a non-type template parameter in C++?
template <int N> class Array { ... }
template <typename T> class Array { ... }
template <float F> class Array { ... }
template <string S> class Array { ... }
Explanation - Non-type template parameters allow passing constants like integers to templates.
Correct answer is: template <int N> class Array { ... }
Q.12 Why are templates considered compile-time polymorphism?
Because they generate code during compilation
Because they use virtual tables
Because they replace dynamic casting
Because they avoid function overloading
Explanation - Templates resolve types at compile-time, making them an example of compile-time polymorphism.
Correct answer is: Because they generate code during compilation
Q.13 Which keyword can be used instead of 'typename' in C++ templates?
generic
class
auto
using
Explanation - Both 'typename' and 'class' can be used to declare type parameters in C++ templates.
Correct answer is: class
Q.14 What is a potential drawback of C++ templates compared to Java generics?
They cannot work with primitive types
They cause code bloat due to multiple instantiations
They are slower at runtime
They are limited to functions only
Explanation - Since C++ generates separate versions of the code for each type, it may increase binary size.
Correct answer is: They cause code bloat due to multiple instantiations
Q.15 Which of the following is NOT allowed as a non-type template parameter?
Integer
Pointer
Float
Reference
Explanation - Non-type template parameters in C++ cannot be floating-point or class objects.
Correct answer is: Float
Q.16 In Java generics, what does <T> represent?
A template keyword
A placeholder for a type parameter
A reference to a class object
A runtime cast operator
Explanation - <T> is used in Java generics to denote a type parameter.
Correct answer is: A placeholder for a type parameter
Q.17 Which of the following is an example of generic class in Java?
class Box<T> { T value; }
generic class Box(type T)
template class Box(T)
Box<T> = new Box(type)
Explanation - In Java, generics are defined using angle brackets with type parameters, like <T>.
Correct answer is: class Box<T> { T value; }
Q.18 What is type erasure in Java generics?
Removing unused types at compile time
Replacing generic types with Object at compile time
Erasing class definitions after execution
Hiding type parameters at runtime for security
Explanation - Java uses type erasure to ensure backward compatibility, converting generics into raw types.
Correct answer is: Replacing generic types with Object at compile time
Q.19 In C++ templates, which type of parameter allows passing an integer?
Type parameter
Non-type parameter
Reference parameter
Pointer parameter
Explanation - Non-type template parameters allow compile-time constants such as integers.
Correct answer is: Non-type parameter
Q.20 What happens if you use a type in a template that does not support required operations?
Compiler gives an error at runtime
Compiler substitutes default operations
Compiler gives an error at compile time
The program executes with warnings
Explanation - C++ checks template instantiations at compile time and errors if unsupported operations are used.
Correct answer is: Compiler gives an error at compile time
Q.21 Which feature of C++ templates allows partial control over certain types?
Partial specialization
Type erasure
Overloading
Polymorphism
Explanation - Partial specialization allows customizing a template for a subset of parameter types.
Correct answer is: Partial specialization
Q.22 Which statement about generic methods in Java is true?
They can have their own type parameters independent of class generics
They cannot be static
They must always use <T> as a parameter name
They only work inside generic classes
Explanation - Generic methods can declare type parameters separately from the generic class.
Correct answer is: They can have their own type parameters independent of class generics
Q.23 Which is an example of a bounded type parameter in Java?
<T extends Number>
<T super Number>
<T implements Number>
<T subclass Number>
Explanation - Java generics use 'extends' to define an upper bound for type parameters.
Correct answer is: <T extends Number>
Q.24 What does STL in C++ stand for, and how does it use templates?
Standard Template Library; uses templates to create generic containers
System Template Library; defines system-level functions
Standard Type Library; defines new primitive types
Storage Template Library; manages file storage
Explanation - The STL uses templates extensively to provide generic data structures and algorithms.
Correct answer is: Standard Template Library; uses templates to create generic containers
