C Interview Questions - Part IV

In this sample Interview Question series, let us focus on program - output type of questions that can be asked in C.

Part I, II and III were focusing on understanding the basics of the language. This part will help you in tackling questions during interview. You may get questions of similar kind during any IT interview. Such questions are likely to be asked either during the initial screening test or during a face to face interview. For the code snippets provided below, predict the output of the program. I strongly advise you to execute the code snippets and understand them completely. This will help you to answer the questions asked on similar lines during a real interview.

1)

void main()
{
int const * a=25;
printf("%d",++(*a));
}


2)

main()
{
char s2[ ]="man";
int i;
for(i=0;s2[ i ];i++)
printf("\n%c%c%c%c",s2[ i ],*(s2+i),*(i+s2),i[s2]);
}

3)

main()
{
float str1 = 1.1;
double str2 = 1.1;
if(str1==str2)
printf("C is a good language");
else
printf("C is a very good language");
}

4)

main()
{
static int var2 = 5;
printf("%d ",var2--);
if(var2)
main();
}

5)

main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j<5 br="" j="">printf(" %d ",*c);
++q; }
for(j=0;j<5 br="" j="">printf(" %d ",*p);
++p; }
}

6)

main()
{
int i=-1,j=-1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf("%d %d %d %d %d",i,j,k,l,m);
}

7)

main()
{
char *p;
printf("%d %d ",sizeof(*p),sizeof(p));
}

8)

main()
{
int i=3;
switch(i)
{
default:printf("zero");
case 1: printf("one");
break;
case 2:printf("two");
break;
case 3: printf("three");
break;
}
}

9)

main()
{
int i=50;
i=!i>14;
Printf ("i=%d",i);
}


10)

#include
main()
{
char s[]={'a','b','c','\n','c','\0'};
char *p,*str,*str1;
p=&s[3];
str=p;
str1=s;
printf("%d",++*p + ++*str1-32);
}

11)

#include
main()
{
int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };
int *p,*q;
p=&a[2][2][2];
*q=***a;
printf("%d----%d",*p,*q);
}

12)

#include
main()
{
struct xx
{
int x;
struct yy
{
char s;
struct xx *p;
};
struct yy *q;
};
}

13)

main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
}

14)

#define square(x) x*x
main()
{
int i;
i = 64/square(4);
printf("%d",i);
}

15)

main()
{
char *p="hai friends",*p1;
p1=p;
while(*p!='\0') ++*p++;
printf("%s %s",p,p1);
}

Answers to these questions with explanations will be given in subsequent postings. More to come shortly..

No comments:

Post a Comment