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

9/13

Page

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

Pick out the correct option.

Select an option to see the answer and solution.

What is the identifier given to string class to declare string objects?

Select an option to see the answer and solution.

Which is used to do the dereferencing?

Select an option to see the answer and solution.

Pick out the correct statement.

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()
{
    double arr[] = {5.0, 6.0, 7.0, 8.0};
    double *p = (arr+2);
    cout << *p << endl;   
    cout << arr << endl;  
    cout << *(arr+3) << endl;
    cout << *(arr) << endl;  
    cout << *arr+9 << endl;  
    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 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(){
 
		C c(10);
		cout<<"value of b is: "<<c.b.show()<<endl;
	}
};
 
int main(int argc, char const *argv[])
{
	C c(1);
	c.show();
	return 0;
}

Select an option to see the answer and solution.

Which is the correct way of concatenating a character at the end of a string object?
way 1:
string s;
s = s + 'a';
 
way 2:
string s;
s.push_back('a');

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[])
{
	string s1 = "Hello";
	string s2 = "World";
	string s3 = s1 + " " + s2;
	cout<<s3;
	return 0;
}

Select an option to see the answer and solution.

When we are using heap operations what do we need to do to save the memory?

Select an option to see the answer and solution.

Which method do we use to append more than one character at a time?

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 Box
{
	int capacity;
    public:
	Box(int cap){
		capacity = cap;
	}
	friend void show();
};
 
void Box::show()
{	
	Box b(10);
	cout<<"Value of capacity is: "<<b.capacity<<endl;
}
 
int main(int argc, char const *argv[])
{
	show();
	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;
int main ()
{
    string str ("steve jobs is legend");
    string::iterator it;
    str.erase (str.begin()+ 5, str.end()-7);
    cout << str << endl;      
    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 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;
}

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 Box
{
    double width;
    public:
    friend void printWidth( Box box );
    void setWidth( double wid );
};
void Box::setWidth( double wid )
{
    width = wid;
}
void printWidth( Box box )
{
    box.width = box.width * 2;
    cout << "Width of box : " << box.width << endl;
}
int main( )
{
    Box box;
    box.setWidth(10.0);
    printWidth( box );
    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 Complex
{
    private:
    float real;
    float imag;
    public:
    Complex():real(0), imag(0){}
    Complex operator ()(float re, float im)
    {
        real += re;
        imag += im;
        return *this;
    }
    Complex operator() (float re)
    {
        real += re;
        return *this;
    }
    void display()
    {
        cout << "(" << real << "," << imag << ")" << endl;
    }
};
int main()
{
    Complex c1, c2;
    c2 = c1(3.2, 5.3);
    c1(6.5, 2.7);
    c2(1.9);
    cout << "c2=";c1.display();
    cout << "c2=";c2.display();
    return 0;
}

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, 3.0);
    complex<float> c2(polar(5.0, 0.75));
    cout  << (c1 += sqrt(c1)) << endl;
    return 0;
}

Select an option to see the answer and solution.

What does the dereference operator will return?

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 Box
{
    int capacity;
    Box(){}
    Box(double capacity){
        this->capacity = capacity;
    }
};
int main(int argc, char const *argv[])
{
    Box b1(10);
    Box b2 = Box(14);
    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 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(){
 
		C c(10);
		cout<<"value of b is: "<<c.b.show()<<endl;
	}
};
 
int main(int argc, char const *argv[])
{
	show();
	return 0;
}

Select an option to see the answer and solution.

Give the function prototype of the operator function which we need to define in this program so that the program has no errors.
#include <iostream>
#include <string>
using namespace std;
class Box{
    int capacity;
public:
    Box(){}
    Box(double capacity){
	this->capacity = capacity;
    }
};
int main(int argc, char const *argv[])
{
    Box b1(10);
    Box b2 = Box(14);
    if(b1 == b2){
	cout<<"Equal";
    }
    else{
        cout<<"Not Equal";
    }
    return 0;
}

Select an option to see the answer and solution.