C++ programming Constructor, Destructor Aptitude Questions and Answers

C++ constructor and destructor questions: This section contains aptitude questions and answers (MCQs) on Constructors and Destructors in C++.
Submitted by IncludeHelp, on May 03, 2019

List of C++ Constructor and Destructor Aptitude Questions & 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: sample(){}
  2. public: sample(){ x=0; y=0;}
  3. public: 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:
		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:
		sample()
		{
			x = 0;
			printf("Object created.");
		}
		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





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.