Object Oriented Programming using C++ Questions and Answers – Template Class

«
»

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Template Class”.

1. A template class can have _____________
a) More than one generic data type
b) Only one generic data type
c) At most two data types
d) Only generic type of integers and not characters
View Answer

Answer: a
Explanation: The template class can support more than one data type. The only thing is to add all the data types required in a list separated by comma within template specification.
advertisement

2. Which among the following is the proper syntax for the template class?
a) template <typename T1, typename T2>;
b) Template <typename T1, typename T2>;
c) template <typename T> T named(T x, T y){ }
d) Template <typename T1, typename T2> T1 named(T1 x, T2 y){ }
View Answer

Answer: c
Explanation: The syntax must start with keyword template, case sensitive. Then it should include the typename and a variable to denote it. Then whenever that variable is used, it replaces it with the data type needed.

3. Can default arguments be used with the template class?
a) Yes, in some special cases
b) Yes, always
c) No, it must satisfy some specific conditions first
d) No, it can’t be done
View Answer

Answer: b
Explanation: The template class can use default arguments. This is used to specify the data type to be considered if it is not specified while passing to the generic class. The default type will be used.
advertisement
advertisement

4. What is the syntax to use explicit class specialization?
a) template <int> class myClass<>{ }
b) template <int> class myClass<int>{ }
c) template <> class myClass<>{ }
d) template <> class myClass<int>{ }
View Answer

Answer: d
Explanation: The class specialization is creation of explicit specialization of a generic class. We have to use template<> constructor for this to work. It works in the same way as with explicit function specialization.

5. Which is the most significant feature that arises by using template classes?
a) Code readability
b) Ease in coding
c) Code reusability
d) Modularity in code
View Answer

Answer: c
Explanation: The code reusability is the feature that becomes more powerful with the use of template classes. You can generate a single code that can be used in variety of programming situations.
advertisement

6. A template class defines the form of a class _____________________ it will operate.
a) With full specification of the data on which
b) With full specification of the functions on which
c) Without full specification of the data on which
d) Without full specification of the functions on which
View Answer

Answer: c
Explanation: The template classes can accept all types of data types. There is no need to specify the data on which the class has to operate. Hence it gives us flexibility to code without worrying about the type of data that might be used in the code.

7. What are the two specializations of I/O template classes in C++?
a) 16-bit character and wide characters
b) 8-bit character and wide characters
c) 32-bit character and locale characters
d) 64-bit characters and locale characters
View Answer

Answer: b
Explanation: The I/O specialization is made with wide character and 8-bit characters. Wide characters are used to store the characters that might take more than 1 byte of space in memory or any size that is different from the one that the machine is using.
advertisement

8. Can typeid() function be used with the object of generic classes?
a) Yes, only if default type is given
b) Yes, always
c) No, generic data can’t be determined
d) No, never possible
View Answer

Answer: b
Explanation: The typeid() function can be used with the objects of generic classes. An instance of a template class will take the type of data that is being used with it. Hence when typeid() function is used, the data type would have already been defined and hence we can get desired result from typeid() function.

9. The _____________ class is a specialization of a more general template class.
a) String
b) Integer
c) Digit
d) Math
View Answer

Answer: a
Explanation: The string class is more specialized. Since the string must be able to store any kind of data that is given to the string. Hence it needs maximum specialization.
advertisement

10. How is function overloading different from template class?
a) Overloading is multiple function doing same operation, Template is multiple function doing different operations
b) Overloading is single function doing different operations, Template is multiple function doing different operations
c) Overloading is multiple function doing similar operation, Template is multiple function doing identical operations
d) Overloading is multiple function doing same operation, Template is same function doing different operations
View Answer

Answer: c
Explanation: The function overloading is multiple functions with similar or different functionality but generic class functions perform the same task on given different types of data.

11. What if static members are declared inside template classes?
a) All instances will share the static variable
b) All instances will have their own static variable
c) All the instances will ignore the static variable
d) Program gives compile time error
View Answer

Answer: b
Explanation: The generic class have a special case with static members. Each instance will have its own static member. The static members are not shared usually.

12. What is the output of following program?

template <typename T>
void test(const T&x) 
{
    static int count = 0;
    cout &lt;&lt; "x = " &lt;&lt; x &lt;&lt; " count = " &lt;&lt; count &lt;&lt; endl;
    ++count;
    return;
}
 
void main() 
{
    test<int> (2);
    test<int>(2);
    test<double>(2.2);
}

a)

x = 2 count = 0
x = 2.2 count = 0
x = 2.2 count = 0

b)

x = 2 count = 0
x = 2 count = 0
x = 2.2 count = 0

c)

x = 2 count = 0
x = 2 count = 1
x = 2.2 count = 0

d)

x = 2 count = 0
x = 2 count = 1
x = 2.2 count = 2
View Answer
Answer: c
Explanation: For each new type, the class will have separate instance. Here two instances will be created and hence counter for integer goes to 1. And for float value, the count remains 0 for the output.
 
 

13. If template class is defined, is it necessary to use different types of data for each call?
a) No, not necessary
b) No, but at least two types must be there
c) Yes, to make proper use of template
d) Yes, for code efficiency
View Answer

Answer: a
Explanation: It is not necessary to use different type with each call to the generic function. Data may be of same type with each call but still the function works. We don’t consider other properties like efficiency with this concept because it is made generic to all data type, hence always works.

14. How many generic types can be given inside a single template class?
a) Only 1
b) Only 3
c) Only 7
d) As many as required
View Answer

Answer: d
Explanation: There is no restriction on the number of types to be used for making the class generic. There can be any number of generic types with a single class. Hence giving flexibility to code with all the data types.

15. Template classes must have at least one static member.
a) True
b) False
View Answer

Answer: b
Explanation: There is no mandatory condition to have static members inside template class. Not only template, it is not mandatory to have static members anywhere. We can use them as required in the code.

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using C++, here is complete set of 1000+ Multiple Choice Questions and Answers.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter