Monday, March 10, 2008

Top C C++ Interview Question Papers

16)
What is the output of the following code for the input "dcba"?
#include<stdio.h>
#include<stdlib.h>
int main()
{
char c;
while (c=getchar()!='a') printf("%d",c);
return 0;
}
Ans:



17)
What is the output of the following code?
#include<stdio.h>
int array[]={1,2,3,4,5,6,7,8};
#define SIZE (sizeof(array)/sizeof(int))

int main()
{
printf("%d",SIZE);
if(-1<=SIZE) printf("1");
else printf("2");
return 0;
}
Ans:



18)
What is the output of the following code?
#include<stdio.h>
#include<string.h>
int main()
{
char a[]="aaa";
char *b="bbb";
strcpy(a,"cc");
printf("%s",a);
strcpy(b,"dd");
printf("%s",b);
return 0;
}
Ans:




19)
Can you predict the output of the following code snippet ?
#include<stdio.h>
#define sq(x) x*x
int main()
{
int a=3;
printf("%d\n",sq(a+3));
}
Ans:



20)
What is the output of this code?
#include<stdio.h>
int main()
{
int *i=0;
printf(" %p\n",i);
return(0);
}
Ans:




21)
Does the following C code compile ???? If it does .. can you
explain what happens when you run it with some arbitrary text
as input or what does it do ????
[ The input is redirected from a file called "input.txt" ]
int main()
{
for(;scanf("%*[^a-z]")+1;putchar(getchar()));
}
Ans:




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




23)
What is the expected output of the code ?????
#include<stdio.h>
#define concatinate(a,b) a##b
#define same1(a) #a
#define same2(a) same1(a)
int main()
{
printf("%s\n",same2(concatinate(1,2)));
printf("%s\n",same1(concatinate(1,2)));
return 0;
}
Ans:



24)
The intention of the following program was to print 42 astericks('*')
But it fails to do so.
You have to add/replace/delete exactly one character in the program
to make it work ???
Find as many solutions as possible.

#include <stdio.h>
int main()
{
int n = 42;
for(int i = 0; i < n; i-- )
printf("*");
return 0;
}
Ans:























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

int main()
{
char s[]="helloworld";
insert(s,'l');
printf("%s",s);
return(0);
}
Ans:




26)
What is the output of the following code?
#include<stdio.h>
#include<string.h>
int s(char *A[20],char *B[20])
{
char *a,*b;
a=A;b=B;
while(*a++!=*b++); *a=*b='\0';
return strlen(A);
}
int main()
{
char A[20]="somestring",
B[20]="debugthecbug";
printf("%d %s %s\n",s(&A,&B),A,B);
return 0;
}
Ans:




27)
Can you predict the output ?
#include<stdio.h>
#define print(var) printf("%s : %d\n",#var,(var))
int main()
{
int y = 100;
int *p;
p = new int;
*p = 10;
y = y/*p; /*dividing y by *p */;
print(y);
return 0;
}
Ans:



28)
Take the input as 4, and write the output of the following code.
#include <stdio.h>
main()
{
int i=0;
printf("%d %d\n",scanf(" %d",&i),i);
printf("%d %d\n",i=4,i);
}
Ans:



29)
Predict the output of the following program.
#include <stdio.h>
int main()
{


int a=3, b = 5;
printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
printf(&a["WHAT%c%c%c\n%c%c\n%c !\n"], 1["this"],
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);


return 0;
}
Ans:





30)
What is the output of the following code?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr=(int*)malloc(sizeof(int));
*ptr=4;
printf("%d",(*ptr)+++*ptr++);
return(0);
}
Ans:




31)
What is the output of the following piece of code?
#include<stdio.h>
int main(int k)
{
if(k<10)
printf("%d ",main(k+1));
return k;
}
Ans:





32)
Predict the output or error if any in the following code.
#include<stdio.h>
main()
{
int i = 4;
printf("%d %d %d",++i,i++,i*i);
printf("\n");
printf("%d %d %d",i*i,++i,i++);
system("PAUSE");
}
Ans:



33)
Give the output of the following piece of code.
#include<stdio.h>
int main()
{
int a,b,c=2,d=10;


printf("%d\n ",scanf("%d%d%d",&a,&b,&c,&d) ) ;


printf("%d\n%d\n%d\n%d\n",a,b,c);
return(0);
}
Ans:



34)
#include <stdio.h>
main()
{
int a[15][10][5]={1};
func(a);
}
Which of these is/are correct function prototypes for func ?

void func( int a[][10][5] );
void func( int a[15][][5] );
void func( int a[15][10][] );
Ans:





35)
Give the output of the following piece of code.
#include <stdio.h>
main()
{
float x=3.14;
printf("%e, %E, %f",x,x,x);
}
Ans: