2022年經(jīng)典C語(yǔ)言測(cè)試題_第1頁(yè)
2022年經(jīng)典C語(yǔ)言測(cè)試題_第2頁(yè)
2022年經(jīng)典C語(yǔ)言測(cè)試題_第3頁(yè)
2022年經(jīng)典C語(yǔ)言測(cè)試題_第4頁(yè)
2022年經(jīng)典C語(yǔ)言測(cè)試題_第5頁(yè)
已閱讀5頁(yè),還剩5頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、一個(gè)很牛的人出的被眾多知名it 企業(yè)用作面試題的看似很簡(jiǎn)單c 語(yǔ)言測(cè)試題c+/c 試題本試題僅用于考查c+/c 程序員的基本編程技能。內(nèi)容限于c+/c 常用語(yǔ)法,不涉及數(shù)據(jù)結(jié)構(gòu)、算法以及深?yuàn)W的語(yǔ)法??荚嚦煽?jī)能反映出考生的編程質(zhì)量以及對(duì)c+/c 的理解程度,但不能反映考生的智力和軟件開發(fā)能力。筆試時(shí)間90 分鐘。請(qǐng)考生認(rèn)真答題,切勿輕視。一、請(qǐng)?zhí)顚慴ool , float, 指針變量與“零值”比較的if 語(yǔ)句。( 10 分)提示:這里“零值”可以是0, 0.0 , false 或者“空指針”。例如int 變量n 與“零值”比較的if 語(yǔ)句為:if ( n = 0 ) if ( n != 0 )

2、以此類推。請(qǐng)寫出bool flag 與“零值”比較的if 語(yǔ)句:請(qǐng)寫出float x 與“零值”比較的if 語(yǔ)句:請(qǐng)寫出char *p 與“零值”比較的if 語(yǔ)句:二、以下為windows nt下的 32 位 c+ 程序,請(qǐng)計(jì)算sizeof 的值( 10 分)char str = “ hello ” ;char *p = str ; int n = 10; 請(qǐng)計(jì)算sizeof (str ) = sizeof ( p ) = sizeof ( n ) = void func ( char str100) 請(qǐng)計(jì)算sizeof( str ) = void *p = malloc( 100 ); 請(qǐng)計(jì)

3、算sizeof ( p ) =精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 1 頁(yè),共 10 頁(yè) - - - - - - - - -精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 1 頁(yè),共 10 頁(yè) - - - - - - - - -高質(zhì)量 c+/c 編程指南, v 1.0 2001 page 2 of 10 三、簡(jiǎn)答題(25 分)1、 頭文件中的ifndef/define/endif 干什么用?2、 #include 和#include “ filename.h” 有什么區(qū)別?3、 const 有什么

4、用途?(請(qǐng)至少說(shuō)明兩種)4、在 c+ 程序中調(diào)用被c 編譯器編譯后的函數(shù),為什么要加extern “ c”聲明?5、請(qǐng)簡(jiǎn)述以下兩個(gè)for 循環(huán)的優(yōu)缺點(diǎn)/ 第一個(gè)for (i=0; in; i+) if (condition) dosomething(); else dootherthing(); / 第二個(gè)if (condition) for (i=0; in; i+) dosomething(); else for (i=0; i= - epsinon) & (x =”或“ =”此類形式。如下是錯(cuò)誤的寫法,不得分。if (x = 0.0) if (x != 0.0) 請(qǐng)寫出char

5、*p 與“零值”比較的if 語(yǔ)句。( 3 分)標(biāo)準(zhǔn)答案:if (p = null)if (p != null)如下寫法均屬不良風(fēng)格,不得分。if (p = 0) if (p != 0) if (p) if (!) 二、以下為windows nt下的 32 位 c+ 程序,請(qǐng)計(jì)算sizeof 的值( 10 分)char str = “ hello ” ;char *p = str ; int n = 10; 請(qǐng)計(jì)算sizeof (str ) = 6 ( 2 分)sizeof ( p ) = 4 (2 分)sizeof ( n ) = 4 (2 分)void func ( char str100)

6、 請(qǐng)計(jì)算sizeof( str ) = 4 ( 2 分) void *p = malloc( 100 ); 請(qǐng)計(jì)算sizeof ( p ) = 4 ( 2 分)精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 5 頁(yè),共 10 頁(yè) - - - - - - - - -精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 5 頁(yè),共 10 頁(yè) - - - - - - - - -高質(zhì)量 c+/c 編程指南, v 1.0 2001 page 6 of 10 三、簡(jiǎn)答題(25 分)1、頭文件中的ifndef/define/e

7、ndif 干什么用?(5 分)答:防止該頭文件被重復(fù)引用。2、 #include 和#include “ filename.h” 有什么區(qū)別?(5 分)答:對(duì)于#include ,編譯器從標(biāo)準(zhǔn)庫(kù)路徑開始搜索filename.h 對(duì)于 #include “ filename.h”,編譯器從用戶的工作路徑開始搜索filename.h 3、 const 有什么用途?(請(qǐng)至少說(shuō)明兩種)(5 分)答: ( 1)可以定義const 常量(2) const 可以修飾函數(shù)的參數(shù)、返回值,甚至函數(shù)的定義體。被const 修飾的東西都受到強(qiáng)制保護(hù),可以預(yù)防意外的變動(dòng),能提高程序的健壯性。4、在 c+ 程序中調(diào)用被

8、c 編譯器編譯后的函數(shù),為什么要加extern “ c”?(5 分)答: c+ 語(yǔ)言支持函數(shù)重載,c 語(yǔ)言不支持函數(shù)重載。函數(shù)被c+ 編譯后在庫(kù)中的名字與c 語(yǔ)言的不同。假設(shè)某個(gè)函數(shù)的原型為:void foo(int x, int y); 該函數(shù)被c 編譯器編譯后在庫(kù)中的名字為_foo,而c+ 編譯器則會(huì)產(chǎn)生像_foo_int_int之類的名字。c+ 提供了c 連接交換指定符號(hào)extern “ c”來(lái)解決名字匹配問(wèn)題。5、請(qǐng)簡(jiǎn)述以下兩個(gè)for 循環(huán)的優(yōu)缺點(diǎn)(5 分)for (i=0; in; i+) if (condition) dosomething(); else dootherthing

9、(); if (condition) for (i=0; in; i+) dosomething(); else for (i=0; in; i+) dootherthing(); 優(yōu)點(diǎn):程序簡(jiǎn)潔缺點(diǎn):多執(zhí)行了n-1 次邏輯判斷,并且打斷了循環(huán)“流水線”作業(yè),使得編譯器不能對(duì)循環(huán)進(jìn)行優(yōu)化處理,降低了效率。優(yōu)點(diǎn):循環(huán)的效率高缺點(diǎn):程序不簡(jiǎn)潔精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 6 頁(yè),共 10 頁(yè) - - - - - - - - -精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 6 頁(yè),共 10 頁(yè)

10、 - - - - - - - - -高質(zhì)量 c+/c 編程指南, v 1.0 2001 page 7 of 10 四、有關(guān)內(nèi)存的思考題(每小題5 分,共20 分)void getmemory(char *p) p = (char *)malloc(100); void test(void) char *str = null; getmemory(str); strcpy(str, hello world); printf(str); 請(qǐng)問(wèn)運(yùn)行test 函數(shù)會(huì)有什么樣的結(jié)果?答:程序崩潰。因?yàn)?getmemory并不能傳遞動(dòng)態(tài)內(nèi)存,test 函數(shù)中的 str一直都是 null。strcpy(st

11、r, hello world);將使程序崩潰。char *getmemory(void) char p = hello world; return p; void test(void) char *str = null; str = getmemory(); printf(str); 請(qǐng)問(wèn)運(yùn)行test 函數(shù)會(huì)有什么樣的結(jié)果?答:可能是亂碼。因?yàn)間etmemory 返回的是指向“棧內(nèi)存”的指針,該指針的地址不是 null,但其原現(xiàn)的內(nèi)容已經(jīng)被清除,新內(nèi)容不可知。void getmemory2(char *p, int num) *p = (char *)malloc(num); void tes

12、t(void) char *str = null; getmemory(&str, 100); strcpy(str, hello); printf(str); 請(qǐng)問(wèn)運(yùn)行test 函數(shù)會(huì)有什么樣的結(jié)果?答:( 1)能夠輸出hello( 2)內(nèi)存泄漏void test(void) char *str = (char *) malloc(100); strcpy(str, “ hello” ); free(str); if(str != null) strcpy(str, “ world ” ); printf(str); 請(qǐng)問(wèn)運(yùn)行test 函數(shù)會(huì)有什么樣的結(jié)果?答:篡改動(dòng)態(tài)內(nèi)存區(qū)的內(nèi)容,

13、后果難以預(yù)料,非常危險(xiǎn)。因?yàn)?free(str);之后, str成為野指針,if(str != null)語(yǔ)句不起作用。精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 7 頁(yè),共 10 頁(yè) - - - - - - - - -精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 7 頁(yè),共 10 頁(yè) - - - - - - - - -高質(zhì)量 c+/c 編程指南, v 1.0 2001 page 8 of 10 五、編寫strcpy 函數(shù)( 10 分)已知 strcpy 函數(shù)的原型是char *strcpy(char

14、 *strdest, const char *strsrc); 其中 strdest 是目的字符串,strsrc 是源字符串。( 1)不調(diào)用c+/c 的字符串庫(kù)函數(shù),請(qǐng)編寫函數(shù)strcpy char *strcpy(char *strdest, const char *strsrc); assert(str dest!=null) & (strsrc !=null); / 2分 char *address = str dest; / 2分 while( (*str dest+ = * strsrc +) != 0 ) / 2分 null ; return address ; / 2分

15、( 2) strcpy 能把 strsrc 的內(nèi)容復(fù)制到strdest ,為什么還要char * 類型的返回值?答:為了實(shí)現(xiàn)鏈?zhǔn)奖磉_(dá)式。/ 2分例如int length = strlen( strcpy( strdest, “hello world”) );六、編寫類string的構(gòu)造函數(shù)、析構(gòu)函數(shù)和賦值函數(shù)(25 分)已知類string 的原型為:class string public: string(const char *str = null); / 普通構(gòu)造函數(shù)string(const string &other); / 拷貝構(gòu)造函數(shù) string(void); / 析構(gòu)函數(shù)s

16、tring & operate =(const string &other); / 賦值函數(shù) private: char *m_data; / 用于保存字符串; 請(qǐng)編寫string的上述4 個(gè)函數(shù)。標(biāo)準(zhǔn)答案:/ string的析構(gòu)函數(shù)string: string(void) / 3分 delete m_data; / 由于 m_data 是內(nèi)部數(shù)據(jù)類型,也可以寫成 delete m_data; 精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 8 頁(yè),共 10 頁(yè) - - - - - - - - -精品學(xué)習(xí)資料 可選擇p d f - -

17、 - - - - - - - - - - - - 第 8 頁(yè),共 10 頁(yè) - - - - - - - - -高質(zhì)量 c+/c 編程指南, v 1.0 2001 page 9 of 10 / string的普通構(gòu)造函數(shù)string: string(const char *str) / 6分 if(str=null) m_data = new char1; / 若能加 null 判斷則更好*m_data = 0 ; else int length = strlen(str); m_data = new charlength+1; / 若能加 null 判斷則更好strcpy(m_data, st

18、r); / 拷貝構(gòu)造函數(shù)string:string(const string &other) / 3分 int length = strlen(other.m_data); m_data = new charlength+1; / 若能加 null 判斷則更好strcpy(m_data, other.m_data); / 賦值函數(shù)string & string:operate =(const string &other) / 13分 / (1) 檢查自賦值 / 4分if(this = &other) return *this; / (2) 釋放原有的內(nèi)存資源 / 3分delete m_data; / ( 3)分配新的內(nèi)存資源,并復(fù)制內(nèi)容 / 3分int length = strlen(other.m_data); m_data = new charlength+1; / 若能加 null 判斷則更好strcpy(m_data, other.m_data); / ( 4)返回本對(duì)象的引用 / 3分return *this; 精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 9 頁(yè),共 10 頁(yè) - - - - - - - - -精品學(xué)習(xí)資

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論