1. 程式人生 > >找工作筆試面試那些事兒(7)---測試題答案

找工作筆試面試那些事兒(7)---測試題答案

一、請填寫BOOL  , float,  指標變數  與“零值”比較的 if  語句。 

提示:這裡“零值”可以是0 ,  0.0 , FALSE 或者“空指標”。例如 int  變數 n  與“零值”

比較的 if  語句為: 

        if ( n  ==  0 )  

        if ( n != 0 ) 

以此類推。 

 

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

 if (  flag  ) 

 if (  !flag  ) 

 

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

const float EPSINON = 0.00001; 

if ((x >= - EPSINON) && (x <= EPSINON)

不可將浮點變數用“==”或“!= ”與數字比較,應該設法轉化成“>=”或“ <=”此類形式

 

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

if   (p == NULL)  

if   (p != NULL)  

二、以下為Windows XP 下的32位C++ 程式,請計算size of 的值

char str[]  = “Hello” ; 
char  *p  =  str ; 
int  n  =  10; 


請計算 

sizeof (str ) =   6       

          

sizeof (p)   =  4        

           

sizeof (n) =  4

void Func ( char str[100]) 

}

請計算 

sizeof( str )  =   4

void *p = malloc( 100 ); 

請計算 

sizeof ( p ) =  4

三、簡答

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

 

 答:防止該標頭檔案被重複引用。

 

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

 

 答:對於#include  <filename.h> ,編譯器從標準庫路徑開始搜尋 filename.h 

     對於#include  “filename.h”  ,編譯器從使用者的工作路徑開始搜尋 filename.h 

 

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

 

 答:(1 )可以定義 const 常量 

(2 )const 可以修飾函式的引數、返回值,甚至函式的定義體。被const 修飾的東西都受到強制保護,可以預防意外的變動,能提高程式的健壯性。

 

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

答:C++ 語言支援函式過載,C 語言不支援函式過載。函式被C++ 編譯後在庫中的名字與C 語言的不同。假設某個函式的原型為:  void foo(int x, int y); 

該函式被 C 編譯器編譯後在庫中的名字為_foo,而C++ 編譯器則會產生像_foo_int_int 之類的名字。 

C++ 提供了C 連線交換指定符號extern “C ”來解決名字匹配問題。

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

//  第一個 

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

if (condition) 

    DoSomething(); 

else 

    DoOtherthing(); 

}

優點:程式簡潔 

缺點:多執行了N-1 次邏輯判斷,並且打斷了迴圈“流水線”作業,使得編譯器不能對迴圈進行優化處理,降低了效率。

//  第二個 

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); 

答:程式崩潰。 

因為GetMemory並不能傳遞動態記憶體,Test函式中的 str一直都是 NULL 。 strcpy(str, "hello wo rld");將使程式崩潰。

//第二個

char *GetMemory(void) 

{  

char p[] = "hello world"; 

return p; 

void Test(void) 

char *str = NULL; 

str = GetMemory();   

printf(str); 

}

答:可能是亂碼。 

因為GetMemory 返回的是指向“棧記憶體”的指標,該指標的地址不是 NULL ,但其原現的內容已經被清除,新內容不可知。

//第三個

Void GetMemory2(char **p, int num) 

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

}  

void Test(void) 

char *str = NULL; 

GetMemory(&str, 100); 

strcpy(str, "hello");   

printf(str);  

}  

答: 

(1 )能夠輸出hello  

(2 )記憶體洩漏

//第四個

void Test(void) 

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

 strcpy(str, “ hello ” ); 

  free(str);       

  if(str != NULL) 

 { 

    strcpy(str, “ world ” );  

printf(str); 

答:篡改動態記憶體區的內容,後果難以預料,非常危險。 

因為free(str);之後,str 成為野指標, if(str != NULL)語句不起作用。

五、C函式編寫題

已知strcpy 函式的原型是 

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

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

 

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

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

    assert((strDest!=NULL) && ( strSrc  !=NULL));  
    char *address =  strDest;       // 2 分 
    while( (* strDest++ = * strSr c ++) !=  ‘\ 0’)     
       NULL ;  
       return  address ;        
}


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

答:為了實現鏈式表示式。

例如  int length = strlen( strcpy( strDest, “hello world”) ); 

六、類函式編寫題

已知類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 個函式。

// String 的解構函式 
 String : : ~String(void)               

  delete [] m_data;                        
//  由於m_data是內部資料型別,也可以寫成 delete m_data; 
 }
// String 的普通建構函式               
 String : : String(const char *str)      // 6 分 

  if(str==NULL)                           
 { 
    m_data = new char[1];    // 若能加 NULL 判斷則更好 
  *m_data = ‘ \ 0 ’ ;                       
  }                                          
 else 
 { 
    int length = strlen(str);            
    m_data = new char[length+1];  //  若能加 NULL 判斷則更好       
    strcpy(m_data, str);                 
 } 
}   
//  拷貝建構函式 
  String::String(const String &other)   // 3 分 
 {  
  int length = strlen(other.m_data);    
  m_data = new char[length+1];      //  若能加 NULL 判斷則更好     
  strcpy(m_data, other.m_data);          

//  賦值函式 
  String & String::operate =(const String &other)    // 13分 
 {  
  // (1) 檢查自賦值                     // 4 分 
  if(this ==  &other) 
   return *this; 
  
// (2)  釋放原有的記憶體資源            // 3 分 
  delete []  m_data; 
   
  // (3 )分配新的記憶體資源,並複製內容 // 3 分 
  int length = strlen(other.m_data);   
  m_data = new char[length+1];         // 若能加 NULL 判斷則更好 
  strcpy(m_data, other.m_data); 
   
  // (4 )返回本物件的引用            // 3 分 
  return *this; 
}  


--------------------- 
作者:寒小陽 
來源:CSDN 
原文:https://blog.csdn.net/han_xiaoyang/article/details/10896159?utm_source=copy 
版權宣告:本文為博主原創文章,轉載請附上博文連結!