Monday 2 January 2012

Frequently Asked Questions in Technical Round at MNCs like TCS, WIPRO, INFOSYS,..etc - 1

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


1)

main()

{

static char names[5][20]={"pascal","ada","cobol","fortran","perl"};


int i;


char *t;


t=names[3];


names[3]=names[4];


names[4]=t;


for (i=0;i<=4;i++)


printf("%s",names[i]);


}

Answer:

Compiler error: Lvalue required in function main


Explanation:

Array names are pointer constants. So it cannot be modified.


2)

void main()

{

int i=5;


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


}

Answer:

Output Cannot be predicted  exactly.


Explanation:

Side effects are involved in the evaluation of   i.


3)

void main()

{

int i=5;


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


}

Answer:

Compiler Error


Explanation:

The expression i+++++i is parsed as i ++ ++ + i which is an illegal combination of operators.


4)

#include<stdio.h>

main()

{

int i=1,j=2;


switch(i)


 {


 case 1:  printf("GOOD");


break;


case j:  printf("BAD");


break;


}


}

Answer:

Compiler Error: Constant expression required in function main.


Explanation:

The case statement can have only constant expressions (this implies that we cannot use variable names directly so an error).


 Note:

Enumerated types can be used in case statements.

No comments:

Post a Comment