C++ MCQs with Explanations (Templates)

Templates

1. A template may be used to create a family of

Explanation:

Correct answer: a) different types of variables

Templates in C++ allow you to define generic classes and functions that can be used with different data types. A template can be used to create a family of classes or functions that share the same logic but operate on different types of data.


2. C++ templates provide support for designing

Explanation:

Correct answer: c) generic programming

C++ templates enable generic programming, which allows you to write code that can work with different data types without duplicating the code for each data type. Templates facilitate code reusability and increase flexibility in the design of classes and functions.


3. A class template is designed

Explanation:

Correct answer: b) to work with different data types

A class template in C++ is designed to work with different data types. It allows you to create a blueprint for a class that can be instantiated with various data types. The same template class can be used to generate different classes based on the specified data types.


4. Which one of the following is FALSE?

Explanation:

Correct answer: d) Templates automatically create different versions of the function at run-time, depending on user input

This statement is false. Templates in C++ are a compile-time feature. When you use a template function or class, the compiler generates different versions of the function or class for each data type used with the template. These versions are created at compile-time, not run-time.


5. When we use the concept of templates, when does the program generate actual code for a template function?

Explanation:

Correct answer: d) When the function is executed at run-time

Templates in C++ are used to generate code at compile-time. However, the actual code for a template function is generated when the function is executed at run-time, with the specific data type argument provided. This process is called template instantiation.


6. Given below is the definition of a function template

        template <class T>
        T mul (x, y)
        {
            return x * y;
        }
    

Identify errors, if any.

Explanation:

Correct answer: a) class T in the first line should be within angle brackets < and >

In the function template definition, the template parameter should be enclosed within angle brackets < and >. The correct syntax is:

            template <class T>
            T mul (T x, T y)
            {
                return x * y;
            }
        

The template parameter <class T> specifies that the function is a template function, and <T> indicates the type parameter.


7. Which of the following statements is TRUE?

Explanation:

Correct answer: d) All the above

All the statements are true. Let's explain each one:

a) Member functions of a template class defined outside the class need not be defined as function templates: When you define a member function of a template class outside the class definition, you should include the template declaration for the function. However, if the function is not used with different data types, it can be defined without the template declaration, just like a regular function.

b) In class templates with multiple arguments, all the arguments need not be of class T type arguments: In class templates with multiple arguments, different arguments can have different types. They don't need to be of the same type as the template parameter T. This allows greater flexibility in defining template classes that work with multiple data types.

c) Template functions cannot be overloaded: Template functions can be overloaded with different sets of template arguments. This allows you to create multiple versions of a template function that can handle different data types or different numbers of arguments.


8. Consider a function template as shown below:

        template <class T>
        int find (T* array, T n, int m)
        {
            for (int i = 0; i < m; i++)
            {
                if (array[i] == n)
                    return i;
            }
            return (-1);
        }
    

Which one of the following prototype calls can generate a function?

Explanation:

Correct answer: b) find (T*, int, int);

In the function template definition, the template parameter T represents the type of data stored in the array. When calling the function, you need to provide the actual data type for the template parameter. Option b) correctly uses the template parameter T when calling the function, indicating that the array contains elements of type T, and n is of type T.


9. As we know, a macro can be used to create different versions of a function for different data types. What is then the difference between a macro and a class template?

Explanation:

Correct answer: a) Macros do not perform type checking and c) Macros are suitable for functions that can be written in a single statement.

Macros and class templates are two different mechanisms for code generation in C++. The main differences between them are as follows:

a) Macros do not perform type checking: In C++, macros are simple text replacements. They do not consider the data types of the arguments passed to them. This lack of type checking can lead to errors and unexpected behavior if incorrect data types are used with the macro.

b) Macros do not specify the type of the value to be returned: Macros do not have a return type; they are purely text-based replacements. Any value generated by a macro is determined by the text substitution performed during preprocessing.

c) Macros are suitable for functions that can be written in a single statement: Macros are often used for simple and short functions that can be written in a single statement. They are less suitable for complex functions that require multiple lines of code, as macros do not support multiple statements and can lead to unintended side effects.


10. The following is a list of header lines for creating function templates. Which of them is legal?

Explanation:

Correct answer: a) template <class T> swap (T & x, T & y)

The correct syntax for creating a function template in C++ is to use the "template <class T>" syntax before the function declaration. Option a) correctly provides the template declaration, specifying that the function is a template function that works with type T.

Option b) is not valid because it does not include the "class" keyword before the template parameter T.

Option c) is not valid because it places "class" after "template" and "T" inside angle brackets. The correct order should be "template <class T>" with "class" before "T."

Option d) is not valid because option a) is correct, and it is one of the options.



Your Score is 10

THE STUDENT FRIENDLY BOOK TO LEARN C++. THE EXPLANATIONS ARE IN SIMPLE ENGLISH AND THEY PROVIDE EXERCISES TO MAKE YOUR LEARNING EXCELLENT.

Topic wise C++ MCQs Index ≡
Click on particular topic to visit that page
  1. Principles of Object-Oriented Programming 
  2. Beginning with C++
  3. Tokens, Expressions, and Control Structures
  4. Functions in C++
  5. Classes and Objects
  6. Constructors and Destructors
  7. Operator Overloading
  8. Inheritance
  9. Pointers, Virtual Functions, and Polymorphism
  10. Managing Console I/O Operations
  11. Working with Files
  12. Templates
  13. Exception Handling
  14. Standard Template Library
  15. Manipulating Strings
  16. New Features in ANSI C++ Standards
  17. Object-Oriented System Development

Post a Comment

0 Comments