O GoConqr está de visual novo! Conte para nós o que você achou.

Quiz

C++ Templates

PathaPadha Support 10 perguntas

Fazer o quiz

Este quiz tem tempo limite, por isso abre no player de sempre.

Perguntas do quiz (10)

  1. 1Which 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.
    1. A1 and 2
    2. B1, 2 and 3
    3. C1, 2 and 4
    4. D1, 2, 3 and 4
  2. 2Predict 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;
    }
    1. Ax = 1 count = 0

      x = 1 count = 1

      x = 1.1 count = 0
    2. Bx = 1 count = 0

      x = 1 count = 0

      x = 1.1 count = 0
    3. Cx = 1 count = 0

      x = 1 count = 1

      x = 1.1 count = 2
    4. DCompiler Error
  3. 3#include <iostream>
    using namespace std;

    template <typename T>
    T max(T x, T y)
    {
    return (x > y)? x : y;
    }
    int main()
    {
    cout << max(3, 7) << std::endl;
    cout << max(3.0, 7.0) << std::endl;
    cout << max(3, 7.0) << std::endl;
    return 0;
    }
    1. A7
      7.0
      7.0
    2. BCompiler Error in all cout statements as data type is not specified
    3. CCompiler Error in last cout statement as call to max is ambiguous.
    4. DNone of the above
  4. 4Output 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;
    }
    1. A0
      0
    2. B1
      1
    3. C2
      1
    4. D1
      0
  5. 5Output 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;
    }
    1. A6
      12
    2. B2
      8
    3. CCompiler Error: There can not be more than one template arguments
    4. D8
      8
  6. 6Output 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;
    }
    1. A16
      24
    2. B8
      16
    3. C20
      28
    4. DCompiler Error: template parameters cannot have default values
  7. 7Output 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;
    }
    1. ACompiler error, template argument must be a data type
    2. B10
      1
    3. C10000
      256
    4. D1
      1
  8. 8Output?
    #include <iostream>
    using namespace std;

    template <int i>
    void fun()
    {
    i = 20;
    cout << i;
    }

    int main()
    {
    fun<10>();
    return 0;
    }
    1. A10
    2. B20
    3. CCompilation Error
  9. 9Output?
    #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);
    }
    1. A20
    2. BCalled 20
    3. CCompilation Error
  10. 10Output?
    #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;
    }
    1. ACompiler Error
    2. B1024
    3. C2
    4. D1

Informações

Descrição
C++ Quiz sobre C++ Templates, criado por PathaPadha Support em 04-11-2020.
Idioma
English
Visualizações
121
Etiquetas
c++
Avaliação
Ainda sem avaliações
Avalie este quiz
Avalie este quiz

Fale Conosco

Sobre o que é sua mensagem?

O que você acha do novo visual?

Você gostou?