c/c++面试试题(二)
21.?New delete 與malloc free 的聯(lián)系與區(qū)別?
答案:都是在堆(heap)上進(jìn)行動(dòng)態(tài)的內(nèi)存操作。用malloc函數(shù)需要指定內(nèi)存分配的字節(jié)數(shù)并且不能初始化對(duì)象,new 會(huì)自動(dòng)調(diào)用對(duì)象的構(gòu)造函數(shù)。delete 會(huì)調(diào)用對(duì)象的destructor,而free 不會(huì)調(diào)用對(duì)象的destructor.
22.?#define DOUBLE(x) x+x ,i = 5*DOUBLE(5); i 是多少?
答案:i 為30。
23. 有哪幾種情況只能用intialization list 而不能用assignment?
答案:當(dāng)類中含有const、reference 成員變量;基類的構(gòu)造函數(shù)都需要初始化表。
24.?C++是不是類型安全的?
答案:不是。兩個(gè)不同類型的指針之間可以強(qiáng)制轉(zhuǎn)換(用reinterpret cast)。C#是類型安全的。
25.?main 函數(shù)執(zhí)行以前,還會(huì)執(zhí)行什么代碼?
答案:全局對(duì)象的構(gòu)造函數(shù)會(huì)在main 函數(shù)之前執(zhí)行。
26.?描述內(nèi)存分配方式以及它們的區(qū)別?
1) 從靜態(tài)存儲(chǔ)區(qū)域分配。內(nèi)存在程序編譯的時(shí)候就已經(jīng)分配好,這塊內(nèi)存在程序的整個(gè)運(yùn)行期間都存在。例如全局變量,static 變量。
2) 在棧上創(chuàng)建。在執(zhí)行函數(shù)時(shí),函數(shù)內(nèi)局部變量的存儲(chǔ)單元都可以在棧上創(chuàng)建,函數(shù)執(zhí)行結(jié)束時(shí)這些存儲(chǔ)單元自動(dòng)被釋放。棧內(nèi)存分配運(yùn)算內(nèi)置于處理器的指令集。
3) 從堆上分配,亦稱動(dòng)態(tài)內(nèi)存分配。程序在運(yùn)行的時(shí)候用malloc 或new 申請任意多少的內(nèi)存,程序員自己負(fù)責(zé)在何時(shí)用free 或delete 釋放內(nèi)存。動(dòng)態(tài)內(nèi)存的生存期由程序員決定,使用非常靈活,但問題也最多。
27.struct 和 class 的區(qū)別
答案:struct 的成員默認(rèn)是公有的,而類的成員默認(rèn)是私有的。struct 和 class 在其他方面是功能相當(dāng)?shù)摹?
從感情上講,大多數(shù)的開發(fā)者感到類和結(jié)構(gòu)有很大的差別。感覺上結(jié)構(gòu)僅僅象一堆缺乏封裝和功能的開放的內(nèi)存位,而類就象活的并且可靠的社會(huì)成員,它有智能服務(wù),有牢固的封裝屏障和一個(gè)良好定義的接口。既然大多數(shù)人都這么認(rèn)為,那么只有在你的類有很少的方法并且有公有數(shù)據(jù)(這種事情在良好設(shè)計(jì)的系統(tǒng)中是存在的!)時(shí),你也許應(yīng)該使用 struct 關(guān)鍵字,否則,你應(yīng)該使用 class 關(guān)鍵字。?
28.當(dāng)一個(gè)類A 中沒有生命任何成員變量與成員函數(shù),這時(shí)sizeof(A)的值是多少,如果不是零,請解釋一下編譯器為什么沒有讓它為零。(Autodesk)
答案:肯定不是零。舉個(gè)反例,如果是零的話,聲明一個(gè)class A[10]對(duì)象數(shù)組,而每一個(gè)對(duì)象占用的空間是零,這時(shí)就沒辦法區(qū)分A[0],A[1]…了。
29. 在8086 匯編下,邏輯地址和物理地址是怎樣轉(zhuǎn)換的?(Intel)
答案:通用寄存器給出的地址,是段內(nèi)偏移地址,相應(yīng)段寄存器地址*10H+通用寄存器內(nèi)地址,就得到了真正要訪問的地址。
30.?比較C++中的4種類型轉(zhuǎn)換方式?
請參考:http://blog.csdn.net/wfwd/archive/2006/05/30/763785.aspx,重點(diǎn)是static_cast, dynamic_cast和reinterpret_cast的區(qū)別和應(yīng)用。
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 相比,有何優(yōu)點(diǎn)?
答案:1) const 常量有數(shù)據(jù)類型,而宏常量沒有數(shù)據(jù)類型。編譯器可以對(duì)前者進(jìn)行類型安全檢查。而對(duì)后者只進(jìn)行字符替換,沒有類型安全檢查,并且在字符替換可能會(huì)產(chǎn)生意料不到的錯(cuò)誤。
????? 2) 有些集成化的調(diào)試工具可以對(duì)const 常量進(jìn)行調(diào)試,但是不能對(duì)宏常量進(jìn)行調(diào)試。
33.簡述數(shù)組與指針的區(qū)別?
數(shù)組要么在靜態(tài)存儲(chǔ)區(qū)被創(chuàng)建(如全局?jǐn)?shù)組),要么在棧上被創(chuàng)建。指針可以隨時(shí)指向任意類型的內(nèi)存塊。
(1)修改內(nèi)容上的差別
char a[] = “hello”;
a[0] = ‘X’;
char *p = “world”; // 注意p 指向常量字符串
p[0] = ‘X’; // 編譯器不能發(fā)現(xiàn)該錯(cuò)誤,運(yùn)行時(shí)錯(cuò)誤
(2) 用運(yùn)算符sizeof 可以計(jì)算出數(shù)組的容量(字節(jié)數(shù))。sizeof(p),p 為指針得到的是一個(gè)指針變量的字節(jié)數(shù),而不是p 所指的內(nèi)存容量。C++/C 語言沒有辦法知道指針?biāo)傅膬?nèi)存容量,除非在申請內(nèi)存時(shí)記住它。注意當(dāng)數(shù)組作為函數(shù)的參數(shù)進(jìn)行傳遞時(shí),該數(shù)組自動(dòng)退化為同類型的指針。
char a[] = "hello world";
char *p = a;
cout<< sizeof(a) << endl; // 12 字節(jié)
cout<< sizeof(p) << endl; // 4 字節(jié)
計(jì)算數(shù)組和指針的內(nèi)存容量
void Func(char a[100])
{
cout<< sizeof(a) << endl; // 4 字節(jié)而不是100 字節(jié)
}
34.類成員函數(shù)的重載、覆蓋和隱藏區(qū)別?
答案:
a.成員函數(shù)被重載的特征:
(1)相同的范圍(在同一個(gè)類中);
(2)函數(shù)名字相同;
(3)參數(shù)不同;
(4)virtual 關(guān)鍵字可有可無。
b.覆蓋是指派生類函數(shù)覆蓋基類函數(shù),特征是:
(1)不同的范圍(分別位于派生類與基類);
(2)函數(shù)名字相同;
(3)參數(shù)相同;
(4)基類函數(shù)必須有virtual 關(guān)鍵字。
c.“隱藏”是指派生類的函數(shù)屏蔽了與其同名的基類函數(shù),規(guī)則如下:
(1)如果派生類的函數(shù)與基類的函數(shù)同名,但是參數(shù)不同。此時(shí),不論有無virtual關(guān)鍵字,基類的函數(shù)將被隱藏(注意別與重載混淆)。
(2)如果派生類的函數(shù)與基類的函數(shù)同名,并且參數(shù)也相同,但是基類函數(shù)沒有virtual 關(guān)鍵字。此時(shí),基類的函數(shù)被隱藏(注意別與覆蓋混淆)
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. 如何打印出當(dāng)前源文件的文件名以及源文件的當(dāng)前行號(hào)?
答案:
cout << __FILE__ ;
cout<<__LINE__ ;
__FILE__和__LINE__是系統(tǒng)預(yù)定義宏,這種宏并不是在某個(gè)文件中定義的,而是由編譯器定義的。
37. main 主函數(shù)執(zhí)行完畢后,是否可能會(huì)再執(zhí)行一段代碼,給出說明?
答案:可以,可以用_onexit 注冊一個(gè)函數(shù),它會(huì)在main 之后執(zhí)行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.文件中有一組整數(shù),要求排序后輸出到另一個(gè)文件中
答案:
#i nclude<iostream>
#i nclude<fstream>
using namespace std;
void Order(vector<int>& data) //bubble sort
{
int count = data.size() ;
int tag = false ; // 設(shè)置是否需要繼續(xù)冒泡的標(biāo)志位
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(); //關(guān)閉輸入文件流
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(); //關(guān)閉輸出文件流
}
?
40. 鏈表題:一個(gè)鏈表的結(jié)點(diǎn)結(jié)構(gòu)
struct Node
{
int data ;
Node *next ;
};
typedef struct Node Node ;
(1)已知鏈表的頭結(jié)點(diǎn)head,寫一個(gè)函數(shù)把這個(gè)鏈表逆序 ( 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)已知兩個(gè)鏈表head1 和head2 各自有序,請把它們合并成一個(gè)鏈表依然有序。(保留所有結(jié)點(diǎn),即便大小相同)
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)已知兩個(gè)鏈表head1 和head2 各自有序,請把它們合并成一個(gè)鏈表依然有序,這次要求用遞歸方法進(jìn)行。 (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 ;
}
41. 分析一下這段程序的輸出 (Autodesk)
class B
{
public:
B()
{
cout<<"default constructor"<<endl;
}
~B()
{
cout<<"destructed"<<endl;
}
B(int i):data(i)??? //B(int) works as a converter ( int -> instance of ?B)
{
cout<<"constructed by parameter " << data <<endl;
}
private:
int data;
};
B Play( B b)
{
return b ;
}
(1)??????????????????????????????????????????? results:
int main(int argc, char* argv[])??????constructed by parameter 5
{???????????????????????????????????? destructed? B(5)形參析構(gòu)
B t1 = Play(5); B t2 = Play(t1);?? destructed? t1形參析構(gòu)
return 0; destructed? t2 注意順序!
}?????????????????????????????????????destructed? t1
(2)???????????????????????????????????results:
int main(int argc, char* argv[])??????constructed by parameter 5
{???????????????????????????????????? destructed? B(5)形參析構(gòu)
B t1 = Play(5); B t2 = Play(10);?? constructed by parameter 10
return 0; destructed? B(10)形參析構(gòu)
}???????????????????????????????????? destructed? t2 注意順序!
????????????????????????????????????? destructed? t1
42. 寫一個(gè)函數(shù)找出一個(gè)整數(shù)數(shù)組中,第二大的數(shù) (microsoft)
答案:
const int MINNUMBER = -32767 ;
int find_sec_max( int data[] , int count)
{
int maxnumber = data[0] ;
int sec_max = MINNUMBER ;
for ( int i = 1 ; i < count ; i++)
{
if ( data[i] > maxnumber )
{
sec_max = maxnumber ;
maxnumber = data[i] ;
}
else
{
if ( data[i] > sec_max )
sec_max = data[i] ;
}
}
return sec_max ;
}
43. 寫一個(gè)在一個(gè)字符串(n)中尋找一個(gè)子串(m)第一個(gè)位置的函數(shù)。
KMP算法效率最好,時(shí)間復(fù)雜度是O(n+m)。
44. 多重繼承的內(nèi)存分配問題:
?? 比如有class A : public class B, public class C {}
?? 那么A的內(nèi)存結(jié)構(gòu)大致是怎么樣的?
這個(gè)是compiler-dependent的, 不同的實(shí)現(xiàn)其細(xì)節(jié)可能不同。
如果不考慮有虛函數(shù)、虛繼承的話就相當(dāng)簡單;否則的話,相當(dāng)復(fù)雜。
可以參考《深入探索C++對(duì)象模型》,或者:
http://blog.csdn.net/wfwd/archive/2006/05/30/763797.aspx
45. 如何判斷一個(gè)單鏈表是有環(huán)的?(注意不能用標(biāo)志位,最多只能用兩個(gè)額外指針)
?? struct node { char val; node* next;}
???bool check(const node* head) {} //return?false : 無環(huán);true: 有環(huán)
一種O(n)的辦法就是(搞兩個(gè)指針,一個(gè)每次遞增一步,一個(gè)每次遞增兩步,如果有環(huán)的話兩者必然重合,反之亦然):
bool check(const node* head)
{
??? if(head==NULL)? return false;
??? node *low=head, *fast=head->next;
??? while(fast!=NULL && fast->next!=NULL)
??? {
??????? low=low->next;
??????? fast=fast->next->next;
??????? if(low==fast) return true;
??? }
??? return false;
}
總結(jié)
以上是生活随笔為你收集整理的c/c++面试试题(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 8月19学习练习[两三个TableVie
- 下一篇: 网络协议TCP/IP、IPX/SPX、N