您的位置:首页 > 编程语言 > C语言/C++

常见C++笔试题目整理(含答案)8

2010-03-11 23:59 441 查看
网上流传的一份常见C++笔试题目汇总,供各位有找工作需要的同学参考之用,因为原文较长,遂采用连载形式,预计需要连载10期左右,有耐心的同学就请一直跟下去吧,相信一定会有所收获。
提前说明一点,题目来在网络,答案是网上资料配的仅供参考,已经有许多大侠发现了其中的问题,如有同学觉得哪道题目有异议,欢迎讨论!

题目60-66
--------------------------------------------------------------------------
60.
试编写函数判断计算机的字节存储顺序是开序(little endian)还是降序(bigendian)
答:
bool IsBigendian()
{
unsigned short usData = 0x1122;
unsigned char *pucData = (unsigned char*)&usData;
return (*pucData == 0x22);
}
--------------------------------------------------------------------------
61.简述Critical Section和Mutex的不同点
答:
对几种同步对象的总结
1.Critical Section
A.速度快
B.不能用于不同进程
C.不能进行资源统计(每次只可以有一个线程对共享资源进行存取)
2.Mutex
A.速度慢
B.可用于不同进程
C.不能进行资源统计
3.Semaphore
A.速度慢
B.可用于不同进程
C.可进行资源统计(可以让一个或超过一个线程对共享资源进行存取)
4.Event
A.速度慢
B.可用于不同进程
C.可进行资源统计
--------------------------------------------------------------------------
62.一个数据库中有两个表:
一张表为Customer,含字段ID,Name;
一张表为Order,含字段ID,CustomerID(连向Customer中ID的外键),Revenue;
写出求每个Customer的Revenue总和的SQL语句。
建表
create table customer
(
ID int primary key,Name char(10)
)
go
create table [order]
(
ID int primary key,CustomerID int foreign key references customer(id) , Revenue float
)
go
--查询
select Customer.ID, sum( isnull([Order].Revenue,0) )
from customer full join [order]
on( [order].customerid=customer.id )
group by customer.id
--------------------------------------------------------------------------
63.请指出下列程序中的错误并且修改
void GetMemory(char *p){
p=(char *)malloc(100);
}
void Test(void){
char *str=NULL;
GetMemory=(str);
strcpy(str,"hello world");
printf(str);
}
A:错误--参数的值改变后,不会传回
GetMemory并不能传递动态内存,Test函数中的 str一直都是 NULL。
strcpy(str, "hello world");将使程序崩溃。
修改如下:
char *GetMemory(){
char *p=(char *)malloc(100);
return p;
}
void Test(void){
char *str=NULL;
str=GetMemory(){
strcpy(str,"hello world");
printf(str);
}
方法二:void GetMemory2(char **p)变为二级指针.
void GetMemory2(char **p, int num)
{
*p = (char *)malloc(sizeof(char) * num);
}
--------------------------------------------------------------------------
64.程序改错
class mml
{
private:
static unsigned int x;
public:
mml(){ x++; }
mml(static unsigned int &) {x++;}
~mml{x--;}
pulic:
virtual mon() {} = 0;
static unsigned int mmc(){return x;}
......
};
class nnl:public mml
{
private:
static unsigned int y;
public:
nnl(){ x++; }
nnl(static unsigned int &) {x++;}
~nnl{x--;}
public:
virtual mon() {};
static unsigned int nnc(){return y;}
......
};
代码片断:
mml* pp = new nnl;
..........
delete pp;
A:
基类的析构函数应该为虚函数
virtual ~mml{x--;}
--------------------------------------------------------------------------
65.101个硬币100真、1假,真假区别在于重量。请用无砝码天平称两次给出真币重还是假币重的结论。
答:
101个先取出2堆,
33,33
第一次称,如果不相等,说明有一堆重或轻
那么把重的那堆拿下来,再放另外35个中的33
如果相等,说明假的重,如果不相等,新放上去的还是重的话,说明假的轻(不可能新放上去的轻)
第一次称,如果相等的话,这66个肯定都是真的,从这66个中取出35个来,与剩下的没称过的35个比
下面就不用说了
方法二:
第3题也可以拿A(50),B(50)比一下,一样的话拿剩下的一个和真的比一下。
如果不一样,就拿其中的一堆。比如A(50)再分成两堆25比一下,一样的话就在
B(50)中,不一样就在A(50)中,结合第一次的结果就知道了。
--------------------------------------------------------------------------
66.static变量和static 函数各有什么特点?
答:
static变量:在程序运行期内一直有效,如果定义在函数外,则在编译单元内可见,如果在函数内,在在定义的block内可见;
static函数:在编译单元内可见;

未完,待续,进入“我的笔记”看连载的系列的下一回!
最新作品《C语言参悟之旅》全新上市,敬请关注!
官方网站:http://www.tqbooks.net/product/gb/product_detail.asp?catalogid=10&productid=1474
China-pub有售:http://www.china-pub.com/49980
介绍文章:http://student.csdn.net/space.php?uid=113322&do=blog&id=20285
如果你想同我交流,欢迎点击链接http://student.csdn.net/invite.php?u=113322&c=a139a65a1494291d和我成为好友!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: