Monday, March 10, 2008

C C++ Interview Questions

1)
Find the bug in this code.
#include<stdio.h>
int main()
{
int i=3,j=5;
printf("%d",(i+j)++);
return(0);
}
Ans:



2)
What is the output of the following piece of code?
#include<stdio.h>
int main()
{
char c='8';
int d=8;
printf("%d %d %d",d,d+=c>='0'&&c<='9',c++);
return(0);
}
Ans:



3)
x=x*y+1; --------------1
Is this different from x=x*(y+1); --------------2

How to right the assignment operation "2" with out using
parenthesis in one statement.
Ans:




4)
What does this code do?
#include<stdio.h>
void func(char s[],int c)
{
int i,j;
for(i=j=0;s[i]!='\0';i++)
if(s[i]!=c)
s[j++]= s[i] + s[i]==c ;
s[j]='\0';
}

int main()
{
char s[]="aelloworld";
func(s,'k');
printf("%s",s);
return(0);
}
Ans:



5)
Can you find any difference between the two scanf calls made in the
code given below ?

#include<stdio.h>
int main()
{
int x,y;
scanf("%d",&x);
scanf(" %d",&y);
return 0;
}
Ans:




6)
Predict the output of the following piece of code.
Assume that bit position 0 is at the right end and that i and j are
sensible positive values.
#include<stdio.h>
unsigned getbits(unsigned a,int i,int j)
{
return(a>>(i+1-j)) & ~(~0<<j);
}
int main()
{
unsigned num=128;
printf("%d\n",getbits(num,7,5));
return(0);
}
Ans:



7)
What does this code do on input 'abcd' from console?
#include<stdio.h>
int main()
{
char c;
while(c=getchar()!='\n')
printf("%d",c);
return(0);
}
Ans:


8)
What does this code do?
#include<stdio.h>
int main()
{
int i;
int a[20]={1,2,3,4,5,6,7,8,9,10,10,9,8,7,6,1,2,3,4,5};
int n=20;
for(i=0;i<20;i++)
{
printf("%6d%c",a[i],(i%10==9 || i== 19)?'\n':' ');
}
return(0);
}
Ans:



9)
Will this code work properly? If yes why? If not why not?
#include<stdio.h>
void swap(int *a,int *b)
{
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}
int main()
{
int a=3617283450;
int b=3617283449;
swap(&a,&b);
printf("%d %d\n",a,b);
return(0);
}
Ans:



10)
What is the output of the following piece of code?
#include<stdio.h>
int main()
{
int a=3,b=1;
a = b<<a + b>>2;
b = b<<a + b>>2;
printf("%d %d",a,b);
return 0;
}
Ans:



11)
Can you predict the output of the following code snippet ?
#include<stdio.h>
int main()
{
int i=9876;


printf("%d\n",printf("%d",printf("%d",i)));


return 0;
}
Ans:




12)
Give the output of the following code.
#include<stdio.h>
typedef struct s
{
int a[10];
}s;
typedef struct t
{
int *a;
}t;

int main()
{
s s1,s2;t t1;
s1.a[0]=0;

t1.a=s1.a;
s1.a[0]++;
s2=s1;
s1.a[0]++;

printf("%d %d %d",s1.a[0],s2.a[0],t1.a[0]);
return 0;
}
Ans:



13)
You just have to write the declaration of an unsigned int p;
Obviously you can't use any spaces or new lines in the declaration !!
Ans:




14)
What is the output of the following code?
#include<stdio.h>
int main()
{
int x=2,y=3,z1,z2;
z1=x + + + y;
z2=x+++y;
printf("%d %d %d %d",x,y,z1,z2);
return 0;
}
Ans:



15)
Predict the output of the following code with the following inputs ->
1. 2c3
2. 2 5
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *i=0,*j=4;
i=(int*)malloc(sizeof(int));
scanf("%dc%d",&i,&j);
printf("%d %d",i,j);
return(0);
}
Ans: