《为c程序员准备的0x10个最佳问题》由会员分享,可在线阅读,更多相关《为c程序员准备的0x10个最佳问题(29页珍藏版)》请在金锄头文库上搜索。
1、为 C 程序员准备的 0x10 个最佳问题(1)整个测试遵循以下的约定:u 假定在所有的程序中必须的头文件都已经被正确包含。考虑如下的数据类型:u char 为 1 个字节u int 为 4 个字节u long int 为 4 个字节u float 为 4 个字节u double 为个 8 字节u long double 为 8 个字节u 指针为 4 个字节1. Consider the following program:#includestatic jmp_buf buf;main()volatile int b;b =3;if(setjmp(buf)!=0) printf(%d , b)
2、; exit(0);b=5;longjmp(buf , 1);The output for this program is: (a) 3(b) 5(c) 0(d) None of the above2. Consider the following program:main()struct node int a;int b;int c; ;struct node s= 3, 5,6 ;struct node *pt = printf(%d , *(int*)pt);The output for this program is: (a) 3(b) 5(c) 6(d) 73. Consider t
3、he following code segment:int foo ( int x , int n)int val;val =1;if (n0) if (n%2 = 1) val = val *x;val = val * foo(x*x , n/2);return val;What function of x and n is compute by this code segment? (a) xn(b) x*n(c) nx(d) None of the above4. Consider the following program:main()int a5 = 1,2,3,4,5;int *p
4、tr = (int*)(printf(%d %d , *(a+1), *(ptr-1) );The output for this program is: (a) 2 2(b) 2 1(c) 2 5(d) None of the above 5. Consider the following program:void foo(int 3 ); main()int a 33= 1,2,3 , 4,5,6,7,8,9;foo(a);printf(%d , a21);void foo( int b3)+ b; b11 =9;The output for this program is: (a) 8(
5、b) 9(c) 7(d) None of the above6. Consider the following program:main()int a, b,c, d;a=3;b=5;c=a,b;d=(a,b);printf(c=%d ,c); printf(d=%d ,d);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=57. Consider the following program:main()int a3 = 1,2,3 ,4,5,6;int (*ptr)3 =a;printf(%
6、d %d ,(*ptr)1, (*ptr)2 ); +ptr;printf(%d %d ,(*ptr)1, (*ptr)2 );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 above8. Consider following functionint *f1(void)int x =10;return(int *f2(void)int*ptr;*ptr =10;return ptr;int *f3(void)int *ptr;ptr=(int*) malloc(sizeof(in
7、t);return ptr;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 ,f39. Consider the following program:main()int i=3;int j;j = sizeof(+i+ +i);printf(i=%d j=%d, i ,j);The output for this program is:(a) i=4 j=2(b) i=3
8、j=2(c) i=3 j=4(d) i=3 j=610. Consider the following program:void f1(int *, int); void f2(int *, int); void(*p2) ( int *, int);main()int a;int b;p0 = f1;p1 = f2;a=3;b=5; p0(printf(%dt %dt , a ,b);p1(printf(%dt %dt , a ,b);void f1( int* p , int q)int tmp;tmp =*p;*p = q;q= tmp; void f2( int* p , int q)
9、int tmp;tmp =*p;*p = q;q= tmp; 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 311. Consider the following program:void e(int ); main()int a;a=3;e(a);void e(int n)if(n0)e(-n);printf(%d , n);e(-n);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 11
10、2. Consider following declarationtypedef int (*test) ( float * , float*)test tmp;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 13. Consider the fol
11、lowing program:main()char *p;char buf10 = 1,2,3,4,5,6,9,8; p = (buf+1)5;printf(%d , p);The output for this program is: (a) 5(b) 6(c) 9(d) None of the above14. Consider the following program:Void f(char*);main()char * argv = ab ,cd , ef ,gh, ij ,kl ;f( argv ); void f( char *p )char* t;t= (p+= sizeof(
12、int)-1;printf( %s , t);The output for this program is: (a) ab(b) cd(c) ef(d) gh15. Consider the following program:#includeint ripple ( int , .); main()int num;num = ripple ( 3, 5,7);printf( %d , num);int ripple (int n, .)int i , j;int k; va_list p; k= 0;j = 1;va_start( p , n); for (; j0) if (n%2 = 1
13、) product = product*val;n = n/2; val = val* val;/* Code raise a number (x) to a large power (n) using binary doubling strategy */ Algorithm description (while n0) if next most significant binary digit of n( power) is onethen multiply accumulated product by current val , reduce n(power) sequence by a
14、 factor of two using integer division .get next val by multiply current value of itself Answer 4.The answer is (c)type of a is array of int type of &a is pointer to array of int Taking a pointer to the element one beyond the end of an array is sure to work. Answer 5.The answer is (b)Answer 6.The ans
15、wer is (c)The comma separates the elements of a function argument list. The comma is also used as an operator in comma expressions. Mixing the two uses of comma is legal, but you must use parentheses to distinguish them. the left operand E1 is evaluated as a void expression, then E2 is evaluated to give the result and type of the comma expression. By recursion, the expression E1,