C语言测试题

来源:互联网 发布:淘宝商城软包 编辑:程序博客网 时间:2024/04/29 15:26

来自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
原创粉丝点击