A template function must have at least ________ generic data type.
- AZero
- BOne
- CTwo
- DThree
A function template needs at least one template type parameter.
MCQs, short and long questions from lecture 41.Lecture 41 ki MCQs, short aur long sawal.
6 MCQs · 1 short · 4 long questions · 6 MCQs from past papers6 MCQs · 1 short · 4 long sawal · 6 MCQs past papers se
A template function must have at least ________ generic data type.
A function template needs at least one template type parameter.
With template function, the compiler automatically detects the ______ and generates ______ using passed data.
The compiler checks the type of the passed data and generates a new instance of the function for it.
With template function, the compiler automatically detects the passed data and generates a new copy of function using passed data.
The compiler deduces the argument types and instantiates a matching function.
A template function can have different type of arguments.
A function template may use several generic types and ordinary types as parameters.
A template function must have only generic data types.
A function template can mix generic and ordinary parameter types.
The template functions do NOT promote the code reuse
Templates let one definition work for many types, which promotes reuse.
No MCQs match this filter.Is filter mein koi MCQ nahi.
What will be the output of following function if we call this function by passing int 5? template <class T> T reciprocal(T x) { return (1/x); }
0. T is deduced as int, so 1/x is integer division: 1/5 = 0 (the real value 0.2 is truncated), and the function returns int 0.
Write a program which defines a Template function named Add () which adds two variables and then returns the sum. Define two variables of type int in main function, and then call Add () function on these variables. Program output should look like this: Enter two integer values to be added Enter First value: 12 Enter Second value: 25 Addition of two variable of different data type Sum = 37
#include <iostream> using namespace std; template <class T> T Add(T a, T b) { return a + b; } int main() { int x, y; cout << "Enter two integer values to be added" << endl; cout << "Enter First value:" << endl; cin >> x; cout << "Enter Second value:" << endl; cin >> y; cout << "Addition of two variable of different data type" << endl; cout << "Sum = " << Add(x, y) << endl; return 0; }
Write a program that defines a Template function named Square() which finds the square of a number and then return it. Define one variable of type float in main function and then call Square function on that. Program output should look like this: Enter a float value to find its Square: 12.3 Square of float number is: 151.29
#include <iostream> using namespace std; template <class T> T Square(T num) { return num * num; } int main() { float value; cout << "Enter a float value to find its Square: "; cin >> value; cout << "Square of float number is: " << Square(value) << endl; return 0; }
Write a program that defines a Template function named Square() which finds the square of a number and then return it. Define one variable of type double in main function and then call Square function on that. Program output should look like this: Enter a double value to find its Square: 235.3 Square of double number is: 55366.1
#include <iostream> using namespace std; template <class T> T Square(T num) { return num * num; } int main() { double d; cout << "Enter a double value to find its Square: "; cin >> d; cout << "Square of double number is: " << Square(d) << endl; return 0; }
Write a program that uses a function template called min to determine the smaller of two arguments. Test the program using integer, character and floating point number pairs in main ().
#include <iostream> using namespace std; template <class T> T min(T a, T b) { return (a < b) ? a : b; } int main() { cout << "min(10, 4) = " << ::min(10, 4) << endl; cout << "min('x', 'c') = " << ::min('x', 'c') << endl; cout << "min(2.5, 7.25) = " << ::min(2.5, 7.25) << endl; return 0; }