RSS
热门关键字:  数据挖掘  数据仓库  人工智能  搜索引擎  数据挖掘导论

为C程序员准备的0x10个最佳问题

来源: 作者: 时间:2007-01-27 点击:

为C程序员准备的0x10个最佳问题

日期:2006-10-23 11:59:17  点击:64  作者:  来源:Paid King

数据挖掘研究院

数据挖掘研究院

  数据挖掘研究院

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 2) 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 (d)


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
i
n 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;
  数据挖掘研究院

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
匿名?