C++ programming Constructor, Destructor Aptitude Questions and Answers

In this section, there are questions related to Constructor and Destructor.

List of C++ Constructor and Destructor Aptitude Questions and Answers

1) Can we define a class without creating constructors?
  1. Yes
  2. No

2) Which is the correct form of default constructor for following class?
#include <iostream>
using namespace std;

class sample
{
	private:
		int x,y;
};
    
  1. public: void sample(){}
  2. public: void sample(){ x=0; y=0;}
  3. public: void sample(int a,int b){ x=a; y=b;}
  4. Both 1 and 2

3) What will be the output of following program?
#include <iostream>
using namespace std;

class sample
{
	private:
		int x,y;
	public:
		void sample(int a,int b)
		{ x=a; y=b;}
};
int main()
{
	sample s;
	return 0;
}
    
  1. Compile Time Error
  2. Run Time Error
  3. No Error
  4. Warning

4) What will be the output of following program?
#include <iostream>
using namespace std;

class sample
{
	private:
		int x;
	public:
		void sample(){x=0; printf("Object created.");}
		void sample(int a){x=a;}
};

int main()
{
	sample s;
	return 0;
}
    
  1. Compile Time Error
  2. Object Created.
  3. Run Time Error
  4. Can’t be predicted