Saturday, 25 February 2012

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

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


1)

main()

{

int k=1;


printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE");


}

Answer:

1==1 is TRUE


Explanation:

When two strings are placed together (or separated by white-space) they are concatenated (this is called as "stringization" operation). So the string is as if it is given as "%d==1 is %s". The conditional operator( ?: ) evaluates to "TRUE".


2)

main()

{

int y;


scanf("%d",&y); // input given is 2000


if( (y%4==0 && y%100 != 0) || y%100 == 0 )


printf("%d is a leap year");


else


printf("%d is not a leap year");


}

Answer:

2000 is a leap year


Explanation:

An ordinary program to check if leap year or not.


3)

#define max 5

#define int arr1[max]

main()

{

typedef char arr2[max];


arr1 list={0,1,2,3,4};


arr2 name="name";


printf("%d %s",list[0],name);


}

Answer:

Compiler error (in the line arr1 list = {0,1,2,3,4})


Explanation:

arr2 is declared of type array of size 5 of characters. So it can be used to declare the variable name of the type arr2. But it is not the case of arr1. Hence an error.


Rule of Thumb:

#defines are used for textual replacement whereas typedefs are used for declaring new types.

No comments:

Post a Comment