嵌入式 经典嵌入式笔试题集合

 

嵌入式C开发人员笔试题

一、选择题(48%)

备注:非常基本关于C语言的问题,一个信息类(计算机,资讯工程,电子工程, 通信工程)专业的本科毕业生应该达到的水平。题目不难,全部都能快速地答完,当然也需要一定的知识储备。
对于大多数人,我们预期你可能答错 3)   4)  15)题,所以答错3道以内的,我们认为你很棒
答错5道题以内,我们认为你还不错(你还可能答错第9)
如果你有6道以上的题目不能答对,基本上我们都不好说什么了
....

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

   
数据类型    
        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题:#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) 以上均不是


volatile
字面意思是易于挥发的。这个关键字来描述一个变量时,意味着给该变量赋值(写入)之后,马上再读取,写入的值与读取的值可能不一样,所以说它"容易挥发"的。
这是因为这个变量可能一个寄存器,直接与外部设备相连,你写入之后,该寄存器也有可能被外部设备的写操作所改变;或者,该变量被一个中断程序,或另一个进程
改变了.
volatile
不会被编译器优化影响,在longjump,它的值是后面假定的变量值,b最后的值是5,所以5被打印出来
.
setjmp :
设置非局部跳转
/*setjmp.h*/
Stores context information such as register values so that the lomgjmp functioncan return control to the statement following the one calling setjmp.Returns 0when it is initially called.
Lonjjmp:
执行一个非局部跳转
/*setjmp.h*/
Transfers control to the statement where the call to setjmp (which initializedbuf) was made. Execution continues at this point as if longjmp cannot returnthe value 0.A nonvolatile automatic variable might be changed by a call tolongjmp.When you use setjmp and longjmp, the only automatic variables guaranteedto remain valid are those declared volatile.
Note: Test program without volatile qualifier (result may very)

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

结构题的成员在内存中的地址是按照他们定义的位置顺序依次增长的。如果一个结构体

的指针被看成它的第一个成员的指针,那么该指针的确指向第一个成员

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) 以上均不是
此题目较难.
这个程序的非递归版本

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 bycurrent val  ,
  reduce n(power)  sequence by a factor of two using integerdivision .
  get next val by multiply current valueof 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) 25     (d)以上均不是
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)以上均不对

题目自身就给了足够的提示
b[0][0]  = 4
b[1][0]  = 7

  • 0
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值