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

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

u

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

考虑如下的数据类型:

u

char

1

个字节

u

int

4

个字节

u

long int

4

个字节

u

float

4

个字节

u

double

为个

8

字节

u

long double

8

个字节

u

指针为

4

个字节

1. Consider the following program:

1

cbef093dcc044b2793832001e2365e43.png

#include

<

setjmp.h

>

2

cbef093dcc044b2793832001e2365e43.png

static

jmp_buf  buf;

3

cbef093dcc044b2793832001e2365e43.pngmain()

4

2f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png

918e8df969f9f8c8d002f25cda86cade.png

{

5

df37983f39daa189b8c814e01a6a9011.png

volatile

int

b;

6

df37983f39daa189b8c814e01a6a9011.png  b

=

3

;

7

df37983f39daa189b8c814e01a6a9011.png

if

(setjmp(buf)

!=

0

)

8

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png

918e8df969f9f8c8d002f25cda86cade.png

{

9

df37983f39daa189b8c814e01a6a9011.png    printf(

"

%d

"

, b);

10

df37983f39daa189b8c814e01a6a9011.png    exit(

0

);

11

4a5daaec04350a363f186a4d2c5ed6ce.png  }

12

df37983f39daa189b8c814e01a6a9011.png  b

=

5

;

13

df37983f39daa189b8c814e01a6a9011.png  longjmp(buf ,

1

);

14

0ac3a2d53663ec01c7f7225264eeefae.png}

The output for this program is:

(a) 3

(b) 5

(c) 0

(d) None of the above

2. Consider the following program:

1

cbef093dcc044b2793832001e2365e43.pngmain()22f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png918e8df969f9f8c8d002f25cda86cade.png{3df37983f39daa189b8c814e01a6a9011.pngstructnode4f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png918e8df969f9f8c8d002f25cda86cade.png{5df37983f39daa189b8c814e01a6a9011.pnginta;6df37983f39daa189b8c814e01a6a9011.pngintb;7df37983f39daa189b8c814e01a6a9011.pngintc;84a5daaec04350a363f186a4d2c5ed6ce.png   };9f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngstructnode  s=918e8df969f9f8c8d002f25cda86cade.png{3,5,6};10df37983f39daa189b8c814e01a6a9011.pngstructnode*pt=&s;11df37983f39daa189b8c814e01a6a9011.png   printf("%d",*(int*)pt);120ac3a2d53663ec01c7f7225264eeefae.png}

The output for this program is:

(a) 3

(b) 5

(c) 6

(d) 7

3. Consider the following code segment:

1

cbef093dcc044b2793832001e2365e43.pngintfoo (intx ,intn)22f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png918e8df969f9f8c8d002f25cda86cade.png{3df37983f39daa189b8c814e01a6a9011.pngintval;4df37983f39daa189b8c814e01a6a9011.png  val=1;5df37983f39daa189b8c814e01a6a9011.pngif(n>0)6f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png918e8df969f9f8c8d002f25cda86cade.png{7df37983f39daa189b8c814e01a6a9011.pngif(n%2==1)  val=val*x;8df37983f39daa189b8c814e01a6a9011.png    val=val*foo(x*x , n/2);94a5daaec04350a363f186a4d2c5ed6ce.png  }10df37983f39daa189b8c814e01a6a9011.pngreturnval;110ac3a2d53663ec01c7f7225264eeefae.png}

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:

1

cbef093dcc044b2793832001e2365e43.png

main()

2

2f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png

918e8df969f9f8c8d002f25cda86cade.png

{

3

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png

int

a[

5

]

=

918e8df969f9f8c8d002f25cda86cade.png

{

1

,

2

,

3

,

4

,

5

}

;

4

df37983f39daa189b8c814e01a6a9011.png

int

*

ptr

=

(

int

*

)(

&

a

+

1

);

5

df37983f39daa189b8c814e01a6a9011.png  printf(

"

%d %d

"

,

*

(a

+

1

),

*

(ptr

-

1

) );

6

0ac3a2d53663ec01c7f7225264eeefae.png}

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:

1

cbef093dcc044b2793832001e2365e43.png

void

foo(

int

[][

3

] );

2

cbef093dcc044b2793832001e2365e43.pngmain()

3

2f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png

918e8df969f9f8c8d002f25cda86cade.png

{

4

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png

int

a [

3

][

3

]

=

918e8df969f9f8c8d002f25cda86cade.png

{

918e8df969f9f8c8d002f25cda86cade.png

{

1

,

2

,

3

}

,

918e8df969f9f8c8d002f25cda86cade.png

{

4

,

5

,

6

}

,

918e8df969f9f8c8d002f25cda86cade.png

{

7

,

8

,

9

}

}

;

5

df37983f39daa189b8c814e01a6a9011.png  foo(a);

6

df37983f39daa189b8c814e01a6a9011.png  printf(

"

%d

"

, a[

2

][

1

]);

7

0ac3a2d53663ec01c7f7225264eeefae.png}

8

cbef093dcc044b2793832001e2365e43.png

9

cbef093dcc044b2793832001e2365e43.png

void

foo(

int

b[][

3

])

10

2f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png

918e8df969f9f8c8d002f25cda86cade.png

{

11

df37983f39daa189b8c814e01a6a9011.png

++

b;

12

df37983f39daa189b8c814e01a6a9011.png  b[

1

][

1

]

=

9

;

13

0ac3a2d53663ec01c7f7225264eeefae.png}

The output for this program is:

(a) 8

(b) 9

(c) 7

(d) None of the above

6. Consider the following program:

1

cbef093dcc044b2793832001e2365e43.png

main()

2

2f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png

918e8df969f9f8c8d002f25cda86cade.png

{

3

df37983f39daa189b8c814e01a6a9011.png

int

a, b,c, d;

4

df37983f39daa189b8c814e01a6a9011.png  a

=

3

;

5

df37983f39daa189b8c814e01a6a9011.png  b

=

5

;

6

df37983f39daa189b8c814e01a6a9011.png  c

=

a,b;

7

df37983f39daa189b8c814e01a6a9011.png  d

=

(a,b);

8

df37983f39daa189b8c814e01a6a9011.png  printf(

"

c=%d

"

,c);

9

df37983f39daa189b8c814e01a6a9011.png  printf(

"

d=%d

"

,d);

10

0ac3a2d53663ec01c7f7225264eeefae.png}

11

cbef093dcc044b2793832001e2365e43.png

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:

1

cbef093dcc044b2793832001e2365e43.png

main()

2

2f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png

918e8df969f9f8c8d002f25cda86cade.png

{

3

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png

int

a[][

3

]

=

918e8df969f9f8c8d002f25cda86cade.png

{

1

,

2

,

3

,

4

,

5

,

6

}

;

4

df37983f39daa189b8c814e01a6a9011.png

int

(

*

ptr)[

3

]

=

a;

5

df37983f39daa189b8c814e01a6a9011.png  printf(

"

%d %d

"

,(

*

ptr)[

1

], (

*

ptr)[

2

] );

6

df37983f39daa189b8c814e01a6a9011.png

++

ptr;

7

df37983f39daa189b8c814e01a6a9011.png  printf(

"

%d %d

"

,(

*

ptr)[

1

], (

*

ptr)[

2

] );

8

0ac3a2d53663ec01c7f7225264eeefae.png}

9

cbef093dcc044b2793832001e2365e43.png

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

1

cbef093dcc044b2793832001e2365e43.pngint*f1(void)22f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png918e8df969f9f8c8d002f25cda86cade.png{3df37983f39daa189b8c814e01a6a9011.pngintx=10;4df37983f39daa189b8c814e01a6a9011.pngreturn(&x);50ac3a2d53663ec01c7f7225264eeefae.png}6cbef093dcc044b2793832001e2365e43.png7cbef093dcc044b2793832001e2365e43.pngint*f2(void)82f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png918e8df969f9f8c8d002f25cda86cade.png{9df37983f39daa189b8c814e01a6a9011.pngint*ptr;10df37983f39daa189b8c814e01a6a9011.png*ptr=10;11df37983f39daa189b8c814e01a6a9011.pngreturnptr;120ac3a2d53663ec01c7f7225264eeefae.png}13cbef093dcc044b2793832001e2365e43.png14cbef093dcc044b2793832001e2365e43.pngint*f3(void)152f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png918e8df969f9f8c8d002f25cda86cade.png{16df37983f39daa189b8c814e01a6a9011.pngint*ptr;17df37983f39daa189b8c814e01a6a9011.png  ptr=(int*) malloc(sizeof(int));18df37983f39daa189b8c814e01a6a9011.pngreturnptr;190ac3a2d53663ec01c7f7225264eeefae.png}20cbef093dcc044b2793832001e2365e43.png

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:

1

cbef093dcc044b2793832001e2365e43.pngmain()22f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png918e8df969f9f8c8d002f25cda86cade.png{3df37983f39daa189b8c814e01a6a9011.pnginti=3;4df37983f39daa189b8c814e01a6a9011.pngintj;5df37983f39daa189b8c814e01a6a9011.png  j=sizeof(++i+++i);6df37983f39daa189b8c814e01a6a9011.png  printf("i=%d j=%d", i ,j);70ac3a2d53663ec01c7f7225264eeefae.png}8cbef093dcc044b2793832001e2365e43.png

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:

1

cbef093dcc044b2793832001e2365e43.pngvoidf1(int*,int);2cbef093dcc044b2793832001e2365e43.pngvoidf2(int*,int);3cbef093dcc044b2793832001e2365e43.pngvoid(*p[2]) (int*,int);4cbef093dcc044b2793832001e2365e43.pngmain()52f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png918e8df969f9f8c8d002f25cda86cade.png{6df37983f39daa189b8c814e01a6a9011.pnginta;7df37983f39daa189b8c814e01a6a9011.pngintb;8df37983f39daa189b8c814e01a6a9011.png  p[0]=f1;9df37983f39daa189b8c814e01a6a9011.png  p[1]=f2;10df37983f39daa189b8c814e01a6a9011.png  a=3;11df37983f39daa189b8c814e01a6a9011.png  b=5;12df37983f39daa189b8c814e01a6a9011.png  p[0](&a , b);13df37983f39daa189b8c814e01a6a9011.png  printf("%d\t %d\t", a ,b);14df37983f39daa189b8c814e01a6a9011.png  p[1](&a , b);15df37983f39daa189b8c814e01a6a9011.png  printf("%d\t %d\t", a ,b);160ac3a2d53663ec01c7f7225264eeefae.png}17cbef093dcc044b2793832001e2365e43.png18cbef093dcc044b2793832001e2365e43.pngvoidf1(int*p ,intq)192f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png918e8df969f9f8c8d002f25cda86cade.png{20df37983f39daa189b8c814e01a6a9011.pnginttmp;21df37983f39daa189b8c814e01a6a9011.png  tmp=*p;22df37983f39daa189b8c814e01a6a9011.png*p=q;23df37983f39daa189b8c814e01a6a9011.png  q=tmp;240ac3a2d53663ec01c7f7225264eeefae.png}25cbef093dcc044b2793832001e2365e43.png26cbef093dcc044b2793832001e2365e43.pngvoidf2(int*p ,intq)272f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png918e8df969f9f8c8d002f25cda86cade.png{28df37983f39daa189b8c814e01a6a9011.pnginttmp;29df37983f39daa189b8c814e01a6a9011.png  tmp=*p;30df37983f39daa189b8c814e01a6a9011.png*p=q;31df37983f39daa189b8c814e01a6a9011.png  q=tmp;320ac3a2d53663ec01c7f7225264eeefae.png}33cbef093dcc044b2793832001e2365e43.png

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:

1

cbef093dcc044b2793832001e2365e43.pngvoide(int);2cbef093dcc044b2793832001e2365e43.pngmain()32f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png918e8df969f9f8c8d002f25cda86cade.png{4df37983f39daa189b8c814e01a6a9011.pnginta;5df37983f39daa189b8c814e01a6a9011.png  a=3;6df37983f39daa189b8c814e01a6a9011.png  e(a);70ac3a2d53663ec01c7f7225264eeefae.png}8cbef093dcc044b2793832001e2365e43.png9cbef093dcc044b2793832001e2365e43.pngvoide(intn)102f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png918e8df969f9f8c8d002f25cda86cade.png{11df37983f39daa189b8c814e01a6a9011.pngif(n>0)12f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png918e8df969f9f8c8d002f25cda86cade.png{13df37983f39daa189b8c814e01a6a9011.png    e(--n);14df37983f39daa189b8c814e01a6a9011.png    printf("%d", n);15df37983f39daa189b8c814e01a6a9011.png    e(--n);164a5daaec04350a363f186a4d2c5ed6ce.png  }170ac3a2d53663ec01c7f7225264eeefae.png}18cbef093dcc044b2793832001e2365e43.png19cbef093dcc044b2793832001e2365e43.png

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

1

cbef093dcc044b2793832001e2365e43.png

typedef

int

(

*

test) (

float

*

,

float

*

)

2

cbef093dcc044b2793832001e2365e43.pngtest tmp;

3

cbef093dcc044b2793832001e2365e43.png

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:

1

cbef093dcc044b2793832001e2365e43.png

main()

2

2f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png

918e8df969f9f8c8d002f25cda86cade.png

{

3

df37983f39daa189b8c814e01a6a9011.png

char

*

p;

4

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png

char

buf[

10

]

=

918e8df969f9f8c8d002f25cda86cade.png

{

1

,

2

,

3

,

4

,

5

,

6

,

9

,

8

}

;

5

df37983f39daa189b8c814e01a6a9011.png  p

=

(buf

+

1

)[

5

];

6

df37983f39daa189b8c814e01a6a9011.png  printf(

"

%d

"

, p);

7

0ac3a2d53663ec01c7f7225264eeefae.png}

8

cbef093dcc044b2793832001e2365e43.png

The output for this program is:

(a) 5

(b) 6

(c) 9

(d) None of the above

14. Consider the following program:

1

cbef093dcc044b2793832001e2365e43.pngVoid f(char**);2cbef093dcc044b2793832001e2365e43.pngmain()32f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png918e8df969f9f8c8d002f25cda86cade.png{4f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngchar*argv[]=918e8df969f9f8c8d002f25cda86cade.png{"ab","cd","ef","gh","ij","kl"};5df37983f39daa189b8c814e01a6a9011.png  f( argv );60ac3a2d53663ec01c7f7225264eeefae.png}7cbef093dcc044b2793832001e2365e43.png8cbef093dcc044b2793832001e2365e43.pngvoidf(char**p )92f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png918e8df969f9f8c8d002f25cda86cade.png{10df37983f39daa189b8c814e01a6a9011.pngchar*t;11df37983f39daa189b8c814e01a6a9011.png  t=(p+=sizeof(int))[-1];12df37983f39daa189b8c814e01a6a9011.png  printf("%s", t);130ac3a2d53663ec01c7f7225264eeefae.png}14cbef093dcc044b2793832001e2365e43.png

The output for this program is:

(a) ab

(b) cd

(c) ef

(d) gh

15. Consider the following program:

1

cbef093dcc044b2793832001e2365e43.png#include2cbef093dcc044b2793832001e2365e43.pngintripple (int, 

918e8df969f9f8c8d002f25cda86cade.png);3cbef093dcc044b2793832001e2365e43.pngmain()42f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png918e8df969f9f8c8d002f25cda86cade.png{5df37983f39daa189b8c814e01a6a9011.pngintnum;6df37983f39daa189b8c814e01a6a9011.png  num=ripple (3,5,7);7df37983f39daa189b8c814e01a6a9011.png  printf("%d", num);80ac3a2d53663ec01c7f7225264eeefae.png}9cbef093dcc044b2793832001e2365e43.png10cbef093dcc044b2793832001e2365e43.pngintripple (intn, 

918e8df969f9f8c8d002f25cda86cade.png)112f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png918e8df969f9f8c8d002f25cda86cade.png{12df37983f39daa189b8c814e01a6a9011.pnginti , j;13df37983f39daa189b8c814e01a6a9011.pngintk;14df37983f39daa189b8c814e01a6a9011.png  va_list p;15df37983f39daa189b8c814e01a6a9011.png  k=0;16df37983f39daa189b8c814e01a6a9011.png  j=1;17df37983f39daa189b8c814e01a6a9011.png  va_start( p , n);18df37983f39daa189b8c814e01a6a9011.pngfor(; j

edb48e6f68462ea23d9a824f01de40c5.png918e8df969f9f8c8d002f25cda86cade.png{20df37983f39daa189b8c814e01a6a9011.png    i=va_arg( p ,int);21df37983f39daa189b8c814e01a6a9011.pngfor(; i;    i&=i-1)22df37983f39daa189b8c814e01a6a9011.png++k;234a5daaec04350a363f186a4d2c5ed6ce.png  }24df37983f39daa189b8c814e01a6a9011.pngreturnk;250ac3a2d53663ec01c7f7225264eeefae.png}26cbef093dcc044b2793832001e2365e43.png27cbef093dcc044b2793832001e2365e43.png

The output for this program is:

(a) 7

(b) 6

(c) 5

(d) 3

16. Consider the following program:

1

cbef093dcc044b2793832001e2365e43.pngintcounter (inti)22f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png918e8df969f9f8c8d002f25cda86cade.png{3df37983f39daa189b8c814e01a6a9011.pngstaticintcount=0;4df37983f39daa189b8c814e01a6a9011.png  count=count+i;5df37983f39daa189b8c814e01a6a9011.pngreturn(count );60ac3a2d53663ec01c7f7225264eeefae.png}7cbef093dcc044b2793832001e2365e43.png8cbef093dcc044b2793832001e2365e43.pngmain()92f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png918e8df969f9f8c8d002f25cda86cade.png{10df37983f39daa189b8c814e01a6a9011.pnginti , j;11df37983f39daa189b8c814e01a6a9011.pngfor(i=0; i<=5; i++)12df37983f39daa189b8c814e01a6a9011.png    j=counter(i);130ac3a2d53663ec01c7f7225264eeefae.png}14cbef093dcc044b2793832001e2365e43.png

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

1

cbef093dcc044b2793832001e2365e43.png

int

what (

int

x ,

int

n)

2

2f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png

918e8df969f9f8c8d002f25cda86cade.png

{

3

df37983f39daa189b8c814e01a6a9011.png

int

val;

4

df37983f39daa189b8c814e01a6a9011.png

int

product;

5

df37983f39daa189b8c814e01a6a9011.png  product

=

1

;

6

df37983f39daa189b8c814e01a6a9011.png  val

=

x;

7

df37983f39daa189b8c814e01a6a9011.png

while

(n

>

0

)

8

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png

918e8df969f9f8c8d002f25cda86cade.png

{

9

df37983f39daa189b8c814e01a6a9011.png

if

(n

%

2

==

1

)

10

df37983f39daa189b8c814e01a6a9011.png      product

=

product

*

val;

11

df37983f39daa189b8c814e01a6a9011.png    n

=

n

/

2

;

12

df37983f39daa189b8c814e01a6a9011.png    val

=

val

*

val;

13

4a5daaec04350a363f186a4d2c5ed6ce.png  }

14

0ac3a2d53663ec01c7f7225264eeefae.png}

15

cbef093dcc044b2793832001e2365e43.png

/* Code raise a number (x) to a large power (n) using binary doubling strategy */

Algorithm description

1

cbef093dcc044b2793832001e2365e43.png

(

while

n

>

0

)

2

2f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png

918e8df969f9f8c8d002f25cda86cade.png

{

3

df37983f39daa189b8c814e01a6a9011.png

if

next most significant binary digit of  n( power)

is

one

4

df37983f39daa189b8c814e01a6a9011.png  then multiply accumulated product by current val  ,

5

df37983f39daa189b8c814e01a6a9011.png  reduce n(power)  sequence by a factor of two

using

integer division .

6

df37983f39daa189b8c814e01a6a9011.png

get

next val by multiply current value of itself

7

0ac3a2d53663ec01c7f7225264eeefae.png}

8

cbef093dcc044b2793832001e2365e43.png

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.

1

cbef093dcc044b2793832001e2365e43.png

c

=

a,b;

/

*

yields c

=

a

*

/

2

2f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.pngd

=

(a,b);

/**/

/*

d =b

*/

3

cbef093dcc044b2793832001e2365e43.png

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)

1

cbef093dcc044b2793832001e2365e43.png

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

1

cbef093dcc044b2793832001e2365e43.png

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

1

2f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.png

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

1

cbef093dcc044b2793832001e2365e43.png

let  i

=

9

=

1001

2

cbef093dcc044b2793832001e2365e43.png     i

-

1

=

1000

3

cbef093dcc044b2793832001e2365e43.png    (i

-

1

)

+

1

=

i

4

cbef093dcc044b2793832001e2365e43.png

1000

5

cbef093dcc044b2793832001e2365e43.png

+

1

6

cbef093dcc044b2793832001e2365e43.png

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值