Home »
Aptitude Questions and Answers »
C++ Aptitude Questions and Answers
C++ Template Aptitude Questions and Answers
C++ Template Aptitude: This section contains C++ Template Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on February 20, 2021
1) There are the following statements that are given below, which of them are correct about templates in C++?
- The template is a new feature in C++; templates do not exist in C language.
- The template is used for generic programming.
- The template is a special class which used to store data elements.
- All the above
Options:
- A and B
- A and C
- B and C
- D
Correct Answer - 1
A and B
Explanation:
Both A and B statements are correct about containers in C++.
2) We can implement templates as a?
- Class templates
- Function templates
- Enumeration templates
- Pointer templates
Options:
- A and B
- A and C
- B, C, and D
- A, B, C, and D
Correct Answer - 1
A and B
Explanation:
Options A and B are correct about templates in C++.
3) There are following options are given below, which of them is correct syntax of function templates in C++?
- templates <class type> return type function_name(parameter_list) { }
- Template <class type> return type function_name(parameter_list) { }
- Templates <class type> return type function_name(parameter_list) { }
- template <class type> return type function_name(parameter_list) { }
Correct Answer - 4
template <class type> return type function_name(parameter_list) { }
Explanation:
The 4th option is the correct syntax of function templates in C++.
4) What is the correct output of given code snippets in C++?
#include <iostream>
using namespace std;
template <class T>
T sum(T num1, T num2)
{
T C = num1 + num2;
return C;
}
int main()
{
int A = 5;
int B = 10;
double X = 3.5;
double Y = 4.6;
cout << sum(A, B) << " " << sum(X, Y) << endl;
return 0;
}
Options:
- 15 8.1
- 15 8
- Garbage values
- Syntax error
Correct Answer - 1
15 8.1
Explanation:
The above code will print "15 8.1" on the console screen.
5) What is the correct output of given code snippets in C++?
#include <iostream>
using namespace std;
template <class T>
int sum(T num1, T num2)
{
T C = num1 + num2;
return C;
}
int main()
{
int A = 5;
int B = 10;
double X = 3.5;
double Y = 4.6;
cout << sum(A, B) << " " << sum(X, Y) << endl;
return 0;
}
Options:
- 15 8.1
- 15 8
- Garbage values
- Syntax error
Correct Answer - 2
15 8
Explanation:
The above code will print "15 8" on the console screen.
6) What is the correct output of given code snippets in C++?
#include <iostream>
using namespace std;
template <class T1>
int sum(T1 num1, T1 num2)
{
T1 C = num1 + num2;
return C;
}
int main()
{
int A = 5;
int B = 10;
double X = 3.5;
double Y = 4.6;
cout << sum(A, B) << " " << sum(X, Y) << endl;
return 0;
}
Options:
- 15 8.1
- 15 8
- Garbage values
- Syntax error
Correct Answer - 2
15 8
Explanation:
The above code will print "15 8" on the console screen.
7) What is the correct output of given code snippets in C++?
#include <iostream>
using namespace std;
<class T> template int sum(T num1, T num2)
{
T C = num1 + num2;
return C;
}
int main()
{
int A = 5;
int B = 10;
double X = 3.5;
double Y = 4.6;
cout << sum(A, B) << " " << sum(X, Y) << endl;
return 0;
}
Options:
- 15 8.1
- 15 8
- Garbage values
- Syntax error
Correct Answer - 4
Syntax error
Explanation:
The above code will generate a syntax error.
8) What is the correct output of given code snippets in C++?
#include <iostream>
using namespace std;
template <class X, class Y>
Y sum(X num1, Y num2)
{
Y C = num1 + num2;
return C;
}
int main()
{
int A = 5;
int B = 10;
double X = 3.5;
double Y = 4.6;
cout << sum(A, Y) << endl;
return 0;
}
Options:
- 9
- 9.6
- Garbage values
- Syntax error
Correct Answer - 2
9.6
Explanation:
The above code will print "9.6" on the console screen.
9) Can we overload template functions in C++?
- Yes
- No
Correct Answer - 1
Yes
Explanation:
Yes, we can overload template functions in C++.
10) What is the correct output of given code snippets in C++?
#include <iostream>
using namespace std;
class template <class T>
Sample
{
T a;
T b;
public:
Sample(T a, T b)
{
this->a = a;
this->b = b;
}
void print()
{
cout << a << " " << b << endl;
}
};
int main()
{
Sample<int> S(10, 20);
S.print();
return 0;
}
Options:
- 10 20
- Garbage values
- Syntax error
- Runtime exception
Correct Answer - 3
Syntax error
Explanation:
The above code will generate a syntax error.
11) What is the correct output of given code snippets in C++?
#include <iostream>
using namespace std;
template <class T>
class Sample {
T a;
T b;
public:
Sample(T a, T b)
{
this->a = a;
this->b = b;
}
void print()
{
cout << a << " " << b << endl;
}
};
int main()
{
Sample<int> S(10, 20);
S.print();
return 0;
}
Options:
- 10 20
- Garbage values
- Syntax error
- Runtime exception
Correct Answer - 1
10 20
Explanation:
The above code will print "10 20" on the console screen.
12) What is the correct output of given code snippets in C++?
#include <iostream>
using namespace std;
template <class T, int b>
class Sample {
T a;
public:
Sample(T a)
{
this->a = a;
}
void print()
{
cout << a << " " << b << endl;
}
};
int main()
{
Sample<int, 20> S(10);
S.print();
return 0;
}
Options:
- 10 20
- Garbage values
- Syntax error
- Runtime exception
Correct Answer - 1
10 20
Explanation:
The above code will print "10 20" on the console screen.