天天看點

2013-7-22學習C面試題

1.   程式設計:計算班級學生平均成績和不及格人數。

#include <stdio.h>

int main(int argc, constchar * argv[])

{

    int stu[10]={99,98,55,96,95,94,93,92,91,90};

    int sum=0;

    int count=0;

    float avr=0;

    for (int i=0; i<10; i++) {

        sum+=stu[i];

        if (60>stu[i]) {

            count++;

        }

    }

    avr = (float)sum / 10;

    printf("學生平均成績是:%f,不及格人數是:%d",avr,count);

    return0;

}

2.   程式設計,用條件表達式實作三個整數從大到小排序。

    int a=10,b=20,c=30;

    int max,mid,min;

    max=a>b?(a>c?a:c):(b>c?b:c);

    mid=a>b?(a<c?a:c>b?c:b):(b<c?b:c);

    min=a<b?(a<c?a:c):(b<c?b:c);

    printf("max=%d,mid=%d,min=%d",max,mid,min);

3.   請說明三種循環語句的異同點。

     三種循環分别是while;do while 和 for

     相同點都是實作了一段邏輯循環操作,都具有以下三條内容:

     (1)循環體的設計

     (2)循環條件的設計

     (3)循環入口的初始化工作

     不同點:while是先進行判斷,然後執行循環體;do while是先進行循環體然後再進行判斷;for也是先進行判斷然後進入循環體,結束後,進行++操作

     for循環一般用在循環次數固定的情況下

4.   請舉例說明 break、continue 語句的差別。

      break//終止目前循環       contine//跳過此次循環

     #include <stdio.h>

    for (int i=0; i<4; i++) {

        for (int j=0; j<4; j++) {

            if (i==1) {

                continue;

            }

            if (j==2) {

                break;

            printf("%d   %d\n",i,j);

結果:

0   0

0   1

2   0

2   1

3   0

3   1

5.   用三種循環語句編寫程式實作:計算 100 以内的整數之和 。

for循環:

    for (int i=0; i<=100; i++) {

        sum+=i;

    printf("100以内的整數的和:%d",sum);

while循環:

    int i=0;

    while (i<=100) {

        i++;

do while循環:

    do {

    }while (i<=100);

一、請填寫BOOL , float, 指針變量 與“零值”比較的 if 語句。(10分)

提示:這裡“零值”可以是0, 0.0 , FALSE或者“空指針”。例如 int 變量 n 與“零值”比較的 if 語句為:

    if ( n == 0 )

    if ( n != 0 )

以此類推。

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

 if(FALSE == flag)[錯誤]

 if(flag)或者if(!flag)

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

 const double EPSILON = 1.00e-07;

if (x<abs(EPSILON))

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

 if(NULL == p)或者if(NULL != p)

二、以下為Windows NT下的32位C++程式,請計算sizeof的值(10分)

       char  str[] = “Hello” ;

       char   *p = str ;

int     n = 10;

請計算

sizeof (str ) = 5[錯]     6[對]          char str[8] = "Hello"   sizeof(str) =8

sizeof ( p ) =  8    

sizeof ( n ) = 4

void Func ( char str[100])

 sizeof( str ) = 8  //相當于指針

void *p = malloc( 100 );

sizeof ( p ) =8  

三、簡答題(25分)

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

 防止引用該頭檔案的時候重複定義

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

 第一種是指從系統檔案中去找filename.h檔案;第二種是從目前檔案中去找filename.h的檔案

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

 1.const修飾常量,該變量不可修改,例如const double PI=3.14

 2.常指針,const放在不同的地方有不同的效果,例如:

const int *p 和 int const *p 效果是一樣的,指針所指向的内容是不可以變的,指針指向可以變

const int *p 和 int const *p指針指向不可變,但指針指向的内容可以變

int const * const p  指針指向和内容都不可以變

4、在C++ 程式中調用被 C編譯器編譯後的函數,為什麼要加 extern “C”聲明?

 首先,作為extern是C/C++語言中表明函數和全局變量作用範圍(可見性)的關鍵字,該關鍵字告訴編譯器,其聲明的函數和變量可以在本子產品或其它子產品中使用。

被extern "C"修飾的變量和函數是按照C語言方式編譯和連接配接的

5、請簡述以下兩個for循環的優缺點

// 第一個

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

if (condition)

    DoSomething();

else

    DoOtherthing();

// 第二個

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

優點:

 如果N比較小,第一種方法代碼量少

缺點:

 耗時間

 節省時間

 受客觀條件的制約,有可能condition在DoSomething()中被修改

四、有關記憶體的思考題(20分)

void GetMemory(char *p)

p = (char *)malloc(100);

void Test(void)

char *str = NULL;

GetMemory(str); 

strcpy(str, "hello world");

printf(str);

請問運作Test函數會有什麼樣的結果?

答:程式有錯誤!

修改:

方法一:

void GetMemory(char **p)

     p=(char *)malloc(100);

     char *str = NULL;

     GetMemory(&str);

     strcpy(str,"hello world");

     printf(str);

方法二:

char * GetMemory(char *p)

     return p;

     str = GetMemory(str);

char *GetMemory(void)

char p[] = "hello world";

return p;

str = GetMemory();  

答:有錯誤

雖然位址傳過來了,但是“hello world”值沒有傳過來           為什麼? 位址和strlen大小都一樣

Void GetMemory2(char **p, int num)

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

GetMemory(&str, 100);

strcpy(str, "hello"); 

printf(str);  

答:

 能夠輸出hello

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

    strcpy(str, “hello”);

    free(str);    

    if(str != NULL)

    {

      strcpy(str, “world”);

 free了空間以後,str不知道指向哪兒了,可能為空,也可能不為空,是以這樣寫不穩定

五、編寫strcpy函數(10分)

已知strcpy函數的原型是

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

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

(1)不調用C++/C的字元串庫函數,請編寫函數 strcpy

char *strcpy(char *strDest,constchar *strSrc)

    if ((strDest == NULL)||(strSrc==NULL)) {

        returnNULL;

    char *strDestCopy = strDest;

    while ((*strDest++=*strSrc++)!='\0');

    return strDestCopy;

(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個函數。

#include <iostream>

usingnamespacestd;

#include <string.h>

class String1

public:

    String1(constchar *str = NULL) //普通構造函數

        if (str==NULL) {

            m_data = newchar[1];

            m_data = '\0';

        //delete []m_data;

        else

        {

            m_data = newchar[strlen(str)+1];

            strcpy(m_data, str);

    String1(constString1 &other) //拷貝構造函數

/*

        if (&other == this) {

            cout<<"不能為複制本身";

        }*/不需要寫這個  因為不可能自己作為參數傳遞給自己,應為自己還沒建立好

        if (this->m_data == other.m_data) {

            cout<<"不要指派相等的值";

            if (m_data) {

                delete []m_data;

            m_data = newchar [strlen(other.m_data)+1];

            strcpy(m_data, other.m_data);

    ~String1(void)

        if (m_data) {

            delete []m_data;

    String1 &operate = (const String1 &other)

        if(this == &other)

            return *this;

        delete []m_data;

        m_data = newchar[strlen(other.m_data)+1];

        strcpy(m_data,other.m_data);

private:

    char * m_data;//用于儲存字元串

};

    String1 s="ding";

    String1 ss= s;

    cout<<ss;

棧和堆的差別:

棧是自動釋放,堆是手動釋放

局部變量都是存放在棧上面的,new,malloc等建立出來的變量是存在堆上的

棧容量比較小,堆容量比較大

本文轉自蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366457,如需轉載請自行聯系原作者

繼續閱讀