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

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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.