The Test On C: PR Attribute ID Marks New Marks C - 2 3 2.5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

THE TEST ON C

(1)
Explain the output of the following program.

int main( )
{
int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
printf("%u %u %u %d \n",a,*a,**a,***a);
printf("%u %u %u %d \n",a+1,*a+1,**a+1,***a+1);
} ?

'a' will return the base address of the first 3x2 array(i.e.base address of {{2,4},{7,8},{3,4}})

'*a' is equivalent to *(a+0),which will return address of first 3x2 array.

'**a' is equivalent to *(*(a+0)+0), will return address of 1st element in 1st 3x2 array

'***a' is equivalent to *(*(*(a+0)+0)+0), will return value of 1st element in 1st 3x2 array(i.e=2)

'a+1' will return base address of 3x2 array stored in memory.

'*a+1' is equivalent to *(a+0)+1, will return adress of element in 1st 3x2 array and in 2nd column

'**a+1' is equivalent to *(*(a+0)+0)+1), will return
PR Attribute ID Marks New Marks
C_2 3 2.5
(2)
void f(void)
{
static int s = 0;
s++;
if(s == 10)
return;
printf("%d ", s);
f();
}

Page 1 of 11
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=420&s2=20440
int main(void)
{
f();
}
Explain the output of this program. ?

ouput:123456789

Value of the static variable s persists between the function calls.
during first call s becomes 0.
after s++,value of s is 1;
it prints the values of s,
Function f() is recursivelt called.
In second call value of s becomes 2(after s++).
2 is printed,rtecursive call to f() is made.
no. of calls to f() will be 10,in 10th call f()returns.
PR Attribute ID Marks New Marks
C_3 3 3
C_5 1 0.5
(3)
struct u
{
union v
{
int i;
int j;
}a[10];
int b[5];
}w;

int main()
{
printf("%d,%d,%d",sizeof(w),sizeof(w.a),sizeof(w.b));
}
What will be the output this program? Justify your answer. ?
Page 2 of 11
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=420&s2=20440

sizeof(w)=24
(includes size of 5 elements of array b(5*4=20,assuming int takes 4bytes and size of largest
element of union=4,which gives 20+4=24)

sizeof(w.a)=40
(=10*(size of max element),which is 4,so 10*4=40))

sizeof(w.b)=20
(4*5=20)
PR Attribute ID Marks New Marks
C_4 4 1
(4)
#define MAX 10
int main()
{
int low[MAX];
int index;
int *high[3];
for(index = 0; index < MAX; index++)
high[index] = low + index;
}
What is this program trying to do? What could be a bug here? ?
Page 3 of 11
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=420&s2=20440

The program is using an array of pointers (ie.*high[3]).
For an array of integers (with size 10),it is trying to point to each element.

Array of pointers contains just 3 pointers,and in this program ,the index value for high[index]
exceeds 3.
PR Attribute ID Marks New Marks
C_2 2 1
C_6 2 1
(5) Place holder for Large Question (To de done on server) ?

PR Attribute ID Marks New Marks
C_12 2 2
Page 4 of 11
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=420&s2=20440
C_14 2 0.5
C_17 4 3.5
C_19 6 4.5
C_20 2 1
(6)
void function(int p_array[], int size)
{
int temp;
int *fptr = p_array;
int *rptr = p_array + size - 1;

for (; fptr < rptr; fptr++, rptr--)
{
temp = *fptr;
*fptr = *rptr;
*rptr = temp;
}
}
What does this function do? Explain ?

this function accepts an array and its size as its arguments.
fptr points to first element of the array.
rptr points to last element of the array.

A loop is run with a condition that specifies till the value of fptr remains less than value of
rptr (which contains address of first n last element in the 1st iteration),their values are swapped.

fptr is incremented to point to next element and rptr is decremented to point to a prevoius element.

as a result this loop reverses the array.

eg array values=3,2,6,7,8,5,1,9,0

the output array is :0,9,1,5,8,7,6,2,3
PR Attribute ID Marks New Marks
C_9 4 4
#include <stdio.h>
int main()
{
FILE *fp;
char name[10];
Page 5 of 11
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=420&s2=20440
(7)
fp = fopen("test","a");
fprintf(fp,"\n%s","1 a 1");
fp = fopen("test","r");
fscanf(fp,"%s",name);
printf("%s\n",name);
}
What is this program trying to do? Explain. What are the pitfalls in the above program? ?

output:
1 a 1

pitfalls
File pointer should be closed after every operation.
PR Attribute ID Marks New Marks
C_11 4 1
(8)
What is the difference between following functions as far as the output is concerned? Why?

void function1( int n )
{
if ( 1 == n ) {
printf( "%d ", n );
return;
}
function1( n - 1 );
printf( "%d ", n );
}

void function2( int n )
{
printf( "%d ", n );
if ( 1 == n ) {
Page 6 of 11
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=420&s2=20440
return;
}
function2( n - 1 );
} ?

Assume n=5.
In the first function ie function1, numbers from 5 to 1(because function reurns at n=1),will be
displayed in reverse order.such as 12345.
This is because recursive call to the function is made before we print the value of n.
Values of n are printed starting from the top most fram on the stack


In second function ie function2, numbers from 5 to 1 are printed in same order ie 54321.
this is because value of n is printed before a recursive call to the function is made(i.e. before
stack frame of next function call is made).
PR Attribute ID Marks New Marks
C_8 4 3
(9)
int main()
{
int c = 0;
do
{
int c = 0;
++c ;
printf("\n c = %d ", c );
}
while( ++c <= 3 );

printf("\n c = %d\n", c );
}
What will be the ouptut of this program? Justify your answer. ?
Page 7 of 11
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=420&s2=20440

output:
c =1
c =1
c =1
c =1
c =4


do while is executed atleast once.
So 1st time when it is executed,c is initialized to 0. c is incremented by 1(i.e. c=1).
1 is printed.
when condition is checked in the while loop the 'c' of outer context is used.
(++c<=3 implies 1<=3,which evaluates to true,so loop runs one more time).

second time 'c' in inner context is again initialized to 1 and printed.
condition is checked in while i.e. 2<=3,which is again true.loop runs again.

third time again 'c' in inner context is initialized to 1 and printed.
Condition is checked(3<=3)--true

fourth time loop runs prints c=1,
condition is checked 4<=3--which evaluates to false.

loop terminates

value of 'c' in last printf statement is =4(outer context)
PR Attribute ID Marks New Marks
C_1 2 2
C_3 3 3
(10)
enum{FALSE,TRUE};

int main()
{
int i=1;
do
{
printf("%d\n",i);
i++;
if(i < 5)
;
else
continue;
}while(TRUE);
return 0;
}
What would the output of this program? Justify your answer. ?
Page 8 of 11
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=420&s2=20440

it will go into infinite loop

i=1 at start,and is printed,then incremented,so now i=2.
since i>5,comes out of loop and checks for condition in while--true

repeats in loop till i>5,and skips to else part,where a continue statement agains forces another
iteration of the loop.

Since there is no control over value of i,so loop iterates infinitely
PR Attribute ID Marks New Marks
C_6 4 3
(11)
#define square(b) b*b
int main()
{
int a;
a=64/square(4);
printf("%d",a);
}
Explain the output of this program. What could be done to avoid a possible side effect here? ?
Page 9 of 11
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=420&s2=20440

output:
64

in the statement a=64/square(4), square macro template is replaced by macro expansion,which is :

a=64/4*4
which is equivalent to a=16*4=64

Thus macro doesn't do what it is supposed to do.

Parantheses are used to avoid such situation i.e. impose order of evaluation.
i.e. #define square(b)((b)*(b))

will solve the purpose of using this macro
PR Attribute ID Marks New Marks
C_1 1 1
C_5 2 2
(12)
Explain the output of the following program. What are the possible bugs in this?
int main(void)
{
char *p;
p = (char*) malloc(sizeof(char)*17);
if(!p)
{
printf("Allocation Error\n");
}
else
{
strcpy(p, "This is 16 chars");
p = (char*)realloc((void *)p,sizeof(char)*18);
if(!p)
{
printf("Allocation Error\n");
}
else
{
strcat(p, ".");
printf("%s",p);
}
Page 10 of 11
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=420&s2=20440




}
} ?

output:
it will give an error due to the folowing line:
p=(char *)realloc((void*)p,sizeof(char)*18);

'p' is char type pointer and typecasting it to void is not possible.

Moreover,Dynamic memory whenever is assigned to a variable should be released explicitly after its
use is over.
otherwise it could lead to memory leaks.
PR Attribute ID Marks New Marks
C_7 4 1
C_10 2 1
Page 11 of 11
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=420&s2=20440

You might also like