Thursday 19 January 2012

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

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


1)

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);


}

Answer:

100, 100, 100, 2


114, 104, 102, 3


Explanation:

The given array is a 3-D one. It can also be viewed as a 1-D array.


thus, for the first printf statement a, *a, **a  give address of  first element . since the indirection ***a gives the value. Hence, the first line of the output.


for the second printf a+1 increases in the third dimension thus points to value at 114, *a+1 increments in second dimension thus points to 104, **a +1 increments the first dimension thus points to 102 and ***a+1 first gets the value at first location and then increments it by 1. Hence, the output.


2)

main()

{

 show();


}

void show()

{

 printf("I'm the greatest");


}

Answer:

Compier error: Type mismatch in redeclaration of show.


Explanation:

When the compiler sees the function show it doesn't know anything about it. So the default return type (ie, int) is assumed. But when compiler sees the actual definition of show mismatch occurs since it is declared as void. Hence the error.


The solutions are as follows:


1. declare void show() in main() .


2. define show() before main().


3. declare extern void show() before the use of show().


3)

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.


4)

main()

{

 extern out;


 printf("%d", out);


}

int out=100;

Answer:

100


Explanation:

This is the correct way of writing the previous program.

No comments:

Post a Comment