Explore topic-wise MCQs in C++ Programming.

This section includes 28 Mcqs, each offering curated multiple-choice questions to sharpen your C++ Programming knowledge and support exam preparation. Choose a topic below to get started.

1.

What is the output of this program?
#include <iostream>
using namespace std;
template<typename type>
class Test
{
public:
virtual type Function(type num1)
{
return num1 * 2;
}
};
int main()
{
Test<int> num1;
cout << num1.Function(150) << endl;
return 0;
}

A. 2
B. 150
C. Compilation Error
D. 300
E. Runtime Error
Answer» E. Runtime Error
2.

What is the output of this program?
#include <iostream>
using namespace std;
template <class type>
class Sample
{
public:
Sample()
{
};
~Sample()
{
};
type FunA(type num1)
{
return num1;
}
type FunB(type num2)
{
return num2;
}
};
int main()
{
Sample<int> num1;
Sample<double> num2;
cout << num1.FunA(250) << " ";
cout << num2.FunB(5.225);
return 0;
}

A. 250
B. 5.225
C. 5.225 250
D. 250 5.225
E. None of these
Answer» E. None of these
3.

What is the output of this program?
#include <iostream>
using namespace std;
template <typename T = float, int count = 3>
T Multiply(T num1)
{
for(int kk = 0; kk < count; kk++)
{
num1 = num1 * num1;
}
return num1;
};
int main()
{
float num2 = 1.25;
cout << num2 << " " << Multiply<>(num2) << endl;
}

A. 1.25
B. 5.96046
C. 1.25 5.96046
D. 5.96046 1.25
E. None of these
Answer» E. None of these
4.

What is the output of this program?
#include <iostream>
using namespace std;
template <class T>
inline T square(T n1)
{
T Output;
Output = n1 * n1;
return Output;
};
template <>
string square<string>(string Str)
{
return (Str + Str);
};
int main()
{
int k = 4, kk;
string bb("B");
kk = square<int>(k);
cout << k << ": " << kk;
cout << square<string>(bb) << ":" << endl;
}

A. 4:
B. 4: 16BB:
C. 16BB:
D. 16BB: 4:
E. None of these
Answer» C. 16BB:
5.

What is the output of this program?
#include <iostream>
using namespace std;
template<class T = float, int k = 5> class N
{
public:
N();
int value;
};
template<> class N<>
{
public: N();
};
template<> class N<double, 10>
{
public: N();
};
template<class T, int k> N<T, k>::N() : value(k)
{
cout << value;
}
N<>::N()
{
cout << " Out of ";
}
N<double, 10>::N()
{
cout << "10" << endl;
}
int main()
{
N<int, 8> x;
N<> y;
N<double, 10> z;
}

A. 10 out of 8
B. 8
C. 10
D. 8 out of 10
E. None of these
Answer» E. None of these
6.

What is the output of this program?
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
template <class type>
type Maximum(const type value1, const type value2)
{
cout << "No Specialization";
return value1 < value2 ? value2 : value1;
}
template <>
const char *Maximum(const char *value1, const char *value2)
{
return (strcmp(value1, value2) < 0) ? value2 : value1;
}
int main()
{
string S1 = "class", S2 = "template";
const char *value3 = "Interveiw";
const char *value4 = "Mania";
const char *q = Maximum(value3, value4);
cout << q << endl;
return 0;
}

A. template
B. class
C. Interveiw
D. Mania
E. None of these
Answer» E. None of these
7.

What is the output of this program?
#include <iostream>
using namespace std;
template<typename type>
type Maximum(type num1, type num2)
{
return num1 > num2 ? num1:num2;
}
int main()
{
int Res;
Res = Maximum(150, 250);
cout << Res << endl;
return 0;
}

A. 250
B. 150
C. Compilation Error
D. Runtime Error
E. None of these
Answer» B. 150
8.

What is the output of this program?
#include <iostream>
using namespace std;
class classA
{
public:
virtual ~classA(){}
protected:
char p;
public:
char getChar();
};
class classB : public classA
{
public:
void printChar();
};
void classB::printChar()
{
cout << "False" << endl;
}
int main()
{
classB ch;
ch.printChar();
return 1;
}

A. True
B. False
C. Runtime Error
D. Compilation Error
E. None of these
Answer» C. Runtime Error
9.

What is the output of this program?
#include <iostream>
using namespace std;
template <class T>
inline T square(T p)
{
T result;
result = p * p;
return result;
};
template <>
string square<string>(string str)
{
return (str+str);
};
int main()
{
int k = 4, kk;
string bb("A");
kk = square<int>(k);
cout << k << kk;
cout << square<string>(bb) << endl;
}

A. 164AA
B. AA416
C. 416AA
D. All of above
E. None of these
Answer» D. All of above
10.

What is the output of this program?
#include <iostream>
using namespace std;
class BaseClass
{
public:
BaseClass( )
{
cout << "I ";
}
~BaseClass ( )
{
cout << " Mania";
}
};
class DerivedClass : public BaseClass
{
public:
DerivedClass ( )
{
cout << "Love ";
}
~DerivedClass ( )
{
cout << "Interview";
}
};
int main( )
{
DerivedClass x;
}

A. I
B. Love
C. Interview
D. Mania
E. I Love Interview Mania
Answer» F.
11.

What is the output of this program?
#include <iostream>
using namespace std;
template <class type>
class Example
{
public:
Example();
~Example();
type Data(type);
};
template <class type>
type Example<type>::Data(type Value0)
{
return Value0;
}
template <class type>
Example<type>::Example()
{
}
template <class type>
Example<type>::~Example()
{
}
int main(void)
{
Example<char> Value1;
cout << Value1.Data('L') << endl;
return 0;
}

A. Compilation Error
B. Runtime Error
C. Garbage Value
D. L
E. None of these
Answer» E. None of these
12.

What is the output of this program?
#include <iostream>
using namespace std;
template <class T>
class N
{
public:
N(int p): q(p) {}
protected:
int q;
};
template <class T>
class M: public N<char>
{
public:
M(): N<char>::N(50)
{
cout << q * 3 << endl;
}
};
int main()
{
M<char> test;
return 0;
}

A. 50
B. 150
C. Compilation Error
D. Runtime Error
E. None of these
Answer» C. Compilation Error
13.

Which of the following is correct about templates?

A. It is a type of compile time polymorphism
B. It allows the programmer to write one code for all data types
C. Helps in generic programming
D. All of the mentioned
Answer» E.
14.

Which of the following is used for generic programming?

A. Virtual functions
B. Modules
C. Templates
D. Abstract Classes
Answer» D. Abstract Classes
15.

How many kinds of parameters are there in C++?

A. 3
B. 2
C. 1
D. All of above
E. None of these
Answer» B. 2
16.

Which is dependant on template parameter?

A. method
B. base class
C. abstract class
D. All of above
E. None of these
Answer» C. abstract class
17.

What is another name of full specialization?

A. function overloading template
B. explicit specialization
C. implicit specialization
D. All of above
E. None of these
Answer» C. implicit specialization
18.

How many types of specialization are there in c++?

A. 4
B. 3
C. 2
D. 1
E. None of these
Answer» D. 1
19.

What is a function template?

A. creating a function with having a exact type
B. all of the mentioned
C. creating a function without having to specify the exact type
D. All of above
E. None of these
Answer» D. All of above
20.

How many parameters are legal for non-type template?

A. 4
B. 3
C. 2
D. 1
E. None of these
Answer» B. 3
21.

What may be the name of the parameter that the template should take?

A. same as class
B. same as function
C. same as template
D. All of above
E. None of these
Answer» D. All of above
22.

Which are done by compiler for templates?

A. code elimination
B. type-safe
C. portability
D. All of above
E. None of these
Answer» C. portability
23.

What is meant by template parameter?

A. It can be used to evaluate a type
B. It can of no return type
C. It can be used to pass a type as argument
D. All of above
E. None of these
Answer» D. All of above
24.

From where does the template class derived?

A. templated class
B. regular non-templated C++ class or templated class
C. regular non-templated C++ class
D. All of above
E. None of these
Answer» C. regular non-templated C++ class
25.

What can be passed by non-type template parameters during compile time?

A. constant expression
B. float
C. int
D. All of above
E. None of these
Answer» D. All of above
26.

Which of the things does not require instantiation?

A. member class
B. functions
C. non virtual member function
D. All of above
E. None of these
Answer» E. None of these
27.

Which parameter is legal for non-type template?

A. object
B. class
C. pointer to member
D. All of above
E. None of these
Answer» D. All of above
28.

Why we use :: template-template parameter?

A. class
B. rebinding
C. binding
D. both binding & rebinding
E. None of these
Answer» E. None of these