现在的位置: 首页 > 综合 > 正文

C笔试加面试题

2013年09月02日 ⁄ 综合 ⁄ 共 3619字 ⁄ 字号 评论关闭

考查一个初级嵌入式系统开发人员的C基本功,附有答案题目由资深嵌入式系统专家拟定,目的是考查入门级的嵌入式软件开发人员 Gavin Shaw提供详细解答

 

编者按:非常基本关于C语言的问题,一个信息类(计算机,资讯工程,电子工程, 通信工程)专业的本科毕业生应该达到的水平。题目不难,全部都能快速地答完,当然也需要一定的知识储备。

对于大多数人,我们预期你可能答错 3)   4)  15)题,所以答错3道以内的,我们认为你很棒

答错5道题以内,我们认为你还不错(你还可能答错第9)

如果你有6道以上的题目不能答对,基本上我们都不好说什么了....

 

约定:

   1) 下面的测试题中,认为所有必须的头文件都已经正确的包含了

   2)数据类型    

        char 一个字节 1 byte

        int 两个字节 2 byte (16位系统,认为整型是2个字节)

        long int 四个字节 4 byte

        float  四个字节4 byet

        double 八个字节 8 byte

        long double 十个字节 10 byte

        pointer 两个字节 2 byte(注意,16位系统,地址总线只有16)

 

1: 考查对volatile关键字的认识

#include<setjmp.h>

static jmp_buf  buf;

main()   

{  volatile  int b; 

b =3; 

if(setjmp(buf)!=0)   

{    printf("%d ", b);      exit(0);  } 

b=5; 

longjmp(buf , 1);}   请问,这段程序的输出是

(a) 3

(b) 5

(c) 0

(d) 以上均不是

1:   (b)

volatile字面意思是易于挥发的。这个关键字来描述一个变量时,意味着给该变量赋值(写入)之后,马上再读取,写入的值与读取的值可能不一样,所以说它"容易挥发"的。

这是因为这个变量可能一个寄存器,直接与外部设备相连,你写入之后,该寄存器也有可能被外部设备的写操作所改变;或者,该变量被一个中断程序,或另一个进程改变了.

volatile 不会被编译器优化影响,在longjump,它的值是后面假定的变量值,b最后的值是5,所以5被打印出来.

 

setjmp : 设置非局部跳转 /* setjmp.h*/

 

Stores context information such as register values so that the lomgjmp function can return control to the statement following the one calling setjmp.Returns 0 when it is initially called.

 

Lonjjmp: 执行一个非局部跳转 /* setjmp.h*/

 

Transfers control to the statement where the call to setjmp (which initialized buf) was made. Execution continues at this point as if longjmp cannot return the value 0.A nonvolatile automatic variable might be changed by a call to longjmp.When you use setjmp and longjmp, the only automatic variables guaranteed to remain valid are those declared volatile.

 

Note: Test program without volatile qualifier (result may very)

 

更详细介绍,请参阅 C语言的setjmplongjmp

 

2:考查类型转换

main(){  

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

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

struct node *pt = &s;  

printf("%d" ,  *(int*)pt);}  这段程序的输出是:

(a) 3

(b) 5

(c) 6

(d) 7

2:   (a)

结构体的成员在内存中的地址是按照他们定义的位置顺序依次增长的。如果一个结构体的指针被看成它的第一个成员的指针,那么该指针的确指向第一个成员

 

3:考查递归调用

 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;

} 这段代码对xn完成什么样的功能(操作)?

(a) x^n (xn次幂)

(b) x*n(xn的乘积)

(c) n^x(nx次幂)

(d) 以上均不是

3:  (a)

此题目较难.

这个程序的非递归版本

int  what ( int x , int  n){  int val;  int product;  product =1;  val =x; 

while(n>0)  {   

if (n%2 == 1)       

product = product*val;

/*如果是奇数次幂, x(val) 要先乘上一次,偶数次幂,最后返回时才会到这里乘以1*/ 

val = val* val;    n = n/2;   }   return product;}/* 用二元复乘策略 */

算法描述

(while n>0)  {  if  next most significant binary digit of  n( power)  is one  then multiply accumulated product by current val  ,   reduce n(power)  sequence by a factor of two using integer division .  get next val by multiply current value of itself                   }

 

4:考查指针,这道题只适合于那些特别细心且对指针和数组有深入理解的人

main() { 

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

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

printf("%d %d" , *(a+1), *(ptr-1) );}  这段程序的输出是:

(a) 2 2

(b) 2 1

(c) 2 5

(d) 以上均不是

4:  (c)

a的类型是一个整型数组,它有5个成员

&a的类型是一个整型数组的指针

所以&a + 1指向的地方等同于 a[6]

所以*(a+1) 等同于a[1]

ptr等同 a[6], ptr-1就等同与a[5]

 

5:考查多维数组与指针

void foo(int [][3] );    

main(){ 

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

foo(a); 

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

void foo( int b[][3])

{++ b;

b[1][1] =9;} 

这段程序的输出是:

(a) 8

(b) 9

(c) 7

(d)以上均不对

5:  (b)

题目自身就给了足够的提示

b[0][0]  = 4

b[1][0]  = 7

 

6题目:考查逗号表达式

main(){

 int a, b, c, d; 

a=3;  b=5; 

c=a,b; 

d=(a,b); 

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

printf("d=%d" ,d);}这段程序的输出是:

(a) c=3 d=3

(b) c=5 d=3

(c) c=3 d=5

(d) c=5 d=5

6:  (c)

考查逗号表达式,逗号表达式的优先级是很低的,比赋值(=)的优先级低. 逗号表达式的值就是最后一个元素的值,逗号表达式的还有一个作用就是分割函数的参数列表..E1, E2, ..., En

上面这个表示式的左右是,E1, E2,... En的值被分别计算出来,En计算出来的结构赋给整个逗号表达式

c=a,b;       / *yields c=a* /d=(a,b);    /* d =b  */

 

 

7:考查指针数组

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] );}这段程序的输出是:

(a) 2 3 5 6

(b) 2 3 4 5

(c) 4 5 0 0

(d) 以上均不对

7:  (a)

ptr是一个数组的指针,该数组有3int成员

 

8:考查函数指针

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;}上面这3个函数哪一个最可能引起指针方面的问题

(a)

抱歉!评论已关闭.