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

4/13

Page

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

Which is the correct example of a binary operator?

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;
int main ()
{
  string str = "HelloWorld!";
  cout<<str.capacity();
  cout<<str.size();
  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>
#include <cstring>
using namespace std; 
int main(int argc, char const *argv[])
{
	string s("a");
	cout<<s;
	return 0;
}

Select an option to see the answer and solution.

Which of the following operator can 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> 
#include <string>
using namespace std; 
int main(int argc, char const *argv[])
{
	char s1[6] = "Hello";
	char s2[6] = "World";
	char s3[12] = s1 + " " + s2;
	cout<<s3;
	return 0;
}

Select an option to see the answer and solution.

The data members and functions of a class in C++ are by default . . . . . . . .

Select an option to see the answer and solution.

How many arguments will the subscript operator will take for overloading?

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:
	A()
        {
		cout<<"Object of A is created\n";
	}
	void show()
        {
		a++;
		cout<<"a: "<<a<<endl;
	}
};
 
class B
{
    public:
};
 
int main(int argc, char const *argv[])
{
	A a1, a2;
	A a3 = a1 + a2;
	return 0;
}

Select an option to see the answer and solution.


#include <iostream>
using namespace std;
class three_d 
{
    int x, y, z;
    public:
    three_d() { x = y = z = 0; }
    three_d(int i, int j, int k) { x = i; y = j; z = k; }
    three_d operator()(three_d obj);
    three_d operator()(int a, int b, int c);
    friend ostream &operator<<(ostream &strm, three_d op);
};
three_d three_d::operator()(three_d obj)
{
    three_d temp;
    temp.x = (x + obj.x) / 2;
    temp.y = (y + obj.y) / 2;
    temp.z = (z + obj.z) / 2;
    return temp;
}
three_d three_d::operator()(int a, int b, int c)
{
    three_d temp;
    temp.x = x + a;
    temp.y = y + b;
    temp.z = z + c;
    return temp;
}
    ostream &operator<<(ostream &strm, three_d op) {
    strm << op.x << ", " << op.y << ", " << op.z << endl;
    return strm;
}
int main()
{
    three_d objA(1, 2, 3), objB(10, 10, 10), objC;
    objC = objA(objB(100, 200, 300));
    cout << objC;
    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 sample
{
    private:
    int* i;
    int j;
    public:
    sample (int j);
    ~sample ();
    int& operator [] (int n);
};
int& sample::operator [] (int n)
{
    return i[n];
}
sample::sample (int j)
{
    i = new int [j];
    j = j;
}
sample::~sample ()
{
    delete [] i;
}
int main ()
{
    sample m (5);
    m [0] = 25;
    m [1] = 20;
    m [2] = 15;
    m [3] = 10;
    m [4] = 5;
    for (int n = 0; n < 5; ++ n)
    cout << m [n];
    return 0;
}

Select an option to see the answer and solution.

How many specifiers are present in access specifiers in class?

Select an option to see the answer and solution.

Which special character is used to mark the end of class?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <stdio.h> 
#include<iostream>
using namespace std;
int main()
{
    int x = 5, y = 5;
    cout << ++x << --y << endl;
    return 0;
}

Select an option to see the answer and solution.

How to access the object in the 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;
class base
{
    int val1, val2;
    public:
    int get()
    {
        val1 = 100;
        val2 = 300;
    }
    friend float mean(base ob);
};
float mean(base ob)
{
    return float(ob.val1 + ob.val2) / 2;
}
int main()
{
    base obj;
    obj.get();
    cout << mean(obj);
    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 complex
{
    int i;
    int j;
    public:
    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;
}

Select an option to see the answer and solution.

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

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 sample1 
{
    float i, j;
};
class sample2 
{
    int x, y;
    public:
    sample2 (int a, int b) 
    {
         x = a; 
         y = b;
    }
    int result() 
    { 
         return x + y;
     }
};
int main () 
{
    sample1 d;
    sample2 * padd;
    padd = (sample2*) &d;
    cout<< padd->result();
    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>
#include <cstring>
using namespace std; 
int main(int argc, char const *argv[])
{
	const char *a = "Hello\0World";
	cout<<a;
	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 change(int i){
		a = i;
	}
	void value_of_a(){
		cout<<a;
	}
};
 
int A::a = 5;
 
int main(int argc, char const *argv[])
{
	A a1 = A();
	A a2 = A();
	A a3 = A();
	a1.change(10);
	a1.value_of_a();
	a2.value_of_a();
	a3.value_of_a();
	return 0;
}

Select an option to see the answer and solution.