C++ Programming MCQ - Templates
11. What is the output of this program?
#include <iostream>
using namespace std;
template <typename T>
T max (T& p, T& q)
{
return (p>q?p:q);
}
int main ()
{
int x = 55, y = 60, m;
long a = 105, b = 53, n;
m = max(x, y);
n = max(a, b);
cout << m << endl;
cout << n << endl;
return 0;
}
View Answer
12. What is the output of this program?
#include <iostream>
using namespace std;
template <typename T>
void loopIt(T x)
{ int num=3;
T lfc[num];
for(int p = 0; p < num; p++)
{
lfc[p] = x++;
cout << lfc[p] << endl;
}
};
int main()
{
float q = 2.1;
loopIt(q);
}
View Answer
13. What is the output of this program?
#include <iostream>
using namespace std;
template <typename T>
T maximum(T x, T y)
{
return (x > y)? x : y;
}
int main()
{
cout << maximum(3, 7) << std::endl;
cout << maximum(3.0, 7.0) << std::endl;
cout << maximum(3, 7.0) << std::endl;
return 0;
}
View Answer
14. Which of the following statement is correct about the program given below?
#include <iostream>
using namespace std;
template <class T, class U>
class A {
T x;
U y;
static int count;
};
int main() {
A<char, char> p;
A<int, int> q;
cout << sizeof(p) << endl;
cout << sizeof(q) << endl;
return 0;
}
View Answer
15. Which of the following is true about the following program
#include <iostream>
using namespace std;
template <class P, class Q, class R>
class A {
P x;
Q y;
R z;
static int count;
};
int main()
{
A<int, int, int> m;
A<char, char, char> n;
cout << sizeof(m) << endl;
cout << sizeof(n) << endl;
return 0;
}
View Answer
16. What will be the output of this program?
#include <iostream>
using namespace std;
template <int i>
void fun()
{
i = 20;
cout << i;
}
int main()
{
fun<10>();
return 0;
}
View Answer
17. What will be the output of this program?
#include <iostream>
using namespace std;
template<int n> struct funStruct
{
static const int val = 2*funStruct<n-1>::val;
};
template<> struct funStruct<0>
{
static const int val = 1 ;
};
int main()
{
cout << funStruct<10>::val << endl;
return 0;
}
View Answer
18. What will be the output of the following program?
Note:Includes all required header files
using namespace std;
template < typename T >
void print_mydata(T find)
{
cout << find << endl;
}
int main()
{
double p = 17.5;
string s("Hi to all");
print_mydata( p );
print_mydata( s );
return 0;
}
View Answer
19. How many bits of memory needed for internal representation of class?
View Answer
20. What can be passed by non-type template parameters during compile time?
View Answer
Also check :