Sie sind auf Seite 1von 12

THE TEST ON C

(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");
} ?

output is : TRUE
EXPLANATION:
at step : if(NULL) evaluates to --if(0);so if is not executed
then at next step : else if(FALSE) evaluates to -- if(-1);so will execute it and execute
the line after it: puts("TRUE"); --o/p = TRUE
then at next step : else --will not execute after executing else if statement
PR Attribute ID Marks New Marks
C_1 1 1
C_5 2 2
(2) Place holder for Large Question (To de done on server) ?
Page 1 of 12
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=504&s2=20429

PR Attribute ID Marks New Marks
C_12 2 1.5
C_14 2 2
C_17 4 3
C_19 6 3.5
C_20 2 1.5
(3)
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);
}
Page 2 of 12
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=504&s2=20429
Explain the output of this program. ?

the output is => ...135
i.e 3 dots followed by 1 3 and 5

EXPLANATION => IN MIAN call to f1(5)
now at f1:
void f1(5)
{ if(5)
f2(4); ------------->void f2(4)
printf("%d",a); ''here a = 5 { printf("."); //o/p's a '.'
} if(4)
f1(3); ---> void f1(3)
} {
if(3)
f2(2) --------------------------
printf("%d",a); //here a = 3
}
void f2(2) <--------------------
{ printf(".");//o/p's a '.'
if(2)
void f1(1) <------ f1(1);
{ }
if(1)
f2(0) --->void f2(0)
//a = 1 printf("%d",a) { printf(".")//o/p '.'
if(0) f(0)
}
PR Attribute ID Marks New Marks
C_8 4 4
(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? ?
Page 3 of 12
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=504&s2=20429

OUTPUT IS: 0
i in main 0
0
i after call 0
EXPLANATION IS : IN MIAN
{
first call to f1()--> will initialise a static variable k = 0
then i = 50
then prints k as--> o
then in main-->prints --> i in main 0 (as i is defined in its local scope also therefore
value of is 0
then calls to f1() again-->
will prints k as no change in it --
then back in mian--> will print--> i after call 0(as no change in i local to it
PR Attribute ID Marks New Marks
C_3 3 1
C_5 1 1
(5)
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 4 of 12
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=504&s2=20429

OUTPUT IS --> abc
def
def
EXPLANATION -->
AT FIRST fgets reads line:abc and prints to console
then in loop again get line def from file and prints to console
then feof will return zero this time also and control moves to loop again
and prints def again,after that loop ends as feof() returns non

to resolve this issue :
while(1)
{
fgets(line,sizeof(line),fp);
if(feof(fp))
break;
fputs(line,stdout);
}

this will print like: abc
def
PR Attribute ID Marks New Marks
C_11 4 3
(6)
#define MAX 10
void fun1(int data[])
{
int index;
for (index = 0; index < MAX; ++index)
data[index] = 0;
}

void fun2(int *data_ptr)
{
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? ?
Page 5 of 12
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=504&s2=20429

BOTH FUNCTIONS ARE INITIALISING THE ARRAY,ONE OF THEM IS ACCESSING IT AS AN ARRAY OF INTEGERS AND
OTHER IS ACCESSING IT AS POINTER TO INTEGER (WILL BEHAVE AS ARRAY OF INTEGERS)

THE FUNCTION FUN1 IS
TAKING AN INTEGER ARRAY AS DATA[] AND INITALISING IT AS DATA[i] BY FOR LOOP
AND THE FUNCTION fun2
IS TAKING A INTEGRE POINTER AS *DATA_PTR
AND INITIALISING IT BY MOVING THROUGHTHE INDEXES
PR Attribute ID Marks New Marks
C_2 3 2
(7)
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 6 of 12
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=504&s2=20429

IT WOULD PRINT YES FOR THE INPUTS LIKE--> 1,2,4,5,8,9,10
AS HERE IF STATEMENT IS CHECKING FOR ZERO IN EXPRESSION-->(a & (a>>1))
i.e a IS AND WITH (a AFTER SHIFTING IT BY ONE BIT)
THUS ALL THE NUMEBRS IN BINARY WHICH AFTER SHIFTING BY ONE BIT WILL NOT
HAVE THE ONE BIT IN THE SAME POSITIONS OF EARLIE 1'S
WILL EVALUATE TO ZERO AND THEN IF STATEMENT WILL GIVE RESULT AS 1 AND PRINTS
PR Attribute ID Marks New Marks
C_6 4 2
(8)
char takes 1 byte, int takes 4 bytes, double takes 8 bytes.
struct S1 {
char c;
int i[2];
double v;
} SA1;

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


What will be the size of these structures? ?
Page 7 of 12
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=504&s2=20429

IF THE MACHINE IS 64 BIT MACHINE: THEN A WORD IS 8 BYTE LONG
IN 64 BIT MACHINE SIZE OF
--> SA1 IS: 24 BYTES
-->SA2 IS 24 BYTES
EXPLANATION IS: IN 8 BYTE WORD FOR SA1 AT FIRST CHAR c is stored and the for stroing
int i[0] will start from from byte:4 (a multiple of 4) thus occuping one word
the i[1] is stroed in next 4 bytes of next word thus occupying the 2nd word
then double v will start from next word as needs to be stroed in 8 multiple address space

FOR SA2: AT FIRST WORD DOUBLE OCCYPIES THE 8 BYTES,THE INT IP[0] AND INT I[1] OCYPIES THE NEXT
COMPLETE WORD AND THEN CHAR C WILL OCUPPY 1 BYTE ,BUT COMPLIER WILL READ THE COMPLET WORDS AS REST
BYTES ARE ALL PADDED

in 32 BIT MACHINE : HAS 4 BYTE LONG WORD
--> SA1 IS :20 BYTES LONG
--> SA2 IS : 20 BYTES LONG
EXPLANATION IS : IN 4 BYTE WORD FOR SA1
CHAR C IS STROED IN FIRST WORD,FOR INT i[0] IS STROED IN NEXT WORD AS TO BE
STROED IN MULTIPLE OF 4 ,AND REST BYTES FOR FIRST WORD ARE PADDED,THEN I[1] IS STORED IN 3RD WORD
AND THEN DOUBLE VOCCUPIS THE NEXT BYTES OF NEXT TWO WORDS THUS OCCUPING THE 20 BYTES

FOR SA2: AT FIRST DOUBLE X OCUPPIES THE FIRST 2 WORDS,THEN INT I[0] AND I[1] OCUPPIES THE NEXT TWO
WORDS AND THE CHAR C OCCUPY FIRST BYTE OF 5TH WORD BUT REST ALL BYTES ARE PADDED,THUS OCCUPYING 20
BYTES AS A WHOLE
PR Attribute ID Marks New Marks
C_4 4 4
(9)
int main()
{
int i;
for (i = 0; i < 3; ++i)
{
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. ?
Page 8 of 12
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=504&s2=20429

OUTPUT IS : 11
12
13

EXPLANATION IS=> AS IN FOR LOOP
{
int t = 0 is defined with auto stroge type and made equals to 0 each time
of the for loop thus its value will persist in different for lopp execution
as variable i is define as static thus i's value will persist between different
for loop execution.
as *ptr is pointing to i theerfore i is increades by one by statemnet *ptr = *ptr + 1


for(i = 0;i < 3;++i) //i =0
{
-->prints : t as 1 and *ptr as 1
the again for( ) //--> i =1
-->prints : t as 0 and *ptr as 2 as t will be initalised again an again and i same as earlier call

then in next for( -->i = 2
-->prints : t as 0 and *ptr is 2
then in agian for loop i becomes 3 therfore exits
PR Attribute ID Marks New Marks
C_1 2 2
C_3 3 3
(10)
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;
}
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? ?
Page 9 of 12
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=504&s2=20429

in main:
A ARRAY IS INITALISED WITH 2 ROWS AND THREE COLUMNS
AT FIRST FOR LOOPS WE ACCESSING ARRAY ELEMENTS AND INITIALISING THEM WITH CTR VALUES INCREADES
EACH TIME

AND IN SECOND FOR LOOPS WE ARE DISPLAYING THEM BUT ACCESSING THEM BY AT FIRST THREE ROWS AND THEN
IN INNER LOOP TWO COLUMNS AR TRAVERSED

THIS PROGRAM WILLL BHEAVE NORMALLY AS MEMORY FOR ARRAY ELEMENT IS STORED IN CONTIGUOUS LOCATIONS
THEREORE EACH ELEMENT IS ACCESED IN THE SECOND FOR LOOPS ALSO AS
MYARRAY[0][0] -->MYARRAY[0][0]
MYARRAY[0][1] --. MYARRAY[0][1]
MYARRAY[0][2]--> MYARRAY[1][0]
MYARRAY[1][0] -->MYMARRAY[1][1]
MYARRAY[1][1] --> MYARRAY[2][0]
MYARRAY[1][2]-->MYARRAY[2][1]
ARE ACCESED BY THE SECOND FOR LOOPS


THEREFOIRE IS ONLY THAT ARRAY ELEMENTS ARE ACCESSED IN PROPER FASHION AS SHOULD BE
PR Attribute ID Marks New Marks
C_2 2 0
C_6 2 2
(11)
#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. ?
Page 10 of 12
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=504&s2=20429

AT FUNCTION1: *s1 ans *s2 and *s3 should be first given memory and then should be assigned
to a memory address location not the string,as only strcopy works for copying strings from one to
another,and only address or onter pointer can be assigned to another pointer
thus steps wise:
char *s1 = "my"; will give error
char *s2 = "program" //error
char *s3 = strcat(s1,s2);
will not print s3

at function2 : at first menmory is given to s1
then through strcpy "my" is copied to s1
the char *s2 will give error
cahr *s3 = strcat(s1,s2) //error as no s2
will not print s3


bugs here is : at first memory should be assigned to pointers and then strcoy is to be used to copy
strings or asigned to some other pinter
PR Attribute ID Marks New Marks
C_7 4 2
C_10 2 0
(12)
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. ?
Page 11 of 12
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=504&s2=20429





suppose the address of q is :0x200

therefore output is : 0x200 2 0x200 3 0x200
there will be no spaces in between


justification : q = 1
ptr = &q i.e 0x200 (suppose)
pptr = &ptr (i.e. *pptr = ox200)
therefore first printf prints--> ptr value 0x200
then *ptr++ i.e q= q+1 = 2
then next printf prints --. q as 2 and ptr as 0x200
**pptr++ i.e. q = q + 1 i.e q= 3
then next printf prints --> q as 3 and *pptr as 0x200
PR Attribute ID Marks New Marks
C_9 4 1
Page 12 of 12
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=504&s2=20429

Das könnte Ihnen auch gefallen