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

5/13

Page

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

Inline functions are avoided when . . . . . . . .

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 rect
{
    int x, y;
    public:
    void val (int, int);
    int area ()
    {
        return (x * y);
    }
};
void rect::val (int a, int b)
{
    x = a;
    y = b;
}
int main ()
{
    rect rect;
    rect.val (3, 4);
    cout << "rect area: " << rect.area();
    return 0;
}

Select an option to see the answer and solution.

What is a friend function in C++?

Select an option to see the answer and solution.

Pick 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;
class Box
{   
    double length;
    double breadth;
    double height;
    public:
    double getVolume(void)
    {  
        return length * breadth * height;
    }
    void setLength( double len )
    {   
        length = len;
    }
    void setBreadth( double bre )
    {   
        breadth = bre;
    }
    void setHeight( double hei )
    {   
        height = hei;
    }
    Box operator+(const Box& b)
    {  
        Box box;
        box.length = this->length + b.length;
        box.breadth = this->breadth + b.breadth;
        box.height = this->height + b.height;
        return box;
    } 
};
int main( )
{  
    Box Box1;
    Box Box2;
    Box Box3;
    double volume = 0.0;
    Box1.setLength(6.0);
    Box1.setBreadth(7.0);
    Box1.setHeight(5.0);
    Box2.setLength(12.0);
    Box2.setBreadth(13.0);
    Box2.setHeight(10.0);
    volume = Box1.getVolume();
    cout << "Volume of Box1 : " << volume <<endl;
    volume = Box2.getVolume();
    cout << "Volume of Box2 : " << volume <<endl;
    Box3 = Box1 + Box2;
    volume = Box3.getVolume();
    cout << "Volume of Box3 : " << volume <<endl;
    return 0;
}

Select an option to see the answer and solution.

How to declare operator 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 ()
{
    string str ("Ubuntu");
    cout << str.capacity();
    cout << str.max_size();
    return 0;
}

Select an option to see the answer and solution.

What is the use of functor?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void duplicate (int& a, int& b, int& c)
{
    a *= 2;
    b *= 2;
    c *= 2;
}
int main ()
{
    int x = 1, y = 3, z = 7;
    duplicate (x, y, z);
    cout << x << y << z;
    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 a = 20;
    int b = 10;
    int c = 15;
    int d = 5;
    int e;
    e = a + b * c / d;
    cout << e << endl ;
    return 0;
}

Select an option to see the answer and solution.

Which is used to tell the computer that where a pointer is pointing to?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <cmath>
using namespace std;
class Complex
{
    private:
    double real;
    double imag;
    public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i)
    {}
    double mag()
    {  
        return getMag();
    }
    operator double ()
    {
        return getMag();
    }
    private:
    double getMag()
    {
        return sqrt(real * real + imag * imag);
    }
};
int main()
{
    Complex com(3.0, 4.0);
    cout << com.mag();
    cout << com;
    return 0
}

Select an option to see the answer and solution.

What is the scope of typedef defined data types?

Select an option to see the answer and solution.

What is the associativity of add(+);?

Select an option to see the answer and solution.

Which is called ternary operator?

Select an option to see the answer and solution.

How the objects are self-referenced in a member function of that 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 number
{
    int i;
    public:
    int geti();
    void puti(int j);
};
int number::geti()
{
    return i;
}
void number::puti(int j)
{
    i = j;
}
int main()
{
    number s;
    s.puti(10);
    cout << s.geti( );
    return 0;
}

Select an option to see the answer and solution.

How many objects can present in a single class?

Select an option to see the answer and solution.

How many member functions are there in this C++ class excluding constructors and destructors?
class Box
{
	int capacity;
   public:
	void print();
	friend void show();
	bool compare();
	friend bool lost();
};

Select an option to see the answer and solution.

In which form does the function call operator can be overloaded?

Select an option to see the answer and solution.