Vous êtes sur la page 1sur 8

THE TEST ON C

TimeLeft=1:34:41
1
Explain the output of the following program.

#define FALSE -1
#define TRUE 1
#define NULL 0
int main()
{
if(NULL)
puts("NULL");
else if(FALSE)
puts("TRUE");
else
puts("FALSE");
} ?

4Mark
2
#define MAX 10
void fun1(int data[])
{
int index;
for (index = 0; index < MAX; ++index)
data[index] = 0;
}

void fun2(int *data_ptr)
{
Page 1 of 8
2/6/2009 http://10.203.161.13/OES/take.jsp?s1=504&s2=20302
int index;
for (index = 0; index < MAX; ++index)
*(data_ptr + index) = 0;
}
What are fun1() and fun2() trying do? What is the
difference between them? ?

4Mark
3
#define SIZE 20

void function1(void)
{
char *s1 = "my";
char * s2 = "program";
char *s3 = strcat(s1,s2);
printf("%s",s3);
}

void function2(void)
{
char *s1 = (char *) malloc(SIZE * sizeof(char));
strcpy(s1,"my");
char *s2 = "program";
char *s3 = strcat(s1,s2);
printf("%s\n",s3);
}
Explain what would happen at function1 and function2?
Point out the bugs, if any. ?
4Mark
Page 2 of 8
2/6/2009 http://10.203.161.13/OES/take.jsp?s1=504&s2=20302

4
int i =0;

void f1(void)
{
static int k;
i = 50;
printf("%d\n",k);
}

int main()
{
int j;
f1();
i =0;
printf(" i in main %d\n",i);
f1();
printf(" i after call%d\n",i);
}
Explain the output of this program? ?

4Mark
int main()
{
int i;
for (i = 0; i < 3; ++i)
{
Page 3 of 8
2/6/2009 http://10.203.161.13/OES/take.jsp?s1=504&s2=20302
5
int t = 1;
static int i = 1;
int *ptr = &i;

printf("%d %d\n",t, *ptr);
t = t + 1;
*ptr = *ptr + 1;
}
}
Explain the output of this program. ?

4Mark
6
For which input the following program would print
YES? Explain your answer.

int main()
{
int a;
printf( "Enter the number to be tested: " );
scanf( "%d", &a );
if(a & ( a >> 1 ) == 0 )
{
printf( "YES");
}
else
{
printf( "NO");
}
} ?
Page 4 of 8
2/6/2009 http://10.203.161.13/OES/take.jsp?s1=504&s2=20302

4Mark
7
void f2(int);
void f1(int a)
{
if( a )
f2( a - 1 );
printf("%d", a);
}

void f2(int b)
{
printf(".");
if(b)
f1( b - 1 );
}

int main(void)
{
f1(5);
}
Explain the output of this program. ?

4Mark
char takes 1 byte, int takes 4 bytes, double takes 8 bytes.
struct S1 {
char c;
int i[2];
Page 5 of 8
2/6/2009 http://10.203.161.13/OES/take.jsp?s1=504&s2=20302
8
double v;
} SA1;

struct S2 {
double x;
int i[2];
char c;
} SA2;


What will be the size of these structures? ?

4Mark
9
What would be output of this program? What are the
potential issues with respect to the followng piece of
code?
int main()
{
FILE * fp = fopen("test.txt", "r");
char line[100];

while( ! feof(fp) ) {
fgets(line, sizeof(line), fp);
fputs(line, stdout);
}
}
Note: Assume that a file "test.txt" exists and the content
is
abc
def ?
Page 6 of 8
2/6/2009 http://10.203.161.13/OES/take.jsp?s1=504&s2=20302

4Mark
10
int main(void)
{
int **pptr,*ptr, q;
ptr = &q;
pptr = &ptr;
q = 1;
printf("%p ", ptr);
*ptr++;
printf("%d %p\n", q, ptr);
**pptr++;
printf("%d %p\n", q, *pptr);
}
What would be the output of the above program?
Justify your answer. ?

4Mark
11
int main()
{
int i,j;
int ctr = 0;
int myArray[2][3];
for (i=0; i< 2; i++)
for (j=0; j< 3; j++)
{
myArray[i][j] = ctr;
++ctr;
Page 7 of 8
2/6/2009 http://10.203.161.13/OES/take.jsp?s1=504&s2=20302




}
for(i=0;i<3;i++)
for(j=0;j<2;j++)
printf("%d\t",*(*(myArray+i)+j));
}
Explain the behaviour of this program. What is the bug
here? ?

4Mark
12
Place holder for Large Question (To de done on
server) ?

4Mark
submit
Page 8 of 8
2/6/2009 http://10.203.161.13/OES/take.jsp?s1=504&s2=20302

Vous aimerez peut-être aussi