亚信笔试题
亚
亚亚亚亚信联
信联信联信联信联创部
创部创部创部创部分笔
分笔分笔分笔分笔试题
试题试题试题试题。
。。。。(
((((请
请请请请注意
注意注意注意注意保密
保密保密保密保密,
,,,,由
由由由由l
lllla
aaaar
rrrrr
rrrry
yyyy制
制制制制作
作作作作)
))))
卐头文件中的
i
iiiif
ffffn
nnnnd
dddde
eeeef
ffff/
////d
dddde
eeeef
ffffi
iiiin
nnnne
eeee/
////e
eeeen
nnnnd
ddddi
iiiif
ffff干什么用?
答:防止该头文件被重复引用。
卐
#
####i
iiiin
nnnnc
ccccl
llllu
uuuud
dddde
eeee
ffffi
iiiil
lllle
eeeen
nnnna
aaaam
mmmme
eeee.
....h
hhhh>
>>>>和
#
####i
iiiin
nnnnc
ccccl
llllu
uuuud
dddde
eeee“
““““f
ffffi
iiiil
lllle
eeeen
nnnna
aaaam
mmmme
eeee.
....h
hhhh”
””””有什么区别?
答:对于#include
,编译器从标准库路径开始搜索
filename.h
对于#include
“filename.h”,编译器从用户的工作路径开始搜索
filename.h
卐
c
cccco
oooon
nnnns
sssst
tttt有什么用途?(请至少说明两种)
答:(1)可以定义
const常量
(2)const可以修饰函数的参数、返回值,甚至函数的定义体。被
const修饰的东
西都受到强制保护,可以预防意外的变动,能提高程序的健壮性。
卐在
C
CCCC+
+++++
++++程序中调用被
C
CCCC编译器编译后的函数,为什么要加
e
eeeex
xxxxt
tttte
eeeer
rrrrn
nnnn“
““““C
CCCC”
””””?
答:C++语言支持函数重载,C语言不支持函数重载。函数被
C++编译后在库中的名字
与
C语言的不同。假设某个函数的原型为:
void
foo(int
x,
int
y);
该函数被
C编译器编译后在库中的名字为_foo,而
C++编译器则会产生像
_foo_int_int之类的名字。
C++提供了
C连接交换指定符号
extern“C”来解决名字匹配问题。
卐请简述以下两个
f
ffffo
oooor
rrrr循环的优缺点
for
(i=0;
i
i++)
{
if
(condition)
DoSomething();
else
DoOtherthing();
}
优点:程序简洁
缺点:多执行了
N-1次逻辑判断,并且
打断了循环“流水线”作业,使得编译
器不能对循环进行优化处理,降低了效
率。
if
(condition)
{
for
(i=0;
i
i++)
DoSomething();
}
else
1
{
for
(i=0;
i
i++)
DoOtherthing();
}
优点:循环的效率高
缺点:程序不简洁
卐在电信业务的后台处理程序中,经常会涉及到处理字符串,除了用
char
*处理字符串之
外,C++还为我们提供了封装了的字符串类
string,其本质也是用一个动态数组来保存字符
串,类
S
SSSSt
ttttr
rrrri
iiiin
nnnng
gggg的原型为:
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个函数普通构造函数、拷贝构造函数、析构函数和赋值函数。
析构函数
String::~String(void)
{
delete
[]
m_data;
}
普通构造函数
String::String(const
char
*str)
{
if(str==NULL)
{
m_data
=
new
char[1];
*m_data
=
‘\0’;
}
else
{
int
length
=
strlen(str);
m_data
=
new
char[length+1];
strcpy(m_data,
str);
2
}
}
拷贝构造函数
String::String(const
String
&other)
{
int
length
=
strlen(other.m_data);
m_data
=
newchar[length+1];
strcpy(m_data,
other.m_data);
}
赋值函数
String
&
String::operate
=(const
String
&other)
{
//
(1)检查自赋值
if(this
==
&other)
return
*this;
//(2)释放原有的内存资源
delete
[]
m_data;
//(3)分配新的内存资源,并复制内容
int
length
=
strlen(other.m_data);
m_data
=
newchar[length+1];
strcpy(m_data,
other.m_data);
//(4)返回本对象的引用
return
*this;
}
卐有关内存的思考题
void
GetMemory(char
*p)
{
p
=
(char
*)malloc(100);
}
voidTest(void)
{
char
*str
=
NULL;
GetMemory(str);
strcpy(str,
"hello
world");
printf(str);
}
请问运行
Test函数会有什么样的结果?
答:程序崩溃。
因为
GetMemory并不能传递动态内存,
Test函数中的
str一直都是
NULL。
strcpy(str,
"hello
world");将使程序崩
溃。
char
*GetMemory(void)
{
charp[]
=
"hello
world";
return
p;
}
voidTest(void)
{
char
*str
=
NULL;
str
=
GetMemory();
printf(str);
}
请问运行
Test函数会有什么样的结果?
答:可能是乱码。
因为
GetMemory返回的是指向“栈内存”
的指针,该指针的地址不是
NULL,但其原
现的内容已经被清除,新内容不可知。
void
GetMemory2(char
**p,
int
num)
{
*p
=
(char
*)malloc(num);
}
voidTest(void)
{
char
*str
=
NULL;
GetMemory(&str,100);
strcpy(str,
"hello");
printf(str);
}
请问运行
Test函数会有什么样的结果?
答:
(1)能够输出
hello
(2)内存泄漏
voidTest(void)
{
char
*str
=
(char
*)
malloc(100);
strcpy(str,
“hello”);
free(str);
if(str
!=
NULL)
{
strcpy(str,
“world”);
printf(str);
}
}
请问运行
Test函数会有什么样的结果?
答:篡改动态内存区的内容,后果难以预
料,非常危险。
因为
free(str);之后,
str成为野指针,
if(str
!
=
NULL)语句不起作用。