Vidyalelo
C++ Programming · all questions

Classes and Objects in C++
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

251

Questions

7/13

Page

Pick an option on a question to see the right answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
const int SIZE = 10;
class safe
{
    private:
    int arr[SIZE];
    public:
    safe()
    {
        register int i;
        for (i = 0; i < SIZE; i++)
        {
            arr[i] = i;
        }
    }
    int &operator[](int i)
    {
        if (i > SIZE)
        {
            cout << "Index out of bounds" <<endl;
            return arr[0];
        }
        return arr[i];
    }
};
int main()
{
    safe A;
    cout << A[5];
    cout  << A[12];
    return 0;
}

Select an option to see the answer and solution.

Which of the following is correct about friend functions?

Select an option to see the answer and solution.

Which function is used to get the length of a string object?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class sample 
{
    public:
    int x, y;
    sample() {};
    sample(int, int);
    sample operator + (sample);
};
sample::sample (int a, int b) 
{
    x = a;
    y = b;
}
sample sample::operator+ (sample param) 
{
    sample temp;
    temp.x = x + param.x;
    temp.y = y + param.y;
    return (temp);
}
int main () 
{
    sample a (4,1);
    sample b (3,2);
    sample c;
    c = a + b;
    cout << c.x << "," << c.y;
    return 0;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
	static int a;
 
   public:
	void show()
        {
		a++;
		cout<<"a: "<<a<<endl;
	}
	void operator.()
        {
		cout<<"Objects are added\n";
	}
};
 
class B
{
     public:
};
 
int main(int argc, char const *argv[])
{
	A a1, a2;
	return 0;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main() 
{
    int x = 9;
    int* p = &x;
    cout << sizeof(p);
    return 0;
}

Select an option to see the answer and solution.

How many types of user-defined data type are in c++?

Select an option to see the answer and solution.

Which is the correct statement about operator overloading?

Select an option to see the answer and solution.

Which other keywords are also used to declare the class other than class?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
const int limit = 4;
class safearray
{
    private:
    int arr[limit];
    public:
    int& operator [](int n)
    {
        if (n == limit - 1)
        {
            int temp;
            for (int i = 0; i < limit; i++)
            {
                if (arr[n + 1] > arr[n])
                {
                    temp = arr[n];
                    arr[n] = arr[n + 1];
                    arr[n + 1] = temp;
                }     
            }  
        }
        return arr[n];
    }
};
int main()
{
    safearray sa1;
    for(int j = 0; j < limit; j++)
        sa1[j] = j*10;
    for(int j = 0; j < limit; j++)
    {
        int temp = sa1[j];
        cout << "Element " << j << " is " << temp;
    }
    return 0;
}

Select an option to see the answer and solution.

Which of the following is a valid class declaration?

Select an option to see the answer and solution.

Which option is best to eliminate the memory problem?

Select an option to see the answer and solution.

Pick out the correct statement.

Select an option to see the answer and solution.

What do we need to use when we have multiple subscripts?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <complex>
using namespace std;
int main() 
{
   complex<double> c1(4.0, 16.0), c2;
   c2 = pow(c1, 2.0);
   cout << c2;
   return 0;          
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream> 
using namespace std; 
class S 
{ 
	int m; 
   public: 
	#define MAC(S::m)
};
 
int main(int argc, char const *argv[])
{
	cout<<"Hello World";
	return 0;
}

Select an option to see the answer and solution.

Which of the following operator cannot be used to overload when that function is declared as friend function?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main ()
{
    int a;
    int * ptr_b;
    int ** ptr_c;
    a = 1;
    ptr_b = &a;
    ptr_c = &ptr_b;
    cout << a << "\n";
    cout << *ptr_b << "\n";
    cout << *ptr_c << "\n";
    cout << **ptr_c << "\n";
    return 0;
}

Select an option to see the answer and solution.

In Linux, how do the heaps and stacks are managed?

Select an option to see the answer and solution.

Which of the following is not a function of complex values?

Select an option to see the answer and solution.