C++/MFC 試題

?

一.填空題 (26 )

1 WIN32 平臺下, sizeof(short) = __2__ sizeof(int) = __4__ sizeof(long) = __4__ (3 )

2 .請給出如下程序的結果 (2 )

int a = 3;

int b = a << 3;

a = __3__ b = __24__

3 .請 給出如下程序的結果 (2 )

int aaa = 0x01;

htonl(aaa) = _16777216___ //這題運行顯示的是16777216,我覺得可能是隨機值

4 .請給出如下程序的結果 (2 )

#define MAX_NUM 100+200

int nTemp = MAX_NUM*10;

Temp = __2100__

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 )__CObject __

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;

}

?