1. 程式人生 > >找工作筆試面試那些事兒(6)---簡單測試題

找工作筆試面試那些事兒(6)---簡單測試題

作者:寒小陽

時間:2013年9月。

出處:http://blog.csdn.net/han_xiaoyang/article/details/10894159。
宣告:版權所有,轉載請註明出處,謝謝。

C/C++測試題
      前幾個部分從程式設計風格、函式設計、記憶體管理和類的一些相關知識和注意點出發,對C/C++基礎知識和筆試面試易考察的點進行了歸納總結。下面這是針對前述知識出的一份測試題,也是一些易考點,大家認真閱讀了之前的部分,這些題大家都很容易就能做出來。

一、請填寫BOOL, float,  指標變數 與“零值”比較的 if  語句。 
提示:這裡“零值”可以是0 ,  0.0 , FA LSE 或者“空指標”。例如 int  變數 n  與“零值”

比較的 if  語句為: 

        if ( n  ==  0 )  

        if ( n != 0 ) 

以此類推。 

 

請寫出 BOOL  flag  與“零值”比較的 if  語句: 

 

 

請寫出 float  x  與“零值”比較的 if  語句: 

 

 

請寫出 char  *p  與“零值”比較的 if  語句: 

二、以下為Windows xp 下的32位C++ 程式,請計算sizeof 的值
char str[]  = “Hello” ; 

char  *p  =  str ; 

int  n  =  10; 

請計算 

sizeof (str ) =          

          

sizeof (p)   =          

           

sizeof (n) = 

void Func ( char str[100]) 

}

請計算 

  sizeof( str )  =     

  void *p = malloc( 100 ); 

請計算 

sizeof ( p ) =

三、簡答

1 、標頭檔案中的 ifndef/define/endif  幹什麼用? 

 

2 、#include <filename.h>    和  #include“filename .h”有什麼區別? 

 

3 、const有什麼用途?(請至少說明兩種) 

 

4 、在C++ 程式中呼叫被 C編譯器編譯後的函式,為什麼要加 extern“C” 宣告? 

5 、請簡述以下兩個for 迴圈的優點和缺點

//  第一個 

for (i=0; i<N; i++) 

if (condition) 

    DoSomething(); 

else 

    DoOtherthing(); 

}

//  第二個 

if (condition) 

for (i=0; i<N; i++) 

    DoSomething(); 

else 

    for (i=0; i<N; i++) 

    DoOtherthing(); 

}

四、看程式寫結果題
請問執行以下Test函式會有什麼樣的結果?

//第一個

void GetMemory(char *p) 

p = (char *)malloc(100); 

void Test(void)   

char *str = NULL; 

GetMemory(str);  

strcpy(str, "hello world"); 

printf(str); 

//第二個

char *GetMemory(void) 

{  

char p[] = "hello world"; 

return p; 

void Test(void) 

char *str = NULL; 

str = GetMemory();   

printf(str); 

}

//第三個

Void GetMemory2(char **p, int num) 

*p = (char *)malloc(num); 

}  

void Test(void) 

char *str = NULL; 

GetMemory(&str, 100); 

strcpy(str, "hello");   

printf(str);  

}  

//第四個

void Test(void) 

char *str = (char *) malloc(100);

 strcpy(str, “ hello ” ); 

  free(str);       

  if(str != NULL) 

 { 

    strcpy(str, “ world ” );  

printf(str); 

五、C函式編寫題
已知strcpy 函式的原型是 

  char *strcpy(char *strDest, const char *strSrc); 

其中strDest是目的字串,strSrc 是源字串。 

 

(1 )不呼叫C++/C 的字串庫函式,請編寫函式 strcpy 

 

(2 )strcpy 能把strSrc 的內容複製到strDest,為什麼還要char *  型別的返回值? 

六、類函式編寫題
已知類String的原型為: 

 class String 

 { 

    public: 

    String(const char *str = NULL);  //  普通建構函式 

    String(const String &other);      //  拷貝建構函式 

  ~ String(void);            //  解構函式 

    String & operate =(const String &other);  //  賦值函式 

    private: 

   char  *m_data;     // 用於儲存字串 

 }; 

  請編寫String的上述4 個函式。
--------------------- 
作者:寒小陽 
來源:CSDN 
原文:https://blog.csdn.net/han_xiaoyang/article/details/10894159?utm_source=copy 
版權宣告:本文為博主原創文章,轉載請附上博文連結!