亚信联创2011.9.17招聘会笔试题
来源:互联网 发布:navicat for mysql安卓 编辑:程序博客网 时间:2024/05/02 14:03
一、单选题
1、对于如下程序:
#include <iostream>
using namespace std;
class A
{
public:
A()
{
cout<<"A"<<endl;
}
};
int main(void)
{
A a[4], b,*p;
}
会输出多少个A?( )
A、2
B、3
C、5
D、6
答案:C
答案注释:p只是一个对象指针,并没有指向一个对象的内存空间,所以没有调用构造函数。
二、问答题
2、头文件中的 ifndef/define/endif 有什么作用?
答案:防止该头文件被重复引用,避免变量、类型等被重新定义。
3、const 有什么用途?(请至少说明两种)
答案:
(1)可以定义 const 常量。
(2)const可以修饰函数的参数、返回值,甚至函数的定义体。被const修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性。
4、如下的字符串函数,用于生存一个字符串 ”连接号码异常” ,并返回它的指针
char* strfun()
{
char str[20];
strcpy(str, “连接号码异常”);
printf(“%s \n”, str); //printf语句1
return str;
}
void main()
{
char *pstr = strfun();
printf("%s \n", pstr); //printf语句2
}
问题1 : printf语句1和printf语句2哪个能在屏幕上正在打印出来?
问题2 : 如果不能正常在屏幕上打印出字符串,请说明原因。
问题3 : 如果不修改strfun的声明,请问该如何修改上述程序的错误。
答案:
问题1:语句1可以正常打印,语句2不能正常打印;
问题2:语句2使用的指针所指向的内存空间str[20],在函数strfun返回时已经被释放了;
问题3:可以将函数strfun中的语句char str[20];改为char *str = new char[20];
分数:30
5、下面是交换两个double型数据的函数,
void swap( double* p1, double* p2 )
{
double *p;
*p = *p1;
*p1 = *p2;
*p2 = *p;
}
void main()
{
double a = 0.1;
double b = 0.2;
swap( &a, &b );
}
请找出上述代码的错误,指出错误的原因,并改正。
答案:函数swap中混淆了double型指针与double型变量的差别,对于一个未初始化的指针访问其内存空间是非常危险的。对swap函数修改如下:
void swap( double* p1, double* p2 )
{
double p;
p = *p1;
*p1 = *p2;
*p2 =p;
}
6、在电信业务的后台处理程序中,经常会涉及到处理字符串,除了用char *处理字符串之外,C++还为我们提供了封装了的字符串类string,其本质也是用一个动态数组来保存字符串,类String的原型为:
class String
{
public:
String(const char *str = NULL); // 普通构造函数
String(const String &other); // 拷贝构造函数
~String(void); // 析构函数
String & operate =(const String &other); // 赋值函数
private:
char *m_data; // 用于保存字符串
};
请编写String的上述4个函数普通构造函数、拷贝构造函数、析构函数和赋值函数。
答案:
class String
{
private:
char *m_data;
public:
String();
String(const char *str = NULL);
String(const String &other);
~String(void);
String & operator =(const String &other);
};
String::String()
{
m_data = NULL;
}
String::String(const char *str = NULL) //带一个指针的普通构造函数
{
if(str == NULL)
{
m_data = new char[1];
assert(m_data != NULL);
*m_data = '\0';
}
else
{
int length=strlen(str);
m_data = new char[length+1];
assert(m_data != NULL);
strcpy(m_data,str);
}
}
String::String(const String &other) //拷贝构造函数
{
m_data = new char[other.length+1];
assert(m_data != NULL);
strcpy((*this).m_data,other.m_data);
}
String::~String(void) //析构函数
{
if(m_data != NULL)
{
delete m_data;
m_data = NULL;
}
}
String & String::operator=(const String &other) //赋值函数
{
if(&other != this)
{
delete [](*this).m_data;
(*this).m_data = new char[other.length+1];
assert((*this).m_data != NULL);
strcpy((*this).m_data,other.m_data);
}
}
由“名企笔试题库”收集整理
- 亚信联创2011.9.17招聘会笔试题
- 亚信联创2011.9.17招聘会笔试题
- 亚信联创2011.9.17招聘会笔试题
- 亚信联创2011.9.17招聘会笔试题
- 亚信联创2011.9.17招聘会笔试题
- 【转】亚信联创2011.9.17招聘会笔试题 .
- 阿里云2011.9.17招聘会笔试题
- 【转】阿里云2011.9.17招聘会笔试题
- 阿里云2011.9.17招聘会笔试题
- 阿里云2011.9.17招聘会笔试题
- 阿里云2011.9.17招聘会笔试题
- 阿里云2011.9.17招聘会笔试题
- 阿里云2011.9.17招聘会笔试题
- 阿里云2011.9.17招聘会笔试题
- 阿里云2011.9.17招聘会笔试题
- 亚新联创 2011.9.17招聘会笔试题目
- 网新恒天2011.9.21招聘会笔试题
- 网新恒天2011.9.21招聘会笔试题
- 人人都能用英语
- WebStorm+Node.js开发环境的配置
- Intent显示启动与隐式启动
- 天气信息
- iOS的几种设计模式
- 亚信联创2011.9.17招聘会笔试题
- C#中常见的系统内置委托用法详解
- Leetcode 211. Add and Search Word - Data structure design
- 发出商品
- Log4j slf4j 配置简单介绍
- jQuery中html()、text()和val()的区别、jQuery中attr()和prop()的区别
- 新年目标计划
- 系统定位
- Android样式的开发:View Animation篇