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

8/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>
#include <complex>
using namespace std;
int main()
{
   complex<double> c1(4.0,3.0);
   cout << "c1: " << c1;
   complex<float> c2(polar(5.0,0.75));
   cout << c1 + complex<double>(c2.real(),c2.imag());
   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;
    int *p;
    x = 5;
    p = &x;
    cout << *p;
    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 i;
    enum month 
    {
        JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC
    };
    for (i = JAN; i <= DEC; i++)
        cout << i;
    return 0;
}

Select an option to see the answer and solution.

How many approaches are used for operator overloading?

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, b;
    int* c;
    c = &a;
    a = 200;
    b = 200;
    *c = 100;
    b = *c;
    cout << *c << " " << b;
    return 0;
}

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>
#include <string>
using namespace std;
 
class A
{
	static int a;
 
   public:
	void change(int i){
		a = i;
	}
	void value_of_a(){
		cout<<a;
	}
};
 
int main(int argc, char const *argv[])
{
	A a1 = A();
	a1.change(5);
	a1.value_of_a();
	return 0;
}

Select an option to see the answer and solution.

What is the syntax of overloading operator + for class A?

Select an option to see the answer and solution.

What will be the output of the following C++ code if the string entered by the user is "Hello World"?
#include <iostream> 
#include <string>
using namespace std; 
int main(int argc, char const *argv[])
{
	string str;
	cin>>str;
	cout<<str;
	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()
{
    double a = 21.09399;
    float b = 10.20;
    int c ;
    c = (int) a;
    cout << c ;
    c = (int) b;
    cout << c ;
    return 0;
}

Select an option to see the answer and solution.

How many types of representation are in the string?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
    char str1[10] = "Hello";
    char str2[10] = "World";
    char str3[10];
    int  len ;
    strcpy( str3, str1);
    strcat( str1, str2);
    len = strlen(str1);
    cout << len << 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 Box{
    int capacity;
public:
    Box(){}
    Box(double capacity){
	this->capacity = capacity;
    }
    bool operator<(Box b){
	return b.capacity < this->capacity? true : false;
    }
};
int main(int argc, char const *argv[])
{
    Box b1(10);
    Box b2 = Box(14);
     if(b1 < b2){
	cout<<"B1's capacity is small";
    }
    else{
	cout<<"B2's capacity is small";
    }
    return 0;
}

Select an option to see the answer and solution.

Which header file is used to declare the complex number?

Select an option to see the answer and solution.

Pick out the correct statement.

Select an option to see the answer and solution.

Pick the other name of operator function.

Select an option to see the answer and solution.

Which header file is used to include the string object functions in C++?

Select an option to see the answer and solution.

Constructors are used to . . . . . . . .

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(){}
    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 operator+(complex c)
    {
	complex temp;
	temp.i = this->i + c.i;
	temp.j = this->j + c.j;
	temp.show_poss();
    }
 void show(){
    cout<<"Complex Number: "<<i<<" + i"<<j<<endl;
    }
 void show_poss(){
    cout<<"Your result after addition will be: "<<i<<" + i"<<j<<endl;
    }
};
int main(int argc, char const *argv[])
{
    complex c1(1,2);
    complex c2(3,4);
    c1 + c2;
    return 0;
}

Select an option to see the answer and solution.

What is the header file for the string class?

Select an option to see the answer and solution.