Advanced Test in C

redbanana 2003-11-27 10:14:11
In all program, assume that the required header file/files has /have been included ;

Consider the data type :
char is 1 byte
int is 2 byte
long int 4 byte
float is 4 byet
double is 8 byte
long double is 10 byte
pointer is 2 byte



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;

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

What function of x and n is compute by this code segment?

(a) xn
(b) x*n
(c) nx
(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[1][1] =9;
}

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
...全文
128 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
redbanana 2003-11-28
  • 打赏
  • 举报
回复
Answer 9.
The answer is (b)

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.
ross33123 2003-11-28
  • 打赏
  • 举报
回复
其他都好理解,第9题有些怪异。
为什么 sizeof(++i) 对 i肯定没有副作用呢?有没有什么标准 的说法?
redbanana 2003-11-27
  • 打赏
  • 举报
回复
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;
}
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

70,019

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧