Back-End Programming Exam  >  Back-End Programming Tests  >  Learn to Program with C++: Beginner to Expert  >  Test: Templates - 1 - Back-End Programming MCQ

Test: Templates - 1 - Back-End Programming MCQ


Test Description

10 Questions MCQ Test Learn to Program with C++: Beginner to Expert - Test: Templates - 1

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

What is a template?

Detailed Solution for Test: Templates - 1 - Question 1

A template is a formula for creating a generic class

Test: Templates - 1 - Question 2

Predict the output?
#include <iostream>
using namespace std;
 
template <typename T>
void fun(const T&x)
{
    static int count = 0;
    cout << "x = " << x << " count = " << count << endl;
    ++count;
    return;
}
 
int main()
{
    fun<int> (1); 
    cout << endl;
    fun<int>(1); 
    cout << endl;
    fun<double>(1.1);
    cout << endl;
    return 0;
}

Detailed Solution for Test: Templates - 1 - Question 2

Compiler creates a new instance of a template function for every data type. So compiler creates two functions in the above example, one for int and other for double. Every instance has its own copy of static variable. The int instance of function is called twice, so count is incremented for the second call.

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

How to declare a template?

Test: Templates - 1 - Question 4

Output of following program?
#include <iostream>
using namespace std;
 
template <class T>
class Test
{
private:
    T val;
public:
    static int count;
    Test()  {   count++;   }
};
 
template<class T>
int Test<T>::count = 0;
 
int main()
{
    Test<int> a;
    Test<int> b;
    Test<double> c;
    cout << Test<int>::count   << endl;
    cout << Test<double>::count << endl;
    return 0;
}

Detailed Solution for Test: Templates - 1 - Question 4

There are two classes created by the template: Test and Test. Since count is a static member, every class has its own copy of it. Also, count gets incremented in constructor.

Test: Templates - 1 - Question 5

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

Detailed Solution for Test: Templates - 1 - Question 5

There are two types of templates. They are function template and class template.

Test: Templates - 1 - Question 6

Output of following program? Assume that the size of int is 4 bytes and size of double is 8 bytes, and there is no alignment done by the compiler.
#include<iostream>
#include<stdlib.h>
using namespace std;

template<class T, class U, class V=double>
class A  {
    T x;
    U y;
    V z;
    static int count;
};
 
int main()
{
   A<int, int> a;
   A<double, double> b;
   cout << sizeof(a) << endl;
   cout << sizeof(b) << endl;
   return 0;
}

Detailed Solution for Test: Templates - 1 - Question 6

templates can also have default parameters. The rule is same all default values must be on the rightmost side. Since count is static, it is not counted in sizeof.

Test: Templates - 1 - Question 7

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

Test: Templates - 1 - Question 8

What is the output of the program?
#include <iostream>
using namespace std;
 
template <int i>
void fun()
{
   i = 20;
   cout << i;
}
 
int main()
{
   fun<10>();
   return 0;
}

Detailed Solution for Test: Templates - 1 - Question 8

Compiler error in line "i = 20;" Non-type parameters must be const, so they cannot be modified.

Test: Templates - 1 - Question 9

What is a function template?

Detailed Solution for Test: Templates - 1 - Question 9

creating a function without having to specify the exact type.

Test: Templates - 1 - Question 10

Output?
#include <iostream>
using namespace std;
  
template<int n> struct funStruct
{
    static const int val = 2*funStruct<n-1>::val;
};
  
template<> struct funStruct<0>
{
    static const int val = 1 ;
};
  
int main()
{
    cout << funStruct<10>::val << endl;
    return 0;
}

Detailed Solution for Test: Templates - 1 - Question 10

This is an example of template metaprogramming. The program mainly calculates 2^10.

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