Thursday 12 January 2012

Frequently Asked Questions in Technical Round at MNCs like TCS, WIPRO, INFOSYS,..etc – 3

Predict the output or error(s) for the following:


1)

#include<stdio.h>

main()

{

struct xx


{


int x=3;


char name[]="hello";


};


struct xx *s=malloc(sizeof(struct xx));


printf("%d",s->x);


printf("%s",s->name);


}

Answer:

Compiler Error


Explanation:

Initialization should not be done for structure members inside the structure declaration


 

2)

#include<stdio.h>

main()

{

struct xx


{


int x;


struct yy


{


char s;


struct xx *p


};


struct yy *q;


};


}

Answer:

Compiler Error


Explanation:

In the end of nested structure yy a member have to be declared.


 

3)

main()

{

extern int i;


i=20;


printf("%d",sizeof(i));


}

Answer:

Linker error: undefined symbol '_i'.


Explanation:

extern declaration specifies that the variable i is defined somewhere else. The compiler passes the external variable to be resolved by the linker. So compiler doesn't find an error. During linking the linker searches for the definition of i. Since it is not found the linker flags an error.


 

4)

main()

{

printf("%d", out);


}

int out=100;

Answer:

Compiler error: undefined symbol out in function main.


Explanation:

The rule is that a variable is available for use from the point of declaration. Even though a is a global variable, it is not available for main. Hence an error.

No comments:

Post a Comment