C语言高级测试:为C程序员准备的0x10个最佳问题

整个测试遵循以下的约定:

       假定在所有的程序中必须的头文件都已经被正确包含。

考虑如下的数据类型:

       char 为1个字节

       int 为4个字节

       long int 为4个字节

       float 为4个字节

       double 为个8字节

       long double 为 8个字节

       指针为4个字节

1. Consider the following program:

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

}

The output for this program is:

(a) 3

(b) 5

(c) 0

(d) None of the above

2. Consider the following program:

main()

{

struct node

{

int a;

int b;

int c;

};

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

struct node *pt = &s;

printf("%d" ,  *(int*)pt);

}

The output for this program is:

(a) 3

(b) 5

(c) 6

(d) 7

3. Consider the following code segment:

int  foo ( int x , int  n)

{

int val;

val =1;

if (n>0)

{

if (n%2 == 1)  val = val *x;    //n为奇数

val = val * foo(x*x , n/2);

}

return val;

}

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

4. Consider the following program:

main()

{

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

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

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 [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指向{4,5,6}

b[1][1] =9;   //修改的是a[2][1]

}

The output for this program is:

(a) 8

(b) 9

(c) 7

(d) None of the above

6. 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=5

7. Consider the following program:

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

}

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

8. Consider following function

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;

}

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

9. 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 j=2

(c) i=3 j=4

(d) i=3 j=6

10. Consider the following program:

void f1(int *, int);

void f2(int *, int);

void(*p[2]) ( 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);

}

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;

}

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

11. Consider the following program:

void e(int );

main()

{

int a;

a=3;

e(a);

}

void e(int n)

{

if(n>0)

{

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 1

12. Consider following declaration

typedef 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 following program:

main()

{

char *p;

char buf[10] ={ 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 above

14. 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(int))[-1];

printf( "%s" , t);

}

The output for this program is:

(a) ab

(b) cd

(c) ef

(d) gh

15. Consider the following program:

#include<stdarg.h>

int 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 (; j<n;  ++j)

{

i =  va_arg( p , int);

for (; i;    i &=i-1  )

++k;

}

va_end( p);

return k;

}

The output for this program is:

(a) 7

(b) 6

(c) 5

(d) 3

16. Consider the following program:

int counter (int i)

{

static int count =0;

count = count +i;

return (count );

}

main()

{

int i , j;

for (i=0; i <=5; i++)

j = counter(i);

}

The value of j at the end of the execution of the this program is:

(a) 10

(b) 15

(c) 6

(d) 7

Answer With Detailed Explanation

_____________________________________________________________

Answer 1.

The answer is (b)

volatile variable isn't affected by the optimization. Its value after the longjump is the last value variable assumed.

b last value is 5 hence 5 is printed.

setjmp : Sets up for nonlocal goto /* 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: longjmp Performs nonlocal goto /* 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)

Answer 2.

The answer is (a)

The members of structures have address in increasing order of their declaration. If a pointer to a structure is cast to the type of a pointer to its first member, the result refers to the first member.

Answer 3.

The answer is (a)

Non recursive version of the program

int  what ( int x , int  n)

{

int val;

int product;

product =1;

val =x;

while(n>0)

{

if (n%2 == 1)

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

}

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 answer 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, E2, ..., En

results in the left-to-right evaluation of each Ei, with the value and type of En giving the result of the whole expression.

c=a,b;  / *yields c=a* /

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

Answer 7.

The answer is (a)

/* ptr is pointer to array of 3 int */

Answer 8.

The answer is (c)

f1 and f2 return address of local variable ,when function exit local variable disappeared

Answer 9.

The answer is (c)

sizeof operator gives the number of bytes required to store an object of the type of its operand . The operands is either an expression, which is not evaluated ( (++i + ++ i ) is not evaluated so i remain 3 and j is sizeof int that is 4) or a parenthesized type name.

Answer 10.

The answer is (a)

void(*p[2]) ( int *, int);

define array of pointer to function accept two argument that is pointer to int and return int. p[0] = f1; p[1] = f2 contain address of function .function name without parenthesis represent address of function Value and address of variable is passed to function only argument that is effected is a (address is passed). Because of call by value f1, f2 can not effect b

Answer 11.

The answer is (a)

Answer 12.

The answer is (c)

C provide a facility called typedef for creating new data type names, for example declaration

typedef char string

Makes the name string a synonym for int .The type string can be used in declaration, cast, etc, exactly the same way that the type int can be. Notice that the type being declared in a typedef appears in the position of a variable name not after the word typedef.

Answer 13.

The answer is (c)

If the type of an expression is "array of T" for some type T, then the value of the expression is a pointer to the first object in the array, and the type of the expression is altered to "pointer to T"

So (buf+1)[5] is equvalent to *(buf +6) or buf[6]

Answer 14.

The answer is (b)

p+=sizeof(int) point to argv[2]

(p+=sizeof(int))[-1] points to argv[1]

Answer 15.

The answer is (c)

When we call ripple value of the first argument passed to ripple is collected in the n that is 3. va_start initialize p to point to first unnamed argument that is 5 (first argument).Each call of va_arg return an argument and step p to the next argument. va_arg uses a type name to determine what type to return and how big a step to take Consider inner loop

(; i; i&=i-1) k++ /* count number of  1 bit in i *

in five number of 1 bits is (101) 2

in seven number of 1 bits is (111) 3

hence k return 5

example

let  i= 9 = 1001

i-1  = 1000

(i-1) +1 = i

1000

+1

1 001

The right most 1 bit of i has corresponding 0 bit in i-1 this way i & i-1, in a two complement number system will delete the right most 1 bit I(repeat until I become 0 gives number of 1 bits)

Answer 16.

The answer is (b)

Static variable count remain in existence rather than coming and going each time function is called

so first call counter(0) count =0

second call counter(1) count = 0+1;

third call counter(2) count = 1+2; /* count = count +i */

fourth call counter(3) count = 3+3;

fifth call counter(4) count = 6+4;

sixth call counter(5) count = 10+5;

_____________________________________________________________

为C程序员准备的0x10个最佳问题相关推荐

  1. 读书笔记之:C/C++程序员实用大全—C/C++最佳编程指南

    这本书中列出了31章共1500个知识点,带有很多的例子.本书适合对C/C++有一些了解的程序员进行查缺补漏.因为这里边对知识点的讲解比较凌乱不是很条理,前面的内容有时就用到了后面内容,如果你不是了解的 ...

  2. 从程序员到架构师的最佳技术成长之路

    我把程序员到架构师的技术成长之路分为几个典型的阶段:工程师 - 高级工程师 - 技术专家 - 初级架构师 - 中级架构师 - 高级架构师,总的成长原则是每个阶段都需要"积累经验.拓宽视野.深 ...

  3. 这可能是JAVA程序员进阶架构师的最佳之路了 !

    随着大数据时代的到来,[这次国家教育部的改革要动真格了],JAVA程序员们仅有的一点点竞争力很快就不复存在,为什么这么说呢? 人生别只顾低头拉车,更要抬头看路! 国家教育部全面改革:大数据领衔 所有高 ...

  4. 程序员喜欢的 5 款最佳最牛代码比较工具

    点击上方"码农突围",马上关注 这里是码农充电第一站,回复"666",获取一份专属大礼包 真爱,请设置"星标"或点个"在看&quo ...

  5. 非常适合程序员学习新技能的最佳网站

    今天给程序员们整理了一份福利,想要学习新编程技能的朋友千万不要错过啦,针对程序员推荐了50个适合学习新技能的最佳网站,涵盖了各类编程语言.数据处理.艺术.科技等等.它们可以教会你实践练 习任何技能,从 ...

  6. Java程序员必备的6款最佳开发工具, 你的最爱是哪款?

    下面,我将为大家介绍6款Java开发必备工具. 1.Jad Jad用于反编译Java类.你只需要发出jad命令,就可以用纯文本的形式读码.有些新出来的Java类需要你在缺乏文档的情况下使用jar文件. ...

  7. 大牛手把手带你!2021新一波程序员跳槽季,大牛最佳总结

    前言 说不焦虑其实是假的,因为无论是现在还是最近几年,很早就有人察觉Android开发的野蛮生长时代已经过去.过去的优势是市场需要,这个技术少有人有,所以在抢占市场的时候,基本上满足需要就已经可以了. ...

  8. Erlang之父给程序员的两点忠告 | 缅怀

    整理 | 伍杏玲 出品 | CSDN(ID:CSDNnews) 北京时间 4月20日,据Erlang Solutions.Erlang Factories的创始人Francesco Cesarini的 ...

  9. 博文视点大讲堂35期-It's Android Time:程序员创富有道! 圆满结束

    博文视点大讲堂35期 It's Android Time:程序员创富有道! 暨<Google Android创赢路线与产品开发实战>读者见面会圆满结束 PPT下载 面向移动终端的应用开发作 ...

最新文章

  1. 消息中间件—简谈Kafka中的NIO网络通信模型
  2. ExtJs window(一)使用API以及window常用属性和方法
  3. 一个图片展示效果的站点
  4. 计算机应用技术的历史与现状,浅析计算机应用技术的现状及发展趋势
  5. 部署SpringBoot项目到腾讯云或其他服务器
  6. 怎么讲gis里的符号化_地信(GIS)方向考研~?测绘科学与技术
  7. Java 核心编程技术干货,2019 最新整理版
  8. Linux 命令行快捷键
  9. 中南大学计算机基础考试试题,[中学]中南大学计算机基础考试题库试题.doc
  10. 【知识分享】Batch(批处理)-学生管理系统可视化界面的应用
  11. 网络工程师(软考)学习笔记3--计算机网络体系结构2
  12. WA47电子管麦克风
  13. 古方怡雪祛斑效果怎么样,实话实说!
  14. 移动端页面底部导航被浏览器工具栏遮盖解决方法
  15. shader实现飞线效果(three.js练习)
  16. db4小波的一次分解与重构
  17. PDF修改目录和跳转
  18. mingw+msys windows下配置
  19. 2021-2025年中国人工膝关节行业市场供需与战略研究报告
  20. vue3的pinia详解

热门文章

  1. 维基解密曝CIA 入侵苹果、安卓机、电视,快来围观8761份泄密文
  2. 欧盟批准ATT收购时代华纳 或年底前完成交易
  3. 技术公开课:SQL Server 索引优化原则与工具
  4. 感恩节快乐,PM2小窍门致NodeJS开发者!
  5. 3分钟快速presentation
  6. 解决:一个服务器oracle多实例有一个实例没启动,需手动启动操作指引教程
  7. 关键时刻不可或缺的5款高科技紧急应用
  8. 用babel cli编译用ES6写的JSX
  9. 第一个Sprint冲刺成果
  10. java工程开发之图形化界面之(第六课)