C/C++ 筆試、面試題目大匯總(三)

來源:互聯網
上載者:User

21. New delete 與malloc free 的聯絡與區別?
答案:都是在堆(heap)上進行動態記憶體操作。用malloc函數需要指定記憶體配置的位元組數並且不能初始化對象,new 會自動調用對象的建構函式。delete 會調用對象的destructor,而free 不會調用對象的destructor.

22. #define DOUBLE(x) x+x ,i = 5*DOUBLE(5); i 是多少?
答案:i 為30。

23. 有哪幾種情況只能用intialization list 而不能用assignment?

答案:當類中含有const、reference 成員變數;基類的建構函式都需要初始化表。

24. C++是不是型別安全的?
答案:不是。兩個不同類型的指標之間可以強制轉換(用reinterpret cast)。C#是型別安全的。

25. main 函數執行以前,還會執行什麼代碼?
答案:全域對象的建構函式會在main 函數之前執行。

26. 描述記憶體配置方式以及它們的區別?
1) 從靜態儲存地區分配。記憶體在程式編譯的時候就已經分配好,這塊記憶體在程式的整個運行期間都存在。例如全域變數,static 變數
2) 在棧上建立。在執行函數時,函數內局部變數的儲存單元都可以在棧上建立,函數執行結束時這些儲存單元自動被釋放。棧記憶體配置運算內建於處理器的指令集。
3) 從堆上分配亦稱動態記憶體分配。程式在啟動並執行時候用malloc 或new 申請任意多少的記憶體,程式員自己負責在何時用free 或delete 釋放記憶體。動態記憶體的生存期由程式員決定,使用非常靈活,但問題也最多。

27.struct 和 class 的區別

答案:struct 的成員預設是公有的,而類的成員預設是私人的。struct 和 class 在其他方面是功能相當的。

從感情上講,大多數的開發人員感到類和結構有很大的差別。感覺上結構僅僅象一堆缺乏封裝和功能的開放的記憶體位,而類就象活的並且可靠的社會成員,它有智慧型服務,有牢固的封裝屏障和一個良好定義的介面。既然大多數人都這麼認為,那麼只有在你的類有很少的方法並且有公有資料(這種事情在良好設計的系統中是存在的!)時,你也許應該使用 struct 關鍵字,否則,你應該使用 class 關鍵字。 

28.當一個類A 中沒有生命任何成員變數與成員函數,這時sizeof(A)的值是多少,如果不是零,請解釋一下編譯器為什麼沒有讓它為零。(Autodesk)
答案:肯定不是零。舉個反例,如果是零的話,聲明一個class A[10]對象數組,而每一個對象佔用的空間是零,這時就沒辦法區分A[0],A[1]…了。

29. 在8086 彙編下,邏輯地址和物理地址是怎樣轉換的?(Intel)
答案:通用寄存器給出的地址,是段內位移地址,相應段寄存器地址*10H+通用寄存器內地址,就得到了真正要訪問的地址。

30. 比較C++中的4種類型轉換方式?

請參考:http://blog.bioon.com/user1/8688/archives/2006/45399.shtml,重點是static_cast, dynamic_cast和reinterpret_cast的區別和應用。

31.分別寫出BOOL,int,float,指標類型的變數a 與“零”的比較語句。
答案:
BOOL :    if ( !a ) or if(a)
int :     if ( a == 0)
float :   const EXPRESSION EXP = 0.000001
          if ( a < EXP && a >-EXP)
pointer : if ( a != NULL) or if(a == NULL)

 

32.請說出const與#define 相比,有何優點?
答案:1) const 常量有資料類型,而宏常量沒有資料類型。編譯器可以對前者進行類型安全檢查。而對後者只進行字元替換,沒有型別安全檢查,並且在字元替換可能會產生意料不到的錯誤。
      2) 有些整合化的調試工具可以對const 常量進行調試,但是不能對宏常量進行調試。

33.簡述數組與指標的區別?
數組要麼在靜態儲存區被建立(如全域數組),要麼在棧上被建立。指標可以隨時指向任意類型的記憶體塊。
(1)修改內容上的差別
char a[] = “hello”;
a[0] = ‘X’;
char *p = “world”; // 注意p 指向常量字串
p[0] = ‘X’; // 編譯器不能發現該錯誤,執行階段錯誤
(2) 用運算子sizeof 可以計算出數組的容量(位元組數)。sizeof(p),p 為指標得到的是一個指標變數的位元組數,而不是p 所指的記憶體容量。C++/C 語言沒有辦法知道指標所指的記憶體容量,除非在申請記憶體時記住它。注意當數組作為函數的參數進行傳遞時,該數組自動退化為同類型的指標。
char a[] = "hello world";
char *p = a;
cout<< sizeof(a) << endl; // 12 位元組
cout<< sizeof(p) << endl; // 4 位元組
計算數組和指標的記憶體容量
void Func(char a[100])
{
cout<< sizeof(a) << endl; // 4 位元組而不是100 位元組
}

34.類成員函數的重載、覆蓋和隱藏區別?
答案:
a.成員函數被重載的特徵:
(1)相同的範圍(在同一個類中);
(2)函數名字相同;
(3)參數不同;
(4)virtual 關鍵字可有可無。
b.覆蓋是指衍生類別函數覆蓋基類函數,特徵是:
(1)不同的範圍(分別位於衍生類別與基類);
(2)函數名字相同;
(3)參數相同;
(4)基類函數必須有virtual 關鍵字。
c.“隱藏”是指衍生類別的函數屏蔽了與其同名的基類函數,規則如下:
(1)如果衍生類別的函數與基類的函數同名,但是參數不同。此時,不論有無virtual關鍵字,基類的函數將被隱藏(注意別與重載混淆)。
(2)如果衍生類別的函數與基類的函數同名,並且參數也相同,但是基類函數沒有virtual 關鍵字。此時,基類的函數被隱藏(注意別與覆蓋混淆)

35. There are two int variables: a and b, don’t use “if”, “? :”, “switch”or other judgement statements, find out the biggest one of the two numbers.
答案:( ( a + b ) + abs( a - b ) ) / 2

36. 如何列印出當前源檔案的檔案名稱以及源檔案的當前行號?
答案:
cout << __FILE__ ;
cout<<__LINE__ ;
__FILE__和__LINE__是系統預定義宏,這種宏並不是在某個檔案中定義的,而是由編譯器定義的。

37. main 主函數執行完畢後,是否可能會再執行一段代碼,給出說明?
答案:可以,可以用_onexit 註冊一個函數,它會在main 之後執行int fn1(void), fn2(void), fn3(void), fn4 (void);
void main( void )
{
String str("zhanglin");
_onexit( fn1 );
_onexit( fn2 );
_onexit( fn3 );
_onexit( fn4 );
printf( "This is executed first./n" );
}
int fn1()
{
printf( "next./n" );
return 0;
}
int fn2()
{
printf( "executed " );
return 0;
}
int fn3()
{
printf( "is " );
return 0;
}
int fn4()
{
printf( "This " );
return 0;
}
The _onexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to _onexit create a register of functions that are executed in LIFO (last-in-first-out) order. The functions passed to _onexit cannot take parameters.

38. 如何判斷一段程式是由C 編譯器還是由C++編譯器編譯的?
答案:
#ifdef __cplusplus
cout<<"c++";
#else
cout<<"c";
#endif

39.檔案中有一組整數,要求排序後輸出到另一個檔案中
答案:

#i nclude<iostream>

#i nclude<fstream>

using namespace std;

void Order(vector<int>& data) //bubble sort
{
int count = data.size() ;
int tag = false ; // 設定是否需要繼續冒泡的標誌位
for ( int i = 0 ; i < count ; i++)
{
for ( int j = 0 ; j < count - i - 1 ; j++)
{
if ( data[j] > data[j+1])
{
tag = true ;
int temp = data[j] ;
data[j] = data[j+1] ;
data[j+1] = temp ;
}
}
if ( !tag )
break ;
}
}

void main( void )
{
vector<int>data;
ifstream in("c://data.txt");
if ( !in)
{
cout<<"file error!";
exit(1);
}
int temp;
while (!in.eof())
{
in>>temp;
data.push_back(temp);
}
in.close(); //關閉輸入檔案流
Order(data);
ofstream out("c://result.txt");
if ( !out)
{
cout<<"file error!";
exit(1);
}
for ( i = 0 ; i < data.size() ; i++)
out<<data[i]<<" ";
out.close(); //關閉輸出檔案流
}

 

40. 鏈表題:一個鏈表的結點結構
struct Node
{
int data ;
Node *next ;
};
typedef struct Node Node ;

(1)已知鏈表的頭結點head,寫一個函數把這個鏈表逆序 ( Intel)

Node * ReverseList(Node *head) //鏈表逆序
{
if ( head == NULL || head->next == NULL )
return head;
Node *p1 = head ;
Node *p2 = p1->next ;
Node *p3 = p2->next ;
p1->next = NULL ;
while ( p3 != NULL )
{
p2->next = p1 ;
p1 = p2 ;
p2 = p3 ;
p3 = p3->next ;
}
p2->next = p1 ;
head = p2 ;
return head ;
}
(2)已知兩個鏈表head1 和head2 各自有序,請把它們合并成一個鏈表依然有序。(保留所有結點,即便大小相同)
Node * Merge(Node *head1 , Node *head2)
{
if ( head1 == NULL)
return head2 ;
if ( head2 == NULL)
return head1 ;
Node *head = NULL ;
Node *p1 = NULL;
Node *p2 = NULL;
if ( head1->data < head2->data )
{
head = head1 ;
p1 = head1->next;
p2 = head2 ;
}
else
{
head = head2 ;
p2 = head2->next ;
p1 = head1 ;
}
Node *pcurrent = head ;
while ( p1 != NULL && p2 != NULL)
{
if ( p1->data <= p2->data )
{
pcurrent->next = p1 ;
pcurrent = p1 ;
p1 = p1->next ;
}
else
{
pcurrent->next = p2 ;
pcurrent = p2 ;
p2 = p2->next ;
}
}
if ( p1 != NULL )
pcurrent->next = p1 ;
if ( p2 != NULL )
pcurrent->next = p2 ;
return head ;
}
(3)已知兩個鏈表head1 和head2 各自有序,請把它們合并成一個鏈表依然有序,這次要求用遞迴方法進行。 (Autodesk)
答案:
Node * MergeRecursive(Node *head1 , Node *head2)
{
if ( head1 == NULL )
return head2 ;
if ( head2 == NULL)
return head1 ;
Node *head = NULL ;
if ( head1->data < head2->data )
{
head = head1 ;
head->next = MergeRecursive(head1->next,head2);
}
else
{
head = head2 ;
head->next = MergeRecursive(head1,head2->next);
}
return head ;
}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.