C ++编程构造函数,析构函数能力问题和解答

C ++构造函数和析构函数能力问题列表 (List of C++ Constructor and Destructor Aptitude Questions & Answers)

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

  2. No

Answer
Correct Answer - 1

Yes
Yes, we can define a class without creating constructors.

1)我们可以在不创建 构造函数的情况下定义一个类吗?
  1. 没有

回答
正确答案-1


是的,我们可以定义一个类而不创建构造函数。

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

Answer
Correct Answer - 4

Both 1 and 2

2)以下类的默认构造函数的正确形式是什么?
  1. 公开:sample(){}

  2. 公开:sample(){x = 0; y = 0;}

  3. 公开:sample(int a,int b){x = a; y = b;}

  4. 1和2

回答
正确答案-4

1和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

Answer
Correct Answer - 1

Compile Time Error
There is no default constructor in this class definition, and we cannot create an object like this, the correct form of creating object is : sample s(10,20); - pass two values because parameterized constructor is exists in the class definition.

3)以下程序的输出是什么?
  1. 编译时错误

  2. 运行时错误

  3. 没错

  4. 警告

回答
正确答案-1

编译时错误
这个类定义中没有默认的构造函数,我们不能创建这样的对象,创建对象的正确形式是:sample s(10,20); -传递两个值,因为类定义中存在参数化构造函数。

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

Answer
Correct Answer - 2

Object Created.

4)以下程序的输出是什么?
  1. 编译时错误

  2. 对象创建。

  3. 运行时错误

  4. 无法预测

回答
正确答案-2

对象创建。

翻译自: https://www.includehelp.com/cpp-programming/constructor-and-destructor-aptitudue-questions-and-answers.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值