Home » Aptitude Questions and Answers » C++ Aptitude Questions and Answers

C++ programming Constructor, Destructor Aptitude Questions and Answers



C++ Constructor and Destructor based C++ programming aptitude questions and answers - This section has some questions which are based on C++ class’s constructor and destructor feature.

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






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.