C語言測試題

來自CSDN:http://topic.csdn.net/u/20110729/12/9973e5a4-a414-4714-871f-905a85612297_3.html

1,The output for this program is: (a) 3 (b) 5 (c) 0

C/C++ code

#include<setjmp.h>

 

static jmp_buf buf;

 

int main() {

    volatile int b;

    b =3;

 

    if(setjmp(buf)!=0) {

        printf("%d ", b);

        exit(0);

    }

    b=5;

 

    longjmp(buf , 1);

 

    return 0;

}

 



2,The output for this program is: (a) 3 (b) 5 (c) 6 (d) 7

C/C++ code

struct node { int a; int b; int c; };

int main() {

    struct node s= { 3, 5,6 };

    struct node *pt = &s;

    printf("%d" , *(int*)pt);

 

    return 0;

}

 



3,What function of x and n is compute by this code segment?  
(a) x^n (b) x*n (c) n^x (d) None of the above

C/C++ code

int foo ( int x , int n) {

    int val;

    val =1;

    if (n>0) {

        if (n%2 == 1) val = val *x;

        val = val * foo(x*x , n/2);

    }

    return val;

}

 



4,The output for this program is: (a) 2 2 (b) 2 1 (c) 2 5 (d) None of the above

C/C++ code

int main() {

    int a[5] = {1,2,3,4,5};

    int *ptr = (int*)(&a+1);

    printf("%d %d" , *(a+1), *(ptr-1) );

    return 0;

}

 



5,The output for this program is: (a) 8 (b) 9 (c) 7 (d) None of the above

C/C++ code

void foo(int [][3] );

int main(){

    int a [3][3]= { { 1,2,3} , { 4,5,6},{7,8,9}};

    foo(a);

    printf("%d" , a[2][1]);

    return 0;

}

 

void foo( int b[][3]) {

    ++ b;

    b[1][1] =9;

}

 



6,The output for this program is: (a) c=3 d=3 (b) c=5 d=3 (c) c=3 d=5 (d) c=5 d=5

C/C++ code

int main() {

    int a, b,c, d;

    a=3;

    b=5;

    c=a,b;

    d=(a,b);

    printf("c=%d" ,c);

    printf("d=%d" ,d);

    return 0;

}

 



7,The output for this program is:(a) 2 3 5 6 (b) 2 3 4 5 (c) 4 5 0 0 (d) None of the above

C/C++ code

int main() {

    int a[][3] = { 1,2,3 ,4,5,6};

    int (*ptr)[3] =a;

    printf("%d %d " ,(*ptr)[1], (*ptr)[2] );

    ++ptr;

    printf("%d %d" ,(*ptr)[1], (*ptr)[2] );

    return 0;

}

 



8,Which of the above three functions are likely to cause problem with pointers  
(a) Only f3 (b) Only f1 and f3 (c) Only f1 and f2 (d) f1 , f2 ,f3

C/C++ code

int *f1(void) {

    int x =10;

    return(&x);

}

int *f2(void) {

    int*ptr;

    *ptr =10;

    return ptr;

}

int *f3(void) {

    int *ptr;

    ptr=(int*) malloc(sizeof(int));

    return ptr;

}

 



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

C/C++ code

int main() {

    int i=3;

    int j;

    j = sizeof(++i+ ++i);

    printf("i=%d j=%d", i ,j);

    return 0;

}

 



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

C/C++ code

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);

    return 0;

}

 

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;

}

 



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

C/C++ code

void e(int );

int main() {

    int a;

    a=3;

    e(a);

    return 0;

}

 

void e(int n) {

    if(n>0) {

        e(--n);

        printf("%d" , n);

        e(--n);

    }

}

 



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

C/C++ code

typedef int (*test) ( float * , float*)

test tmp;

 



13,The output for this program is: (a) 5 (b) 6 (c) 9 (d) None of the above

C/C++ code

int main() {

    char *p;

    char buf[10] ={ 1,2,3,4,5,6,9,8};

    p = &((buf+1)[5]);

    printf("%d" , *p);

    return 0;

}

 



14,The output for this program is: (a) ab (b) cd (c) ef (d) gh

C/C++ code

void f(char**);

int main() {

    char * argv[] = { "ab" ,"cd" , "ef" ,"gh", "ij" ,"kl" };

    f( argv );

    

    return 0;

}

void f( char **p ) {

    char* t;

    t= (p+= sizeof(int))[-1];

    printf( "%s" , t);    

}

 



15,The output for this program is: (a) 7 (b) 6 (c) 5 (d) 3

C/C++ code

#include<stdarg.h>

int ripple ( int , ...);

int main(){

    int num;

    num = ripple ( 3, 5,7);

    printf( " %d" , num);

    return 0;

}

 

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;

}

 



16, The value of j at the end of the execution of the this program is:
 (a) 10 (b) 15 (c) 6 (d) 7

C/C++ code

int counter (int i) {

    static int count =0;

    count = count +i;

    return (count );

}

int main() {

    int i , j;

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

    j = counter(i);

    return 0;

}

1-5:baacb  6-10:cacca  11-15:accdc  16:b

此答案只作爲參考,不同的編譯器結果是不一樣的。上面是在VC6.0調試運行的。不要迷信編譯器,不要迷信所謂的正確答案。


code:

#include <iostream.h>

#include <stdio.h>

#include <stdlib.h>

#include <stdarg.h>

#include <setjmp.h>


static jmp_buf buf;


struct node { int a; int b; int c; };


typedef int (*test) ( float * , float*) ;

test tmp;


void e(int ); 

void f(char**);

int ripple ( int , ...); 

int counter (int i);


void f1(int *, int); 

void f2(int *, int); 

void(*pp[2]) ( int *, int);


int *ff1(void);

int *ff2(void);

int *ff3(void);


void foo(int [][3] );


int fff ( int x , int n);



int main() {


cout<<"------------begin---------------"<<endl;

    int a; 

    a=4; 

    e(a); 


cout<<endl<<"--------------------------------"<<endl;


char *p; 

    char buf[10] ={ 1,2,3,4,5,6,9,8}; 

    p = &((buf+1)[5]); 

    printf("%d" , *p); 

  

cout<<endl<<"--------------------------------"<<endl;


char * argv[] = { "ab" ,"cd" , "ef" ,"gh", "ij" ,"kl" }; 

    f( argv );


cout<<endl<<"--------------------------------"<<endl;


int num; 

    num = ripple ( 3, 5, 7); 

    printf( "%d" , num); 

 

cout<<endl<<"--------------------------------"<<endl;


int i , j; 

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

j = counter(i); 

cout<<j;


cout<<endl<<"--------------------------------"<<endl;


//int a; 

    int b; 

    pp[0] = f1; 

    pp[1] = f2; 

    a=3; 

    b=5; 

    pp[0](&a , b); 

    printf("%d,%d\t" , a ,b); 

pp[1](&a , b); 

    printf("%d,%d\t" , a ,b); 


cout<<endl<<"--------------------------------"<<endl;

//vc6.0不支持下列寫法

/*int a[][3] = { 1,2,3,4,5,6}; 

    int (*ptr)[3] =a; 

    printf("%d %d " ,(*ptr)[1], (*ptr)[2] ); 

    ++ptr; 

    printf("%d %d" ,(*ptr)[1], (*ptr)[2] ); */

   

cout<<endl<<"--------------------------------"<<endl;


int c, d; 

    a=3; 

    b=5; 

    c=a,b; 

    d=(a,b); 

    printf("c=%d," ,c); 

    printf("d=%d" ,d); 


cout<<endl<<"--------------------------------"<<endl;


int array[3][3]= { { 1,2,3}, { 4,5,6}, {7,8,9}}; 

    foo(array); 

    printf("%d" , array[2][1]); 


cout<<endl<<"--------------------------------"<<endl;


i=3; 

    j = sizeof(++i+ ++i); 

    printf("i=%d j=%d", i ,j); 


cout<<endl<<"--------------------------------"<<endl;


int ar[5] = {1,3,6,9,12};

    int *ptr = (int*)(&ar+1);

cout<<ptr<<"---"<<ar<<endl;

    printf("%d %d %d" ,*ptr, *(ar+1), *(ptr-1) );

    

cout<<endl<<"--------------------------------"<<endl;


/*volatile int bbb;

    bbb =3;

    if(setjmp(buf)!=0) {

        printf("%d ", bbb);

        exit(0);

    } 

    bbb=5;

    longjmp(buf , 1);*/


cout<<endl<<"--------------------------------"<<endl;


struct node s= { 3, 5,6 };

    struct node *pt = &s;

    printf("%d" , *(int*)pt);


cout<<endl<<"--------------------------------"<<endl;


cout<<fff (3, 4)<<endl;


cout<<endl<<"--------------------------------"<<endl;

    return 0;


void e(int n) { 

    if(n>0) { 

        e(--n); 

        printf("%d" , n); 

        e(--n); 

    } 

}


void f( char **p ) { 

    char* t; 

    t= (p+= sizeof(int))[-1]; 

    printf( "%s" , t);     


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; 


int counter (int i) { 

    static int count =0; 

    count = count +i; 

    return (count ); 


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; 

}


int *ff1(void) { 

    int x =10; 

    return(&x); 

int *ff2(void) { 

    int*ptr; 

    *ptr =10; 

    return ptr; 

int *ff3(void) { 

    int *ptr; 

    ptr=(int*) malloc(sizeof(int)); 

    return ptr; 

}


void foo( int b[][3]) { 

    ++ b; 

    b[1][1] =9; 


int fff ( int x , int n) {

    int val;

    val =1;

    if (n>0) {

        if (n%2 == 1) val = val *x;

        val = val * fff(x*x , n/2);

    }

    return val;

}

/*第4題解答:2,5 

*(a+1)就是a[1],*(ptr-1)就是a[4],執行結果是2,5 

  &a+1不是首地址+1,系統會認爲加一個a數組的偏移,是偏移了一個數組的大小(本例是5個int) 

  int *ptr=(int *)(&a+1); 

  則ptr實際是&(a[5]),也就是a+5 

原因如下: 

  &a是數組指針,其類型爲 int (*)[5]; 

  而指針加1要根據指針類型加上一定的值,不同類型的指針+1之後增加的大小不同。 

  a是長度爲5的int數組指針,所以要加 5*sizeof(int) 

  所以ptr實際是a[5] 

  但是prt與(&a+1)類型是不一樣的(這點很重要) 

  所以prt-1只會減去sizeof(int*) 

  a,&a的地址是一樣的,但意思不一樣 

a是數組首地址,也就是a[0]的地址,&a是對象(數組)首地址, 

    a+1是數組下一元素的地址,即a[1],&a+1是下一個對象的地址,即a[5].


*/

result:
------------begin---------------
0120301
--------------------------------
9
--------------------------------
gh
--------------------------------
5
--------------------------------
15
--------------------------------
5,5     5,5
--------------------------------

--------------------------------
c=3,d=5
--------------------------------
9
--------------------------------
i=3 j=4
--------------------------------
0x0012FF18---0x0012FF04
1 3 12
--------------------------------

--------------------------------
3
--------------------------------
81

--------------------------------
Press any key to continue
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章