Back-End Programming Exam  >  Back-End Programming Tests  >  Test: Templates - 2 - Back-End Programming MCQ

Test: Templates - 2 - Back-End Programming MCQ


Test Description

20 Questions MCQ Test - Test: Templates - 2

Test: Templates - 2 for Back-End Programming 2024 is part of Back-End Programming preparation. The Test: Templates - 2 questions and answers have been prepared according to the Back-End Programming exam syllabus.The Test: Templates - 2 MCQs are made for Back-End Programming 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests for Test: Templates - 2 below.
Solutions of Test: Templates - 2 questions in English are available as part of our course for Back-End Programming & Test: Templates - 2 solutions in Hindi for Back-End Programming course. Download more important topics, notes, lectures and mock test series for Back-End Programming Exam by signing up for free. Attempt Test: Templates - 2 | 20 questions in 40 minutes | Mock test for Back-End Programming preparation | Free important questions MCQ to study for Back-End Programming Exam | Download free PDF with solutions
Test: Templates - 2 - Question 1

Which is used to describe the function using placeholder types?

Detailed Solution for Test: Templates - 2 - Question 1

During runtime, We can choose the appropriate type for the function and it is called as template type parameters.

Test: Templates - 2 - Question 2

Which of the following provides the best description of an entity type?

1 Crore+ students have signed up on EduRev. Have you? Download the App
Test: Templates - 2 - Question 3

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

Detailed Solution for Test: Templates - 2 - Question 3

Non-type template parameters provide the ability to pass a constant expression at compile time. The constant expression may also be an address of a function, object or static class member.

Test: Templates - 2 - Question 4

Which of the following is true about templates.
(1) Template is a feature of C++ that allows us to write one code for different data types.
(2) We can write one function that can be used for all data types including user defined types. Like sort(), max(), min(), ..etc.
(3) We can write one class or struct that can be used for all data types including user defined types. Like Linked List, Stack, Queue ..etc.
(4) Template is an example of compile time polymorphism.

Test: Templates - 2 - Question 5

What is meant by template parameter?

Detailed Solution for Test: Templates - 2 - Question 5

A template parameter is a special kind of parameter that can be used to pass a type as argument.

Test: Templates - 2 - Question 6

How many parameters are legal for non-type template?

Detailed Solution for Test: Templates - 2 - Question 6

The following are legal for non-type template parameters: integral or enumeration type, Pointer to object or pointer to function, Reference to object or reference to function, Pointer to member.

Test: Templates - 2 - Question 7

What is the validity of template parameters?

Test: Templates - 2 - Question 8

Which of the following is incorrect in C++ ?
(1) When we write overloaded function we must code the function for each usage.
(2) When we write function template we code the function only once.
(3) It is difficult to debug macros.
(4) Templates are more efficient than macros.

Detailed Solution for Test: Templates - 2 - Question 8
  • When we write overloaded function we must code the function for each usage. Correct
  • When we write function template we code the function only once. Correct
  • It is difficult to debug macros Correct
  • Templates are more efficient than macros Correct

So, option (D) is correct.

Test: Templates - 2 - Question 9

Which parameter is legal for non-type template?

Detailed Solution for Test: Templates - 2 - Question 9

The following are legal for non-type template parameters:integral or enumeration type, Pointer to object or pointer to function, Reference to object or reference to function, Pointer to member.

Test: Templates - 2 - Question 10

Output of following program? Assume that the size of char is 1 byte and size of int is 4 bytes, and there is no alignment done by the compiler.
#include<iostream>
#include<stdlib.h>
using namespace std;
 
template<class T, class U>
class A  {
    T x;
    U y;
    static int count;
};
 
int main()  {
   A<char, char> a;
   A<int, int> b;
   cout << sizeof(a) << endl;
   cout << sizeof(b) << endl;
   return 0;
}

Detailed Solution for Test: Templates - 2 - Question 10

Since count is static, it is not counted in sizeof.

Test: Templates - 2 - Question 11

What is meant by template specialization?

Detailed Solution for Test: Templates - 2 - Question 11

In the template specialization, it will make the template to be specific for some data types.

Test: Templates - 2 - Question 12

Output of following program.
#include <iostream>
using namespace std;
 
template <class T, int max>
int arrMin(T arr[], int n)
{
   int m = max;
   for (int i = 0; i < n; i++)
      if (arr[i] < m)
         m = arr[i];
 
   return m;
}
 
int main()
{
   int arr1[]  = {10, 20, 15, 12};
   int n1 = sizeof(arr1)/sizeof(arr1[0]);
 
   char arr2[] = {1, 2, 3};
   int n2 = sizeof(arr2)/sizeof(arr2[0]);
 
   cout << arrMin<int, 10000>(arr1, n1) << endl;
   cout << arrMin<char, 256>(arr2, n2);
   return 0;
}

Detailed Solution for Test: Templates - 2 - Question 12

We can pass non-type arguments to templates. Non-type parameters are mainly used for specifying max or min values or any other constant value for a particular instance of template. The important thing to note about non-type parameters is, they must be const. Compiler must know the value of non-type parameters at compile time. Because compiler needs to create functions/classes for a specified non-type value at compile time. Following is another example of non-type parameters.

#include <iostream>
using namespace std;

template < class T, int N >
 T fun (T arr[], int size)
{
    if (size > N)
      cout << "Not possible";
    T max = arr[0];
   for (int i = 1; i < size; i++)
      if (max < arr[i])
          max = arr[i];
   return max;
}

int main ()
{
    int arr[] = {12, 3, 14};
    cout << fun  (arr, 3);
}

Test: Templates - 2 - Question 13

Which is called on allocating the memory for array of objects?

Detailed Solution for Test: Templates - 2 - Question 13

When you allocate memory for an array of objects, the default constructor must be called to construct each object. If no default constructor exists, you’re stuck needing a list of pointers to objects.

Test: Templates - 2 - Question 14

Output?
#include <iostream>
using namespace std;
 
template <class T>
T max (T &a, T &b)
{
    return (a > b)? a : b;
}
 
template <>
int max <int> (int &a, int &b)
{
    cout << "Called ";
    return (a > b)? a : b;
}
 
int main ()
{
    int a = 10, b = 20;
    cout << max <int> (a, b);
}

Detailed Solution for Test: Templates - 2 - Question 14

Above program is an example of template specialization. Sometime we want a different behaviour of a function/class template for a particular data type. For this, we can create a specialized version for that particular data type.

Test: Templates - 2 - Question 15

What is other name of full specialization?

Test: Templates - 2 - Question 16

Which value is placed in the base class?

Detailed Solution for Test: Templates - 2 - Question 16

We can place the default type values in a base class and overriding some of them through derivation.

Test: Templates - 2 - Question 17

How many kinds of entities are directly parameterized in c++?

Detailed Solution for Test: Templates - 2 - Question 17

C++ allows us to parameterize directly three kinds of entities through templates: types, constants, and templates.

Test: Templates - 2 - Question 18

Which is used to get the input during runtime?

Detailed Solution for Test: Templates - 2 - Question 18

cin is mainly used to get the input during the runtime.

Test: Templates - 2 - Question 19

Pick out the correct statement about string template?

Detailed Solution for Test: Templates - 2 - Question 19

Every string template is used to replace the string with another string at runtime.

Test: Templates - 2 - Question 20

Which are done by compiler for templates?

Detailed Solution for Test: Templates - 2 - Question 20

The compiler can determine at compile time whether the type associated with a template definition can perform all of the functions required by that template definition.

Information about Test: Templates - 2 Page
In this test you can find the Exam questions for Test: Templates - 2 solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Templates - 2, EduRev gives you an ample number of Online tests for practice
Download as PDF