Advanced Test in C
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