读书人

筒子们来测试上C语言的基础水平

发布时间: 2013-01-23 10:44:49 作者: rapoo

筒子们来测试下C语言的基础水平
本帖最后由 thefirstz 于 2011-07-29 20:53:26 编辑 1,The output for this program is: (a) 3 (b) 5 (c) 0

#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
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
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
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
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
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
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
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
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
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


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
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
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
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
#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

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: a a a b b
6-10: c a c c a
11-15: a c c d c
16: b


第一个是蒙的。这就验证去
[解决办法]
第一个应该是5,直接读寄存器中的数据了。
[解决办法]
不要迷信书、考题、老师、回帖;
要迷信CPU、编译器、调试器、运行结果。

[解决办法]
1-5: b a a c b
6-10: c a c c a
11-15: a c c d c
16: b

其中不能直接测试出答案的是8和12.

8. 只调用f1不会出错。但是int *p = f1() 之后,对*p的读或写均可能出错。故f1错误
f2错误,这个不用说了
f3未赋初值,但这算不上指针错误(甚至算不上错误)。故f3正确

12. C, 不用说了。


其他个人觉得有难度的题目
1. setjmp、longjmp之前没用过,看一下这两个函数的说明就明白了。
4. &a的类型是 int * [5], 因此(&a+1)的和&(a[5])是一样的。
6. c=a,b; 等号优先级比逗号高。
9. sizeof()中的表达式不会计算,只判断类型。要是加一个答案(5,4),错的人估计就很多了(包括我)
13. (buf+1)[5] = buf[6]
15. 两个知识点。一是va_start、va_arg函数的用法;另一个是i &=i-1代表的意义
[解决办法]
引用:
第一个应该是5,直接读寄存器中的数据了。


不对。volatile保证数据每次都从内存中取。
[解决办法]
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); //ptr->a[5];
printf("%d %d" , *(a+1), *(ptr-1) ); //a+1 -> a[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]); //9
return 0;
}

void foo( int b[][3]) { //b->{ { 1,2,3} , { 4,5,6},{7,8,9}}
++ b; // b->{{4,5,6},{7,8,9},{}}
b[1][1] =9; //b[1]->{7,8,9} b[1][1]->8


}



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; //(c=a),b
d=(a,b);
printf("c=%d" ,c); //3
printf("d=%d" ,d); //5
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)->{1,2,3} //output: 2 3
++ptr;
printf("%d %d" ,(*ptr)[1], (*ptr)[2] ); //*ptr->{4, 5, 6} //output 5 6
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);
} //return the pointer of the local memery
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); //sizeof是运算符 一般是预编译确定的 里面运算不执行
printf("i=%d j=%d", i ,j); //i=3 j=4
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

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


p[1](&a , b);
printf("%d\t %d\t" , a ,b); //5 5
return 0;
}

void f1( int* p , int q) { //q 传值
int tmp;
tmp =*p;
*p = q;
q= tmp;
}
void f2( int* p , int q) { //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);
}
}

e(3):
e(2)
printf("%d", 2) //2
e(1) // 0

e(2):
e(1)
printf("%d", 1); //1
e(0) //不打印

e(1):
e(0)
printf("0")
e(-1) //不打印

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

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

int main() {
char *p;
char buf[10] ={ 1,2,3,4,5,6,9,8};
p = (buf+1)[5]; //buf[6]
printf("%d" , p);
return 0;
}


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

return 0;
}
void f( char **p ) {
char* t;
t= (p+= sizeof(int))[-1]; //p+=4 ->{"ij","kl" } (p+=sizeof(int))[0]->"ij"


printf( "%s" , t); //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);
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); //i= 5 , 7
for (; i; i &=i-1 ) // (;i=5 ; i=4) k=1 (;i=4;i=0) k=2 (;i=7; i=6) k=3 (;i=6;i=4) k=4 (;i=4;i=0) k=5
++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

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); //0+0 0+1 1+2 3+3 6+4 10+5
return 0;
}
[解决办法]
抛砖
1、第一次见到setjmp/longjmp函数,但是这个题目应该主要考察volatile
2、指针的类型转换,把struct类型的指针转换为int型的
3、不懂
4、csdn有过详细的讨论,当时也是看了那个帖子才懂的。具体的找不到了,贴个其他的
http://www.cnblogs.com/ylucy/archive/2011/05/03/2035360.html
5、foo(int b[][3])实际等价于 foo(int **b) .++b指向了{4,5,6}。那么b[1][1]实际就是修改了a[2][1]
的内容。
6、逗号表达式的优先级低。
7、ptr是数组的指针,开始指向{1,2,3},加一后指向{4,5,6}
8、f1是使用了局部变量的地址。f2是返回了常量的地址,这个常量是不能修改的
9、开始只看到了++i+ ++i,认为结果是编译器相关的。但是看了matrixcl的分析,才知道是考察sizeof的
10、f1,f2作用相同,都是给*p赋值。p是函数指针的数组。p[0]调用f1,p[1]调用f2
11、一步步的写出递归过程能得出结果,但是这里是a=3,比较好分析,要是a=30就不知道改怎么分析了
12、tmp是函数指针
13、14:和4一样,指针和数组名混用
15、不懂
16、考察static。函数功能从1加到5
[解决办法]

引用:
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……



看见第一题的setjmp.h就懵了,好像考得是汇编吧,jmp long buf(跳转buf)什么的;


第二题*(int*)pt是不是二级指针。。。没学好。。。
第三题带入个x=2,n=3发现是8(2^3)
第四题应该选b,*ptr指向*(a+1),*(ptr-1)应该是a[0]

发现自己不会的好多,看来还得好好练习,好好看书去了,期待楼主详细答案。
[解决办法]
考擦了很多指针的用法,不错 好贴
1 b
volatile 对b 不会进行优化,longjmp(buf , 1); 可以跳转到 setjmp的地方
2 a

3 a

4 b
ptr 得到 a 数组的首地址的下一个

5 b

6 不会

7 a

8 c
局部变量 在程序结束后 会被编译器收回 ,返回其地址 ,行为不可预知。

9 不会

10 a
f() 这个类型 其实也是 指针地址
p[0](&a , b); 等同于 f1() 只有传入地址的变量其值才会改变

11 b
画出函数的调用图 就可以

12 c

13 b

14 d

15 不会

16 b
静态变量 在程序开始时就初始化 。

[解决办法]

引用:
1-5 b,a,a,c,b
6-10 c,b,c,c,a
11-15 a,c,c,d,d
16 b
13题应该是输出*p的值吧?


13题有编译错误, char *p; 定义改成 char p;
[解决办法]
引用:
引用:
引用:

喜欢按着顺序一题一题解决,发现到了第四题,无法理解。。。

4. &a的类型是 int * [5], 因此(&a+1)的和&(a[5])是一样的。

看不大懂。

如果说&a就是int * [5],(&a+1)的和&am……


因为 &a 的类型是 int *[5], 所以 &a+1 = &(a+5), &a+2 = &(a+10).
如果有&a-1的话,&a-1 = &(a-5)
[解决办法]
1~5:b a a c b
6~10:c a c c a
11~15:a c c d c
16:b

13题改为:

int main() {
char p;
char buf[10] ={ 1,2,3,4,5,6,9,8};
p = (buf+1)[5];
printf("%d" , p);
return 0;
}

[解决办法]
引用:
第一个应该是5,直接读寄存器中的数据了。


我只看了第一题,同选这个

读书人网 >C语言

热点推荐