- 相關推薦
中興通訊C++/C筆試題
C語言是在70年代初問世的。一九七八年由美國電話電報公司(AT&T)貝爾實驗室正式發表了C語言。現在很多公司把C++/C列入招聘考核的范圍。本文是關于中興通訊C++/C筆試題,歡迎閱讀學習。
一、請填寫BOOL , float, 指針變量 與“零值”比較的 if 語句。(10分)
提示:這里“零值”可以是0, 0.0 , FALSE或者“空指針”。例如 int 變量 n 與“零值”比較的 if 語句為:
if ( n == 0 )
if ( n != 0 )
以此類推。
請寫出 BOOL flag 與“零值”比較的 if 語句:
if(flag)
if(!flag)
請寫出 float x 與“零值”比較的 if 語句:
if(x>=-0.00001)
if(x<=0.00001)
請寫出 char *p 與“零值”比較的 if 語句:
if(p=null)
if(p!=null)
二、以下為Windows NT下的32位C++程序,請計算sizeof的值(10分)
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
三、簡答題(25分)
1、頭文件中的 ifndef/define/endif 干什么用? 防止重復定義
2、#include 和 #include “filename.h” 有什么區別? 查找范圍不同,后者先查找工作路徑,再查找VC標準路徑;前者只查工作路徑。
3、const 有什么用途?(請至少說明兩種) 定義變量常量,函數常量
4、在C++ 程序中調用被 C編譯器編譯后的函數,為什么要加 extern “C”聲明?二者的編譯器不同
5、請簡述以下兩個for循環的優缺點
// 第一個
for (i=0; i
{
if (condition)
DoSomething();
else
DoOtherthing();
}
// 第二個
if (condition)
{
for (i=0; i
DoSomething();
}
else
{
for (i=0; i
DoOtherthing();
}
先循環再判斷,先判斷再循環
第一個優點:每個循環都先判斷,再執行
第一個缺點:
第二個優點:條件判斷,只發生其中一個行為
第二個缺點:
優點:
缺點:
四、有關內存的思考題(20分)
void GetMemory(char *p)
{
p = (char *)malloc(100);
}
void Test(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
請問運行Test函數會有什么樣的結果?
答:程序崩潰了,
char *GetMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);
}
請問運行Test函數會有什么樣的結果?
答:亂碼,
Void GetMemory2(char **p, int num)
{
*p = (char *)malloc(num);
}
void Test(void)
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
}
請問運行Test函數會有什么樣的結果?
答: 可以運行
void Test(void)
{
char *str = (char *) malloc(100);
strcpy(str, “hello”);
free(str);
if(str != NULL)
{
strcpy(str, “world”);
printf(str);
}
}
請問運行Test函數會有什么樣的結果?
答:word
五、編寫strcpy函數(10分)
已知strcpy函數的原型是
char *strcpy(char *strDest, const char *strSrc);
其中strDest是目的字符串,strSrc是源字符串。
(1)不調用C++/C的字符串庫函數,請編寫函數 strcpy
(2)strcpy能把strSrc的內容復制到strDest,為什么還要char * 類型的返回值?
六、編寫類String的構造函數、析構函數和賦值函數(25分)
已知類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個函數。
【中興通訊C++/C筆試題】相關文章:
c++個人實習心得體會范文01-11
中興辭職報告09-26
c語言心得04-24
c語言編程心得06-19
學習c#心得01-17
C語言實習總結07-10
c語言實踐報告01-17
心理c證培訓心得12-15
心理c證的培訓心得03-22
c語言工作總結03-31