My Report

C++ Programming Mock Test 5


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement
 10%

Question 1 of 10

1. What will be the output of the following C++ code?

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
template<class T>
class A
{
    public:
	T func(T a, T b){
		return a/b;
	}	
};

int main(int argc, char const *argv[])
{
	A <int>a1;
	cout<<a1.func(3,2)<<endl;
	cout<<a1.func(3.0,2.0)<<endl;
	return 0;
}

Question 1 of 10

Question 2 of 10

2. How many types of the constructor are there in C++?

Question 2 of 10

Question 3 of 10

3. What is a template?

Question 3 of 10

Question 4 of 10

4. What will be the output of the following C++ code?

  
    #include <iostream>
    using namespace std;
    template<typename type>
    class TestVirt
    {
        public:
        virtual type TestFunct(type Var1)
        {
            return Var1 * 2;
        }
    };
    int main()
    {
        TestVirt<int> Var1;
        cout << Var1.TestFunct(100) << endl;
        return 0;
    }

Question 4 of 10

Question 5 of 10

5. What will be the output of the following C++ code?

    #include <iostream>
    using namespace std;
    template <class T>
    inline T square(T x)
    {
        T result;
        result = x * x;
        return result;
    };
    template <>
    string square<string>(string ss)
    {
        return (ss+ss);
    };
    int main()
    {
        int i = 2, ii;
        string ww("A");
        ii = square<int>(i);
        cout << i << ": " << ii;
        cout << square<string>(ww) << ":" << endl;
    } 

Question 5 of 10

Question 6 of 10

6. What will be the output of the following C++ code?

    #include <iostream>
    using namespace std;
    class BaseClass 
    {
        int x;
        public:
        void setx(int n) 
        {
            x = n;
        }
        void showx() 
        {
            cout << x ;
        }
    };
    class DerivedClass : private BaseClass
    {
        int y;
        public:
        void setxy(int n, int m)
        {
            setx(n);      
            y = m;
        }
        void showxy() 
        {
            showx();       
            cout << y << '\n';
        }
    };
    int main()
    {
        DerivedClass ob;
        ob.setxy(10, 20);
        ob.showxy();
        return 0;
    } 

Question 6 of 10

Question 7 of 10

7. What will be the output of the following C++ code?

#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;
}

Question 7 of 10

Question 8 of 10

8. How many Sequence Containers are provided by C++?

Question 8 of 10

Question 9 of 10

9. What does this template function indicates?

==================
template<class T>
T func(T a)
{
	cout<<a;
}
==================

Question 9 of 10

Question 10 of 10

10. What will be the output of the following C++ code?

    #include <iostream>
    using namespace std;
    int main()
    {
        try
        {
            throw 1;
        }
        catch (int a)
        {
            cout << "exception number:  " << a << endl;
            return 0;
        }
        cout << "No exception " << endl;
        return 0;
    } 

Question 10 of 10


 

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.