p; a.print(); } void main(void) { A a, *pa,*pb,*pc; B b; C c;
pa=&a; pb=&b; pc=&c;
a.print(); b.print(); c.print();
pa->print(); pb->print(); pc->print();
print(a); print(b); print(c); } A: A::print() B::print() C::print() A::print() B::print() C::print() A::print() A::print() A::print() 31. 试编写函数判断计算机的字节存储顺序是开序(little endian)还是降序(bigendian) 答: bool IsBigendian() { unsigned short usData = 0x1122; unsigned char *pucData = (unsigned char*)&usData; return (*pucData == 0x22); } -------------------------------------------------------------------------- 32.简述Critical Section和Mutex的不同点 答: 对几种同步对象的总结 1.Critical Section A.速度快 B.不能用于不同进程 C.不能进行资源统计(每次只可以有一个线程对共享资源进行存取) 2.Mutex A.速度慢 B.可用于不同进程 C.不能进行资源统计 3.Semaphore A.速度慢 B.可用于不同进程 C.可进行资源统计(可以让一个或超过一个线程对共享资源进行存取) 4.Event A.速度慢 B.可用于不同进程 C.可进行资源统计 -------------------------------------------------------------------------- 33.一个数据库中有两个表: 一张表为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 -------------------------------------------------------------------------- 34.请指出下列程序中的错误并且修改 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); } -------------------------------------------------------------------------- 35.程序改错 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--;} -------------------------------------------------------------------------- 36.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)中,结合第一次的结果就知道了。 -------------------------------------------------------------------------- 37.static变量和static 函数各有什么特点? 答: static变量:在程序运行期内一直有效,如果定义在函数外,则在编译单元内可见,如果在函数内,在在定义的block内可见; static函数:在编译单元内可见; ---------------------------------------------- |