此套试题总分105,考试时间为1小时,考试分数为70分为合格。
C++/MFC试题
一.填空题(26分)
1. WIN32平台下,sizeof(short) = ____,sizeof(int) = ____,sizeof(long) = ____。(3分)
2.请给出如下程序的结果(2分)
int a = 3;
int b = a << 3;
a = ____,b = ____。
3.请给出如下程序的结果(2分)
int aaa = 0x01;
htonl(aaa) = ____。
4.请给出如下程序的结果(2分)
#define MAX_NUM 100+200
int nTemp = MAX_NUM*10;
则Temp = ____。
5.请给出如下程序的结果(3 分)
char szTemp[1000] = “”;
int nLen1 = sizeof(szTemp);
int nLen2 = strlen(szTemp);
strcpy(szTemp, “abc”);
int nLen3 = sizeof(szTemp);
int nLen4 = strlen(szTemp);
int nTemp[100];
int *pTemp = nTemp;
int nLen5 = sizeof(pTemp);
char szResult[200] = “”;
sprintf(szResult, “%d,%d,%d,%d,%02d.”, nLen1, nLen2, nLen3, nLen4, nLen5);
则szResult = ____。
6.MFC中,大部分类是从哪个类继承而来(CCmdTarget、CObject、CWinApp、CWnd)?(2分)____
7.内存是进程范围or线程范围;____
CPU调度时,针对进程or线程;____
函数调用堆栈,针对进程or线程。____(3分)
8.调用函数bbb后,输出是什么(4分)
void ccc(int x)
{
char szTemp[10] = “”;
x = 2;
sprintf(szTemp, “%d,”, x);
afxDump << szTemp;
if(x = 3)
{
int x = 4;
sprintf(szTemp, “%d,”, x);
afxDump << szTemp;
}
sprintf(szTemp, “%d,”, x);
afxDump << szTemp;
}
void bbb()
{
char szTemp[10] = “”;
int x = 7;
ccc(x);
sprintf(szTemp, “%d,”, x);
afxDump << szTemp;
}
二.改错题(总共15分,每题5分)。
1.下面代码有何错误
void func1()
{
int *pa = NULL;
func2(pa);
delete pa;
}
void func2(int *pb)
{
pb = new int(5);
}
2.下面代码有何错误
void func2(int *value)
{
*value = 2;
}
void func1()
{
int *p = 0;
func2(p);
}
3.
int func1(int& b)
{
return 0;
}
void func2()
{
int bbb = 3;
func1(&bbb);
func1(bbb);
}
func2中有何错误,func1的参数b的类型是什么。
三.简答题(64分)
1.请简述C、C++、VC、MFC在概念上的区别(4分)
2.请写一个函数重载的简单例子(4分)
3.用什么函数开启新进程、线程。(4分)
4.SendMessage和PostMessage有什么区别(4分)
5.WaitForSingleObject有何作用;m_pThrd的类型是CWinThread*时,WaitForSingleObject(m_pThrd->m_hThread, INFINITE);有何作用。(4分)
6. __stdcall、__cdecl、__pascal在什么方面有所不同。(4分)
7.请把下述代码加上异常处理。(6分)
int MyWriteFile(CString strFileName, CString strText)
{
int nRet = 0;
CFile myFile;
myFile.Open(strFileName, CFile::modeWrite|CFile::shareExclusive|CFile::modeCreate, NULL);
int nLen = strText.GetLength();
myFile.Write((char*)(LPCSTR)strText, nLen);
myFile.Close();
return nRet;
}
8.请解释“func”为何种类型,这种类型的作用什么,变量ttt 的值是多少?(6分)
typedef int (*func)(int, int*);
int xxx(int a, int *p)
{
return a + *p;
}
int dowork(func aaa, int bbb, int *ccc)
{
return aaa(bbb, ccc);
}
int sss = 4;
int ttt = dowork(&xxx, 3, &sss);
9.请问下述代码中: int operator+(…)起什么作用?this是什么?ccc 的值最终为多少?(6分)
class Fruit
{
public:
Fruit()
{
weight = 2;
}
Fruit(int w)
{
weight = w;
}
int operator+(Fruit f)
{
return this->weight * f.weight;
}
private:
int weight;
};
Fruit aaa;
Fruit bbb(4);
int ccc = aaa + bbb;
10.请解释下面代码采用了何种C++特性(C语言不具备),作用是什么?(6分)
template<typename T>
T sum(T a, T b)
{
return (a + b);
}
11.请解释aaa.h中下面代码的功能(5分)
#if !defined(AFX_MYSUDU_H__9B952BEA_A051_4026_B4E5_0598A39D2DA4__INCLUDED_)
#define AFX_MYSUDU_H__9B952BEA_A051_4026_B4E5_0598A39D2DA4__INCLUDED_
… …
#endif
12.CMemoryState主要功能是什么(5分)
13.请阅读下述代码,写出程序执行的结果(6分)
#include <iostream>
using namespace std;
class CBase
{
public:
virtual void print()
{
cout<< “base” << endl;
}
void DoPrint()
{
print();
}
};
class CChild1: public CBase
{
public:
virtual void print()
{
cout<< “child1” << endl;
}
};
class CChild2: public CBase
{
public:
virtual void print()
{
cout<< “child2” << endl;
}
};
void DoPrint(CBase *base)
{
base->DoPrint();
}
void main()
{
CBase* base = new CBase();
CChild1* child1 = new CChild1();
CChild2* child2 = new CChild2();
DoPrint(child1);
DoPrint(child2);
DoPrint(base);
delete base;
base = child1;
base->print();
delete child1;
delete child2;
}