My Report

C++ Programming Mock Test 4


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. How many types are there in increment/decrement operator?

Question 1 of 10

Question 2 of 10

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

#include <iostream>
#include <string>
using namespace std;
class B
{
	int b;
   public:
	B(int i){
		b = i;
	}
};

class C
{
	B b;
    public:
	C(int i){
		b = B(i);
	}
	friend void show();
};

void show()
{
	C c(10);
	cout<<"value of b is: "<<c.b.b<<endl;
}

int main(int argc, char const *argv[])
{
	show();
	return 0;
}

Question 2 of 10

Question 3 of 10

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

#include <iostream>
#include <string>
using namespace std;
class B
{
	int b;
     public:
	B(){}
	B(int i){
		b = i;
	}
	int show(){
		return b;
	}
};

class C
{
	B b;
     public:
	C(int i){
		b = B(i);
	}
	friend void show();
};

void show()
{
	C c(10);
	cout<<"value of b is: "<<c.b.show()<<endl;
}

int main(int argc, char const *argv[])
{
	show();
	return 0;
}

Question 3 of 10

Question 4 of 10

4. In case of non-static member functions how many maximum object arguments a binary operator overloaded function can take?

Question 4 of 10

Question 5 of 10

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

 
    #include <iostream>
    using namespace std;
    class sample 
    {
        int width, height;
        public:
        void set_values (int, int);
        int area () {return (width * height);}
        friend sample duplicate (sample);
    };
    void sample::set_values (int a, int b) 
    {
        width = a;
        height = b;
    }
    sample duplicate (sample rectparam)
    {
        sample rectres;
        rectres.width = rectparam.width * 2;
        rectres.height = rectparam.height * 2;
        return (rectres);
    }  
    int main ()  
    {
        sample rect, rectb;
        rect.set_values (2, 3);
        rectb = duplicate (rect);
        cout << rectb.area();
        return 0;
    }

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 A
    {
        public:
        int x;
        A(int n = 0) : x(n) {};
        int& operator[](int n)
        {
             cout << "0" ;
             return x;
        }
        int operator[](int n) const
        {
             cout << "1" ;
             return x;
        }
     };
    void foo(const A& a)
    {
        int z = a[2];
    }
    int main()
    {
        A a(7);
        a[3]  = 8;
        int z = a[2];
        foo(a);
        return 0;
    } 

Question 6 of 10

Question 7 of 10

7. Pick the correct statement.

Question 7 of 10

Question 8 of 10

8. How to store the large objects in c++ if it extends its allocated memory?

Question 8 of 10

Question 9 of 10

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

#include <iostream>
#include <string>
using namespace std;
class complex
{
	int i;
	int j;
        public:
	complex(){}
	complex(int a, int b)
        {
	        i = a;
		j = b;
	}

	complex operator+(complex c)
        {
		complex temp;
		temp.i = this->i + c.i;
		temp.j = this->j + c.j;
		return temp;
	}

	void show(){
		cout<<"Complex Number: "<<i<<" + i"<<j<<endl;
	}
};

int main(int argc, char const *argv[])
{
	complex c1(1,2);
	complex c2(3,4);
	complex c3 = c1 + c2;
	c3.show();
	return 0;
}

Question 9 of 10

Question 10 of 10

10. How many parameters does a default constructor require?

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.