C語言測試題第二部分

 
9,The output for this program is: (a) i=4 j=2 (b) i=3 j=2 (c) i=3 j=4 (d) i=3 j=6
int main() {
   
int i=3;
   
int j;
    j
=sizeof(++i+++i);
printf("i=%d j=%d", i ,j);
   
return0;
}
分析:因爲sizeof是運算符,不是函數。我們很多時候都將sizeof當成了函數,這是不對的。sizeof只對數據類型進行判定而不是去計算表達式。所以i的值不會改變。關於sizeof我們可以看看MSDN上如何對sizeof進行定義的:
sizeof Operatorsizeof expression The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t. The expression is either an identifier or a type-cast expression (a type specifier enclosed in parentheses). When applied to a structure type or variable, sizeof returns the actual size, which may include padding bytes inserted for alignment. When applied to a statically dimensioned array, sizeof returns the size of the entire array. The sizeof operator cannot return the size of dynamically allocated arrays or external arrays.
答案 :(c) i=3 j=4
10,The output for this program is: (a) 5 5 5 5 (b) 3 5 3 5 (c) 5 3 5 3 (d) 3 3 3 3
void f1(int*, int);
void f2(int*, int);
void(*p[2]) ( int*, int);
int main() {
   
int a;
   
int b;
    p[
0] = f1;
    p[
1] = f2;
    a
=3;
    b
=5;
    p[
0](&a , b);
    printf(
"%d\t %d\t" , a ,b);
    p[
1](&a , b);
    printf(
"%d\t %d\t" , a ,b);
   
return0;
}
void f1( int* p , int q) {
   
int tmp;
    tmp
=*p;
   
*p = q;
    q
= tmp;
}
void f2( int* p , int q) {
   
int tmp;
    tmp
=*p;
   
*p = q;
    q
= tmp;
}
分析:考察函數參數的傳遞方式,值傳遞和地址傳遞。值傳遞在函數調用後不改變實參,而地址傳遞在函數調用後改變實參。只要知道這一點,本題就很好解答。f1和f2中的兩個參數第一個都是地址傳遞,第二個都是值傳遞。
答案: (a) 5 5 5 5
11,The output for this program is: (a) 0 1 2 0 (b) 0 1 2 1 (c) 1 2 0 1 (d) 0 2 1 1
void e(int );
int main() {
   
int a;
    a
=3;
    e(a);
   
return0;
}

void e(int n) {
   
if(n>0) {
        e(
--n);
        printf(
"%d" , n);
        e(
--n);
    }
}
分析:考察遞歸函數的調用,調用深度,調用後一級一級的返回。
e(3)  e(2)   e(1)
e(3):                                                  e(2):                                               e(1):
   e(2)                                                         e(1)                                                e(0)
   printf("%d" , n);  //2                                printf("%d" , n);  //1                     printf("%d" , n);  //0 
 e(1)                                                            e(0)                                                e(-1)
答案:
(a) 0 1 2 0
12,type of tmp is 
(a) Pointer to function of having two arguments that is pointer to float 
(b) int 
(c) Pointer to function having two argument that is pointer to float and return int 
(d) None of the above
typedef int (*test) ( float* , float*)
test tmp;
分析:考察函數指針和typedef。函數指針的形式是:數據類型標誌符 (指針變量名) (形參列表); 例如:int (*f) (int x); /* 聲明一個函數指針 */
typedef爲C語言的關鍵字,作用是爲一種數據類型定義一個新名字。在編程中使用typedef目的一般有兩個,一個是給變量一個易記且意義明確的新名字,另一個是簡化一些比較複雜的類型聲明。 typedef int (*test) ( float* , float*)  中test是一個函數指針,經過typedef定以後成爲一個函數指針的數據類型。所以test tmp中temp是一個指向函數的指針,這個函數返回整型值
答案:(c) Pointer to function having two argument that is pointer to float and return int 
13,The output for this program is: (a) 5 (b) 6 (c) 9 (d) None of the above
intmain() {
   
char*p;
   
charbuf[10]={1,2,3,4,5,6,9,8};
    p
=&((buf+1)[5]);
printf("%d" , *p);
   
return0;
}
分析:考察數組地址的變化。buf是數組的起始地址,也即第一個元素的地址&buf[0],buf+1就是數組的第二個元素的地址&buf[1],現在要以buf+1作爲起始地址,(buf+1)[5] 相當於 buf+1+5 即buf[6]
答案:(c) 9
14,The output for this program is: (a) ab (b) cd (c) ef (d) gh
Void f(char**);
int main() {
   
char* argv[] = { "ab" ,"cd" , "ef" ,"gh", "ij" ,"kl" };
    f( argv );
    
   
return0;
}
void f( char**p ) {
   
char* t;
    t
= (p+=sizeof(int))[-1];
    printf(
"%s" , t);    
}
分析:數組中存放可以分高地址和低地址兩種順序存放。在調用函數f中 p+=sizeof(int) 相當於 p+=4 此時p指向第五個元素,再取p[-1]就相當於p-=1;此時指向了第四個元素。返回給t即 t =“gh”
答案:(d) gh
15,The output for this program is: (a) 7 (b) 6 (c) 5 (d) 3
#include<stdarg.h>
int ripple ( int , ...);
int main(){
   
int num;
num = ripple ( 3, 5,7); printf( " %d" , num);
   
return0;
}
int ripple (int n, ...) {
   
int i , j;
   
int k;
    va_list p; //參數列表
    k
=0;
    j
=1;
    va_start( p , n);  //獲取第一個參數
   
for (; j<n; ++j) {
        i
= va_arg( p , int);  //獲取以一個參數
       
for (; i; i &=i-1 )
           
++k;
    }
   
return k;
}
分析:考察不定參數函數的知識點。
答案:(c) 5
16, The value of j at the end of the execution of the this program is:
 (a) 10 (b) 15 (c) 6 (d) 7
int counter (int i) {
staticint count =0;
    count
= count +i;
   
return (count );
}
int main() {
   
int i , j;
   
for (i=0; i <=5; i++)
    j
= counter(i);
   
return0;
}
分析:主要考察靜態變量static,靜態變量的生存期是在整個程序結束的時候才結束。內存中的位置:靜態存儲區(靜態存儲區在整個程序運行期間都存在)。
答案:(b) 15

發佈了39 篇原創文章 · 獲贊 9 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章