C++ Interview Questions
Questions Answers Views Company eMail

c++ provides classes...and classes do what we want but why then strcut are used...if we say data hiding... it is also provided by c++ in structs then why to prefer clasess

HCL, TCS,

1 3925

Is this job good for future? can do this job post grduate student?

1683

totoo po ba ang manga aliens!

2325

When a function is made inline. Write the situation where inline functions may not work.

2 5337

When we use Abstract Class and when we use Interface?where we will implement in real time?

1660

define oops concept with example

Cap Gemini,

1 2546

Can anyone please explain runtime polymorphism with a real time example??at what ciscumstances we go for it??

1 4001

What Is a Polymorphism? How many types of polymorphism and whats that use in application?

2 3630

let a,b,c be three integer numbers.write a c++ program with a function void rotate 1()such that a->b->c and c->a.

1 4083

write a porgram in c++ that reads an integer and print the biggest digit in the number

1759

declare an array of structure where the members of the structure are integer variable float variable integer array char variable access all elements of the structure using dot operator and this pointer operator

1737

can inline function declare in private part of class?

1 3649

any one please tell me the purpose of operator overloading

Amazon,

1959

write a program to search and display the position of an element in a single-dimentional array using function.

1 3064

C Questions Note : All the programs are tested under Turbo C/C++ compilers. It is assumed that,  Programs run under DOS environment,  The underlying machine is an x86 system,  Program is compiled using Turbo C/C++ compiler. The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed). Predict the output or error(s) for the following: 1. void main() { int const * p=5; printf("%d",++(*p)); } 2. main() { char s[ ]="man"; int i; for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); } 3. main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); } 4. main() { static int var = 5; printf("%d ",var--); if(var) main(); } 5. main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); ++q; } for(j=0;j<5;j++){ printf(" %d ",*p); ++p; } } 6. main() { extern int i; i=20; printf("%d",i); } 7. 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); } 8. main() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); } 9. main() { int i=3; switch(i) { default:printf("zero"); case 1: printf("one"); break; case 2:printf("two"); break; case 3: printf("three"); break; } } 10. main() { printf("%x",-1<<4); } 11. main() { char string[]="Hello World"; display(string); } void display(char *string) { printf("%s",string); } 12. main() { int c=- -2; printf("c=%d",c); } 13. #define int char main() { int i=65; printf("sizeof(i)=%d",sizeof(i)); } 14. main() { int i=10; i=!i>14; Printf ("i=%d",i); } 15. #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); } 16. #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); } 17. #include main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s; printf("%d",s->x); printf("%s",s->name); } 18. #include main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; } 19. main() { printf("\nab"); printf("\bsi"); printf("\rha"); } 20. main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); } 21. #define square(x) x*x main() { int i; i = 64/square(4); printf("%d",i); } 22. main() { char *p="hai friends",*p1; p1=p; while(*p!='\0') ++*p++; printf("%s %s",p,p1); } 23. #include #define a 10 main() { #define a 50 printf("%d",a); } 24. #define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); } 25. main() { printf("%p",main); } 27) main() { clrscr(); } clrscr(); 28) enum colors {BLACK,BLUE,GREEN} main() { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); } 29) void main() { char far *farther,*farthest; printf("%d..%d",sizeof(farther),sizeof(farthest)); } 30) main() { int i=400,j=300; printf("%d..%d"); } 31) main() { char *p; p="Hello"; printf("%c\n",*&*p); } 32) main() { int i=1; while (i<=5) { printf("%d",i); if (i>2) goto here; i++; } } fun() { here: printf("PP"); } 33) 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]); } 34) void main() { int i=5; printf("%d",i++ + ++i); } 35) void main() { int i=5; printf("%d",i+++++i); } 36) #include main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } } 37) main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here } 38) #define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); } 39) main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); } 40) #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); } 41) #include main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s=malloc(sizeof(struct xx)); printf("%d",s->x); printf("%s",s->name); } 42) #include main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; } 43) main() { extern int i; i=20; printf("%d",sizeof(i)); } 44) main() { printf("%d", out); } int out=100; 45) main() { extern out; printf("%d", out); } int out=100; 46) main() { show(); } void show() { printf("I'm the greatest"); } 47) main( ) { int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}}; printf(“%u %u %u %d \n”,a,*a,**a,***a); printf(“%u %u %u %d \n”,a+1,*a+1,**a+1,***a+1); } 48) main( ) { int a[ ] = {10,20,30,40,50},j,*p; for(j=0; j<5; j++) { printf(“%d” ,*a); a++; } p = a; for(j=0; j<5; j++) { printf(“%d ” ,*p); p++; } } 49) main( ) { static int a[ ] = {0,1,2,3,4}; int *p[ ] = {a,a+1,a+2,a+3,a+4}; int **ptr = p; ptr++; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); *ptr++; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); *++ptr; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); ++*ptr; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); } 50) main( ) { char *q; int j; for (j=0; j<3; j++) scanf(“%s” ,(q+j)); for (j=0; j<3; j++) printf(“%c” ,*(q+j)); for (j=0; j<3; j++) printf(“%s” ,(q+j)); } 51) main( ) { void *vp; char ch = ‘g’, *cp = “goofy”; int j = 20; vp = &ch; printf(“%c”, *(char *)vp); vp = &j; printf(“%d”,*(int *)vp); vp = cp; printf(“%s”,(char *)vp + 3); } 52) main ( ) { static char *s[ ] = {“black”, “white”, “yellow”, “violet”}; char **ptr[ ] = {s+3, s+2, s+1, s}, ***p; p = ptr; **++p; printf(“%s”,*--*++p + 3); } 53) main() { int i, n; char *x = “girl”; n = strlen(x); *x = x[n]; for(i=0; i main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); } 72) #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); } 73) #include main() { register i=5; char j[]= "hello"; printf("%s %d",j,i); } 74) main() { int i=5,j=6,z; printf("%d",i+++j); } 76) struct aaa{ struct aaa *prev; int i; struct aaa *next; }; main() { struct aaa abc,def,ghi,jkl; int x=100; abc.i=0;abc.prev=&jkl; abc.next=&def; def.i=1;def.prev=&abc;def.next=&ghi; ghi.i=2;ghi.prev=&def; ghi.next=&jkl; jkl.i=3;jkl.prev=&ghi;jkl.next=&abc; x=abc.next->next->prev->next->i; printf("%d",x); } 77) struct point { int x; int y; }; struct point origin,*pp; main() { pp=&origin; printf("origin is(%d%d)\n",(*pp).x,(*pp).y); printf("origin is (%d%d)\n",pp->x,pp->y); } 78) main() { int i=_l_abc(10); printf("%d\n",--i); } int _l_abc(int i) { return(i++); } 79) main() { char *p; int *q; long *r; p=q=r=0; p++; q++; r++; printf("%p...%p...%p",p,q,r); } 80) main() { char c=' ',x,convert(z); getc(c); if((c>='a') && (c<='z')) x=convert(c); printf("%c",x); } convert(z) { return z-32; } 81) main(int argc, char **argv) { printf("enter the character"); getchar(); sum(argv[1],argv[2]); } sum(num1,num2) int num1,num2; { return num1+num2; } 82) # include int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); } 83) # include aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){ printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[2](); } 85) #include main() { FILE *ptr; char i; ptr=fopen("zzz.c","r"); while((i=fgetch(ptr))!=EOF) printf("%c",i); } 86) main() { int i =0;j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); } 87) main() { int i; i = abc(); printf("%d",i); } abc() { _AX = 1000; } 88) int i; main(){ int t; for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) printf("%d--",t--); } // If the inputs are 0,1,2,3 find the o/p 89) main(){ int a= 0;int b = 20;char x =1;char y =10; if(a,b,x,y) printf("hello"); } 90) main(){ unsigned int i; for(i=1;i>-2;i--) printf("c aptitude"); } 91) In the following pgm add a stmt in the function fun such that the address of 'a' gets stored in 'j'. main(){ int * j; void fun(int **); fun(&j); } void fun(int **k) { int a =0; /* add a stmt here*/ } 92) What are the following notations of defining functions known as? i. int abc(int a,float b) { /* some code */ } ii. int abc(a,b) int a; float b; { /* some code*/ } 93) main() { char *p; p="%d\n"; p++; p++; printf(p-2,300); } 94) main(){ char a[100]; a[0]='a';a[1]]='b';a[2]='c';a[4]='d'; abc(a); } abc(char a[]){ a++; printf("%c",*a); a++; printf("%c",*a); } 95) func(a,b) int a,b; { return( a= (a==b) ); } main() { int process(),func(); printf("The value of process is %d !\n ",process(func,3,6)); } process(pf,val1,val2) int (*pf) (); int val1,val2; { return((*pf) (val1,val2)); } 96) void main() { static int i=5; if(--i){ main(); printf("%d ",i); } } 97) void main() { int k=ret(sizeof(float)); printf("\n here value is %d",++k); } int ret(int ret) { ret += 2.5; return(ret); } 98) void main() { char a[]="12345\0"; int i=strlen(a); printf("here in 3 %d\n",++i); } 99) void main() { unsigned giveit=-1; int gotit; printf("%u ",++giveit); printf("%u \n",gotit=--giveit); } 100) void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); } C,C++ Questions 1. Base class has some virtual method and derived class has a method with the same name. If we initialize the base class pointer with derived object,. calling of that virtual method will result in which method being called? a. Base method b. Derived method.. 2. For the following C program #define AREA(x)(3.14*x*x) main() {float r1=6.25,r2=2.5,a; a=AREA(r1); printf("\n Area of the circle is %f", a); a=AREA(r2); printf("\n Area of the circle is %f", a); } What is the output? 3. What do the following statements indicate. Explain. • int(*p)[10] • int*f() • int(*pf)() • int*p[10] 4. void main() { int d=5; printf("%f",d); } 5. void main() { int i; for(i=1;i<4,i++) switch(i) case 1: printf("%d",i);break; { case 2:printf("%d",i);break; case 3:printf("%d",i);break; } switch(i) case 4:printf("%d",i); } 6. void main() { char *s="\12345s\n"; printf("%d",sizeof(s)); } 7. void main() { unsigned i=1; /* unsigned char k= -1 => k=255; */ signed j=-1; /* char k= -1 => k=65535 */ /* unsigned or signed int k= -1 =>k=65535 */ if(ij) printf("greater"); else if(i==j) printf("equal"); } 8. void main() { float j; j=1000*1000; printf("%f",j); } 1. 1000000 2. Overflow 3. Error 4. None 9. How do you declare an array of N pointers to functions returning pointers to functions returning pointers to characters? 10. A structure pointer is defined of the type time . With 3 fields min,sec hours having pointers to intergers. Write the way to initialize the 2nd element to 10. 11. In the above question an array of pointers is declared. Write the statement to initialize the 3rd element of the 2 element to 10; 12. int f() void main() { f(1); f(1,2); f(1,2,3); } f(int i,int j,int k) { printf("%d %d %d",i,j,k); } What are the number of syntax errors in the above? 13. void main() { int i=7; printf("%d",i++*i++); } 14. #define one 0 #ifdef one printf("one is defined "); #ifndef one printf("one is not defined "); 15. void main() { int count=10,*temp,sum=0; temp=&count; *temp=20; temp=∑ *temp=count; printf("%d %d %d ",count,*temp,sum); } 16. There was question in c working only on unix machine with pattern matching. 14. what is alloca() 17. main() { static i=3; printf("%d",i--); return i>0 ? main():0; } 18. char *foo() { char result[100]); strcpy(result,"anything is good"); return(result); } void main() { char *j; j=foo() printf("%s",j); } 19. void main() { char *s[]={ "dharma","hewlett-packard","siemens","ibm"}; char **p; p=s; printf("%s",++*p); printf("%s",*p++); printf("%s",++*p); } 20. Output of the following program is main() {int i=0; for(i=0;i<20;i++) {switch(i) case 0:i+=5; case 1:i+=2; case 5:i+=5; default i+=4; break;} printf("%d,",i); } } a) 0,5,9,13,17 b) 5,9,13,17 c) 12,17,22 d) 16,21 e) Syntax error 21. What is the ouptut in the following program main() {char c=-64; int i=-32 unsigned int u =-16; if(c>i) {printf("pass1,"); if(c>2); } 28 Find the output for the following C program #define swap1(a,b) a=a+b;b=a-b;a=a-b; main() { int x=5,y=10; swap1(x,y); printf("%d %d\n",x,y); swap2(x,y); printf("%d %d\n",x,y); } int swap2(int a,int b) { int temp; temp=a; b=a; a=temp; return; } 29 Find the output for the following C program main() { char *ptr = "Ramco Systems"; (*ptr)++; printf("%s\n",ptr); ptr++; printf("%s\n",ptr); } 30 Find the output for the following C program #include main() { char s1[]="Ramco"; char s2[]="Systems"; s1=s2; printf("%s",s1); } 31 Find the output for the following C program #include main() { char *p1; char *p2; p1=(char *) malloc(25); p2=(char *) malloc(25); strcpy(p1,"Ramco"); strcpy(p2,"Systems"); strcat(p1,p2); printf("%s",p1); } 32. Find the output for the following C program given that [1]. The following variable is available in file1.c static int average_float; 33. Find the output for the following C program # define TRUE 0 some code while(TRUE) { some code } 34. struct list{ int x; struct list *next; }*head; the struct head.x =100 Is the above assignment to pointer is correct or wrong ? 35.What is the output of the following ? int i; i=1; i=i+2*i++; printf(%d,i); 36. FILE *fp1,*fp2; fp1=fopen("one","w") fp2=fopen("one","w") fputc('A',fp1) fputc('B',fp2) fclose(fp1) fclose(fp2) } Find the Error, If Any? 37. What are the output(s) for the following ? 38. #include char *f() {char *s=malloc(8); strcpy(s,"goodbye"); } main() { char *f(); printf("%c",*f()='A'); } 39. #define MAN(x,y) (x)>(y)?(x):(y) {int i=10; j=5; k=0; k=MAX(i++,++j); printf(%d %d %d %d,i,j,k); } 40. void main() { int i=7; printf("%d",i++*i++); } C,C++ Questions 1. Base class has some virtual method and derived class has a method with the same name. If we initialize the base class pointer with derived object,. calling of that virtual method will result in which method being called? a. Base method b. Derived method.. 2. For the following C program #define AREA(x)(3.14*x*x) main() {float r1=6.25,r2=2.5,a; a=AREA(r1); printf("\n Area of the circle is %f", a); a=AREA(r2); printf("\n Area of the circle is %f", a); } What is the output? 3. What do the following statements indicate. Explain. • int(*p)[10] • int*f() • int(*pf)() • int*p[10] 4. void main() { int d=5; printf("%f",d); } 5. void main() { int i; for(i=1;i<4,i++) switch(i) case 1: printf("%d",i);break; { case 2:printf("%d",i);break; case 3:printf("%d",i);break; } switch(i) case 4:printf("%d",i); } 6. void main() { char *s="\12345s\n"; printf("%d",sizeof(s)); } 7. void main() { unsigned i=1; /* unsigned char k= -1 => k=255; */ signed j=-1; /* char k= -1 => k=65535 */ /* unsigned or signed int k= -1 =>k=65535 */ if(ij) printf("greater"); else if(i==j) printf("equal"); } 8. void main() { float j; j=1000*1000; printf("%f",j); } 1. 1000000 2. Overflow 3. Error 4. None 9. How do you declare an array of N pointers to functions returning pointers to functions returning pointers to characters? 10. A structure pointer is defined of the type time . With 3 fields min,sec hours having pointers to intergers. Write the way to initialize the 2nd element to 10. 11. In the above question an array of pointers is declared. Write the statement to initialize the 3rd element of the 2 element to 10; 12. int f() void main() { f(1); f(1,2); f(1,2,3); } f(int i,int j,int k) { printf("%d %d %d",i,j,k); } What are the number of syntax errors in the above? 13. void main() { int i=7; printf("%d",i++*i++); } 14. #define one 0 #ifdef one printf("one is defined "); #ifndef one printf("one is not defined "); 15. void main() { int count=10,*temp,sum=0; temp=&count; *temp=20; temp=∑ *temp=count; printf("%d %d %d ",count,*temp,sum); } 16. There was question in c working only on unix machine with pattern matching. 14. what is alloca() 17. main() { static i=3; printf("%d",i--); return i>0 ? main():0; } 18. char *foo() { char result[100]); strcpy(result,"anything is good"); return(result); } void main() { char *j; j=foo() printf("%s",j); } 19. void main() { char *s[]={ "dharma","hewlett-packard","siemens","ibm"}; char **p; p=s; printf("%s",++*p); printf("%s",*p++); printf("%s",++*p); } 20. Output of the following program is main() {int i=0; for(i=0;i<20;i++) {switch(i) case 0:i+=5; case 1:i+=2; case 5:i+=5; default i+=4; break;} printf("%d,",i); } } a) 0,5,9,13,17 b) 5,9,13,17 c) 12,17,22 d) 16,21 e) Syntax error 21. What is the ouptut in the following program main() {char c=-64; int i=-32 unsigned int u =-16; if(c>i) {printf("pass1,"); if(c>2); } 28 Find the output for the following C program #define swap1(a,b) a=a+b;b=a-b;a=a-b; main() { int x=5,y=10; swap1(x,y); printf("%d %d\n",x,y); swap2(x,y); printf("%d %d\n",x,y); } int swap2(int a,int b) { int temp; temp=a; b=a; a=temp; return; } 29 Find the output for the following C program main() { char *ptr = "Ramco Systems"; (*ptr)++; printf("%s\n",ptr); ptr++; printf("%s\n",ptr); } 30 Find the output for the following C program #include main() { char s1[]="Ramco"; char s2[]="Systems"; s1=s2; printf("%s",s1); } 31 Find the output for the following C program #include main() { char *p1; char *p2; p1=(char *) malloc(25); p2=(char *) malloc(25); strcpy(p1,"Ramco"); strcpy(p2,"Systems"); strcat(p1,p2); printf("%s",p1); } 32. Find the output for the following C program given that [1]. The following variable is available in file1.c static int average_float; 33. Find the output for the following C program # define TRUE 0 some code while(TRUE) { some code } 34. struct list{ int x; struct list *next; }*head; the struct head.x =100 Is the above assignment to pointer is correct or wrong ? 35.What is the output of the following ? int i; i=1; i=i+2*i++; printf(%d,i); 36. FILE *fp1,*fp2; fp1=fopen("one","w") fp2=fopen("one","w") fputc('A',fp1) fputc('B',fp2) fclose(fp1) fclose(fp2) } Find the Error, If Any? 37. What are the output(s) for the following ? 38. #include char *f() {char *s=malloc(8); strcpy(s,"goodbye"); } main() { char *f(); printf("%c",*f()='A'); } 39. #define MAN(x,y) (x)>(y)?(x):(y) {int i=10; j=5; k=0; k=MAX(i++,++j); printf(%d %d %d %d,i,j,k); } 40. void main() { int i=7; printf("%d",i++*i++); } C Questions Note : All the programs are tested under Turbo C/C++ compilers. It is assumed that,  Programs run under DOS environment,  The underlying machine is an x86 system,  Program is compiled using Turbo C/C++ compiler. The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed). Predict the output or error(s) for the following: 1. void main() { int const * p=5; printf("%d",++(*p)); } 2. main() { char s[ ]="man"; int i; for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); } 3. main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); } 4. main() { static int var = 5; printf("%d ",var--); if(var) main(); } 5. main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); ++q; } for(j=0;j<5;j++){ printf(" %d ",*p); ++p; } } 6. main() { extern int i; i=20; printf("%d",i); } 7. 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); } 8. main() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); } 9. main() { int i=3; switch(i) { default:printf("zero"); case 1: printf("one"); break; case 2:printf("two"); break; case 3: printf("three"); break; } } 10. main() { printf("%x",-1<<4); } 11. main() { char string[]="Hello World"; display(string); } void display(char *string) { printf("%s",string); } 12. main() { int c=- -2; printf("c=%d",c); } 13. #define int char main() { int i=65; printf("sizeof(i)=%d",sizeof(i)); } 14. main() { int i=10; i=!i>14; Printf ("i=%d",i); } 15. #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); } 16. #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); } 17. #include main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s; printf("%d",s->x); printf("%s",s->name); } 18. #include main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; } 19. main() { printf("\nab"); printf("\bsi"); printf("\rha"); } 20. main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); } 21. #define square(x) x*x main() { int i; i = 64/square(4); printf("%d",i); } 22. main() { char *p="hai friends",*p1; p1=p; while(*p!='\0') ++*p++; printf("%s %s",p,p1); } 23. #include #define a 10 main() { #define a 50 printf("%d",a); } 24. #define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); } 25. main() { printf("%p",main); } 27) main() { clrscr(); } clrscr(); 28) enum colors {BLACK,BLUE,GREEN} main() { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); } 29) void main() { char far *farther,*farthest; printf("%d..%d",sizeof(farther),sizeof(farthest)); } 30) main() { int i=400,j=300; printf("%d..%d"); } 31) main() { char *p; p="Hello"; printf("%c\n",*&*p); } 32) main() { int i=1; while (i<=5) { printf("%d",i); if (i>2) goto here; i++; } } fun() { here: printf("PP"); } 33) 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]); } 34) void main() { int i=5; printf("%d",i++ + ++i); } 35) void main() { int i=5; printf("%d",i+++++i); } 36) #include main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } } 37) main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here } 38) #define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); } 39) main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); } 40) #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); } 41) #include main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s=malloc(sizeof(struct xx)); printf("%d",s->x); printf("%s",s->name); } 42) #include main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; } 43) main() { extern int i; i=20; printf("%d",sizeof(i)); } 44) main() { printf("%d", out); } int out=100; 45) main() { extern out; printf("%d", out); } int out=100; 46) main() { show(); } void show() { printf("I'm the greatest"); } 47) main( ) { int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}}; printf(“%u %u %u %d \n”,a,*a,**a,***a); printf(“%u %u %u %d \n”,a+1,*a+1,**a+1,***a+1); } 48) main( ) { int a[ ] = {10,20,30,40,50},j,*p; for(j=0; j<5; j++) { printf(“%d” ,*a); a++; } p = a; for(j=0; j<5; j++) { printf(“%d ” ,*p); p++; } } 49) main( ) { static int a[ ] = {0,1,2,3,4}; int *p[ ] = {a,a+1,a+2,a+3,a+4}; int **ptr = p; ptr++; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); *ptr++; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); *++ptr; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); ++*ptr; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); } 50) main( ) { char *q; int j; for (j=0; j<3; j++) scanf(“%s” ,(q+j)); for (j=0; j<3; j++) printf(“%c” ,*(q+j)); for (j=0; j<3; j++) printf(“%s” ,(q+j)); } 51) main( ) { void *vp; char ch = ‘g’, *cp = “goofy”; int j = 20; vp = &ch; printf(“%c”, *(char *)vp); vp = &j; printf(“%d”,*(int *)vp); vp = cp; printf(“%s”,(char *)vp + 3); } 52) main ( ) { static char *s[ ] = {“black”, “white”, “yellow”, “violet”}; char **ptr[ ] = {s+3, s+2, s+1, s}, ***p; p = ptr; **++p; printf(“%s”,*--*++p + 3); } 53) main() { int i, n; char *x = “girl”; n = strlen(x); *x = x[n]; for(i=0; i main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); } 72) #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); } 73) #include main() { register i=5; char j[]= "hello"; printf("%s %d",j,i); } 74) main() { int i=5,j=6,z; printf("%d",i+++j); } 76) struct aaa{ struct aaa *prev; int i; struct aaa *next; }; main() { struct aaa abc,def,ghi,jkl; int x=100; abc.i=0;abc.prev=&jkl; abc.next=&def; def.i=1;def.prev=&abc;def.next=&ghi; ghi.i=2;ghi.prev=&def; ghi.next=&jkl; jkl.i=3;jkl.prev=&ghi;jkl.next=&abc; x=abc.next->next->prev->next->i; printf("%d",x); } 77) struct point { int x; int y; }; struct point origin,*pp; main() { pp=&origin; printf("origin is(%d%d)\n",(*pp).x,(*pp).y); printf("origin is (%d%d)\n",pp->x,pp->y); } 78) main() { int i=_l_abc(10); printf("%d\n",--i); } int _l_abc(int i) { return(i++); } 79) main() { char *p; int *q; long *r; p=q=r=0; p++; q++; r++; printf("%p...%p...%p",p,q,r); } 80) main() { char c=' ',x,convert(z); getc(c); if((c>='a') && (c<='z')) x=convert(c); printf("%c",x); } convert(z) { return z-32; } 81) main(int argc, char **argv) { printf("enter the character"); getchar(); sum(argv[1],argv[2]); } sum(num1,num2) int num1,num2; { return num1+num2; } 82) # include int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); } 83) # include aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){ printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[2](); } 85) #include main() { FILE *ptr; char i; ptr=fopen("zzz.c","r"); while((i=fgetch(ptr))!=EOF) printf("%c",i); } 86) main() { int i =0;j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); } 87) main() { int i; i = abc(); printf("%d",i); } abc() { _AX = 1000; } 88) int i; main(){ int t; for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) printf("%d--",t--); } // If the inputs are 0,1,2,3 find the o/p 89) main(){ int a= 0;int b = 20;char x =1;char y =10; if(a,b,x,y) printf("hello"); } 90) main(){ unsigned int i; for(i=1;i>-2;i--) printf("c aptitude"); } 91) In the following pgm add a stmt in the function fun such that the address of 'a' gets stored in 'j'. main(){ int * j; void fun(int **); fun(&j); } void fun(int **k) { int a =0; /* add a stmt here*/ } 92) What are the following notations of defining functions known as? i. int abc(int a,float b) { /* some code */ } ii. int abc(a,b) int a; float b; { /* some code*/ } 93) main() { char *p; p="%d\n"; p++; p++; printf(p-2,300); } 94) main(){ char a[100]; a[0]='a';a[1]]='b';a[2]='c';a[4]='d'; abc(a); } abc(char a[]){ a++; printf("%c",*a); a++; printf("%c",*a); } 95) func(a,b) int a,b; { return( a= (a==b) ); } main() { int process(),func(); printf("The value of process is %d !\n ",process(func,3,6)); } process(pf,val1,val2) int (*pf) (); int val1,val2; { return((*pf) (val1,val2)); } 96) void main() { static int i=5; if(--i){ main(); printf("%d ",i); } } 97) void main() { int k=ret(sizeof(float)); printf("\n here value is %d",++k); } int ret(int ret) { ret += 2.5; return(ret); } 98) void main() { char a[]="12345\0"; int i=strlen(a); printf("here in 3 %d\n",++i); } 99) void main() { unsigned giveit=-1; int gotit; printf("%u ",++giveit); printf("%u \n",gotit=--giveit); } 100) void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); } C,C++ Questions 1. Base class has some virtual method and derived class has a method with the same name. If we initialize the base class pointer with derived object,. calling of that virtual method will result in which method being called? a. Base method b. Derived method.. 2. For the following C program #define AREA(x)(3.14*x*x) main() {float r1=6.25,r2=2.5,a; a=AREA(r1); printf("\n Area of the circle is %f", a); a=AREA(r2); printf("\n Area of the circle is %f", a); } What is the output? 3. What do the following statements indicate. Explain. • int(*p)[10] • int*f() • int(*pf)() • int*p[10] 4. void main() { int d=5; printf("%f",d); } 5. void main() { int i; for(i=1;i<4,i++) switch(i) case 1: printf("%d",i);break; { case 2:printf("%d",i);break; case 3:printf("%d",i);break; } switch(i) case 4:printf("%d",i); } 6. void main() { char *s="\12345s\n"; printf("%d",sizeof(s)); } 7. void main() { unsigned i=1; /* unsigned char k= -1 => k=255; */ signed j=-1; /* char k= -1 => k=65535 */ /* unsigned or signed int k= -1 =>k=65535 */ if(ij) printf("greater"); else if(i==j) printf("equal"); } 8. void main() { float j; j=1000*1000; printf("%f",j); } 1. 1000000 2. Overflow 3. Error 4. None 9. How do you declare an array of N pointers to functions returning pointers to functions returning pointers to characters? 10. A structure pointer is defined of the type time . With 3 fields min,sec hours having pointers to intergers. Write the way to initialize the 2nd element to 10. 11. In the above question an array of pointers is declared. Write the statement to initialize the 3rd element of the 2 element to 10; 12. int f() void main() { f(1); f(1,2); f(1,2,3); } f(int i,int j,int k) { printf("%d %d %d",i,j,k); } What are the number of syntax errors in the above? 13. void main() { int i=7; printf("%d",i++*i++); } 14. #define one 0 #ifdef one printf("one is defined "); #ifndef one printf("one is not defined "); 15. void main() { int count=10,*temp,sum=0; temp=&count; *temp=20; temp=∑ *temp=count; printf("%d %d %d ",count,*temp,sum); } 16. There was question in c working only on unix machine with pattern matching. 14. what is alloca() 17. main() { static i=3; printf("%d",i--); return i>0 ? main():0; } 18. char *foo() { char result[100]); strcpy(result,"anything is good"); return(result); } void main() { char *j; j=foo() printf("%s",j); } 19. void main() { char *s[]={ "dharma","hewlett-packard","siemens","ibm"}; char **p; p=s; printf("%s",++*p); printf("%s",*p++); printf("%s",++*p); } 20. Output of the following program is main() {int i=0; for(i=0;i<20;i++) {switch(i) case 0:i+=5; case 1:i+=2; case 5:i+=5; default i+=4; break;} printf("%d,",i); } } a) 0,5,9,13,17 b) 5,9,13,17 c) 12,17,22 d) 16,21 e) Syntax error 21. What is the ouptut in the following program main() {char c=-64; int i=-32 unsigned int u =-16; if(c>i) {printf("pass1,"); if(c>2); } 28 Find the output for the following C program #define swap1(a,b) a=a+b;b=a-b;a=a-b; main() { int x=5,y=10; swap1(x,y); printf("%d %d\n",x,y); swap2(x,y); printf("%d %d\n",x,y); } int swap2(int a,int b) { int temp; temp=a; b=a; a=temp; return; } 29 Find the output for the following C program main() { char *ptr = "Ramco Systems"; (*ptr)++; printf("%s\n",ptr); ptr++; printf("%s\n",ptr); } 30 Find the output for the following C program #include main() { char s1[]="Ramco"; char s2[]="Systems"; s1=s2; printf("%s",s1); } 31 Find the output for the following C program #include main() { char *p1; char *p2; p1=(char *) malloc(25); p2=(char *) malloc(25); strcpy(p1,"Ramco"); strcpy(p2,"Systems"); strcat(p1,p2); printf("%s",p1); } 32. Find the output for the following C program given that [1]. The following variable is available in file1.c static int average_float; 33. Find the output for the following C program # define TRUE 0 some code while(TRUE) { some code } 34. struct list{ int x; struct list *next; }*head; the struct head.x =100 Is the above assignment to pointer is correct or wrong ? 35.What is the output of the following ? int i; i=1; i=i+2*i++; printf(%d,i); 36. FILE *fp1,*fp2; fp1=fopen("one","w") fp2=fopen("one","w") fputc('A',fp1) fputc('B',fp2) fclose(fp1) fclose(fp2) } Find the Error, If Any? 37. What are the output(s) for the following ? 38. #include char *f() {char *s=malloc(8); strcpy(s,"goodbye"); } main() { char *f(); printf("%c",*f()='A'); } 39. #define MAN(x,y) (x)>(y)?(x):(y) {int i=10; j=5; k=0; k=MAX(i++,++j); printf(%d %d %d %d,i,j,k); } 40. void main() { int i=7; printf("%d",i++*i++); } C QUESTIONS How do you decide which integer type to use? What should the 64-bit type on a machine that can support it? What's the best way to declare and define global variables and functions? What does extern mean in a function declaration? What's the auto keyword good for? I can't seem to define a linked list successfully. I tried typedef struct { char *item; NODEPTR next; } *NODEPTR; but the compiler gave me error messages. Can't a structure in C contain a pointer to itself? How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters? How can I declare a function that can return a pointer to a function of the same type? What's the right declaration for main()? Is void main() correct? What am I allowed to assume about the initial values of variables which are not explicitly initialized? If global variables start out as "zero", is that good enough for null pointers and floating-point zeroes? This code, straight out of a book, isn't compiling: int f() { char a[] = "Hello, world!"; } What's wrong with this initialization? char *p = malloc(10); What is the difference between these initializations? char a[] = "string literal"; char *p = "string literal"; What's the difference between these two declarations? struct x1 { ... }; typedef struct { ... } x2; Why doesn't struct x { ... }; x thestruct; work? Can a structure contain a pointer to itself? What's the best way of implementing opaque (abstract) data types in C? I came across some code that declared a structure like this: struct name { int namelen; char namestr[1]; }; and then did some tricky allocation to make the namestr array act like it had several elements. Is this legal or portable? Is there a way to compare structures automatically? How can I pass constant values to functions which accept structure arguments? How can I read/write structures from/to data files? Why does sizeof report a larger size than I expect for a structure type, as if there were padding at the end? How can I determine the byte offset of a field within a structure? How can I access structure fields by name at run time? This program works correctly, but it dumps core after it finishes. Why? struct list { char *item; struct list *next; } /* Here is the main program. */ main(argc, argv) { ... } Can I initialize unions? What is the difference between an enumeration and a set of preprocessor #defines? Is there an easy way to print enumeration values symbolically? Why doesn't this code: a[i] = i++; work? I've experimented with the code int i = 3; i = i++; on several compilers. Some gave i the value 3, and some gave 4. Which compiler is correct? Can I use explicit parentheses to force the order of evaluation I want? Even if I don't, doesn't precedence dictate it? How can I understand these complex expressions? What's a "sequence point"? If I'm not using the value of the expression, should I use i++ or ++i to increment a variable? Why doesn't the code int a = 1000, b = 1000; long int c = a * b; work? I'm trying to declare a pointer and allocate some space for it, but it's not working. What's wrong with this code? char *p; *p = malloc(10); Does *p++ increment p, or what it points to? I have a char * pointer that happens to point to some ints, and I want to step it over them. Why doesn't ((int *)p)++; work? I have a function which accepts, and is supposed to initialize, a pointer: void f(int *ip) { static int dummy = 5; ip = &dummy; } But when I call it like this: int *ip; f(ip); the pointer in the caller remains unchanged. Why? Can I use a void ** pointer as a parameter so that a function can accept a generic pointer by reference? I have a function extern int f(int *); which accepts a pointer to an int. How can I pass a constant by reference? A call like f(&5); doesn't seem to work. Does C even have "pass by reference"? What is infamous null pointer? How do I get a null pointer in my programs? Is the abbreviated pointer comparison "if(p)" to test for non- null pointers valid? What if the internal representation for null pointers is nonzero? What is NULL and how is it #defined? How should NULL be defined on a machine which uses a nonzero bit pattern as the internal representation of a null pointer? If NULL were defined as follows: #define NULL ((char *)0) wouldn't that make function calls which pass an uncast NULL work? If NULL and 0 are equivalent as null pointer constants, which should I use? What does a run-time "null pointer assignment" error mean? How can I track it down? Why are array and pointer declarations interchangeable as function formal parameters? How can an array be an lvalue, if you can't assign to it? Practically speaking, what is the difference between arrays and pointers? How do I declare a pointer to an array? How can I set an array's size at run time? How can I avoid fixed-sized arrays? How can I declare local arrays of a size matching a passed-in array? How can I dynamically allocate a multidimensional array? How do I write functions which accept two-dimensional arrays when the width is not known at compile time? How can I use statically- and dynamically-allocated multidimensional arrays interchangeably when passing them to functions? Why doesn't sizeof properly report the size of an array when the array is a parameter to a function? Why doesn't this fragment work? char *answer; printf("Type something:\n"); gets(answer); printf("You typed \"%s\"\n", answer); I just tried the code char *p; strcpy(p, "abc"); and it worked. How? Why didn't it crash? How much memory does a pointer variable allocate? Why am I getting "warning: assignment of pointer from integer lacks a cast" for calls to malloc()? Why does some code carefully cast the values returned by malloc to the pointer type being allocated? Why isn't a pointer null after calling free()? How unsafe is it to use (assign, compare) a pointer value after it's been freed? When I call malloc() to allocate memory for a pointer which is local to a function, do I have to explicitly free() it? I'm allocating structures which contain pointers to other dynamically-allocated objects. When I free a structure, do I also have to free each subsidiary pointer? Must I free allocated memory before the program exits? How does free() know how many bytes to free? Can I query the malloc package to find out how big an allocated block is? Is it legal to pass a null pointer as the first argument to realloc()? What's the difference between calloc() and malloc()? Is it safe to take advantage of calloc's zero-filling? Does free() work on memory allocated with calloc(), or do you need a cfree()? What is alloca() and why is its use discouraged? Why doesn't strcat(string, '!'); work? How can I get the numeric (character set) value corresponding to a character, or vice versa? What is the right type to use for Boolean values in C? Is if(p), where p is a pointer, a valid conditional? How can I write a generic macro to swap two values? What's the best way to write a multi-statement macro? Is it acceptable for one header file to #include another? What's the difference between #include <> and #include "" ? What are the complete rules for header file searching? How can I construct preprocessor #if expressions which compare strings? Does the sizeof operator work in preprocessor #if directives? Can I use an #ifdef in a #define line, to define something two different ways? Is there anything like an #ifdef for typedefs? How can I use a preprocessor #if expression to tell if a machine is big-endian or little-endian? How can I list all of the predefined identifiers? How can I write a macro which takes a variable number of arguments? What is the "ANSI C Standard?" What's the difference between "const char *p" and "char * const p"? Why can't I pass a char ** to a function which expects a const char **? What's the correct declaration of main()? Can I declare main() as void ? What does the message "warning: macro replacement within a string literal" mean? What are #pragmas and what are they good for? What does "#pragma once" mean? Is char a[3] = "abc"; legal? What does it mean? Why can't I perform arithmetic on a void * pointer? What's the difference between memcpy() and memmove()? What should malloc(0) do? Return a null pointer or a pointer to 0 bytes? What's wrong with this code? char c; while((c = getchar()) != EOF) ... Why does the code while(!feof(infp)) { fgets(buf, MAXLINE, infp); fputs(buf, outfp); } copy the last line twice? How can I read one character at a time, without waiting for the RETURN key? How can I print a '%' character in a printf format string? What printf format should I use for a typedef like size_t when I don't know whether it's long or some other type? How can I implement a variable field width with printf? How can I print numbers with commas separating the thousands? What about currency formatted numbers? Why doesn't the call scanf("%d", i) work? How can I specify a variable width in a scanf() format string? Why doesn't this code: double d; scanf("%f", &d); work? How can I tell how much destination buffer space I'll need for an arbitrary sprintf call? How can I avoid overflowing the destination buffer with sprintf()? What's the difference between fgetpos/fsetpos and ftell/fseek? What are fgetpos() and fsetpos() good for? How can I redirect stdin or stdout to a file from within a program? How can I read a binary data file properly? How can I convert numbers to strings (the opposite of atoi)? Is there an itoa() function? Why does strncpy() not always place a '\0' terminator in the destination string? Why do some versions of toupper() act strangely if given an upper-case letter? How can I split up a string into whitespace-separated fields? How can I duplicate the process by which main() is handed argc and argv? How can I sort a linked list? How can I sort more data than will fit in memory? How can I get the current date or time of day in a C program? How can I add N days to a date? How can I find the difference between two dates? How can I get random integers in a certain range? How can I generate random numbers with a normal or Gaussian distribution? What does it mean when the linker says that _end is undefined? When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999? What's a good way to check for "close enough" floating-point equality? How do I round numbers? Why doesn't C have an exponentiation operator? How do I test for IEEE NaN and other special values? What's a good way to implement complex numbers in C? How can %f be used for both float and double arguments in printf()? Aren't they different types? How can I write a function that takes a variable number of arguments? How can I write a function that takes a format string and a variable number of arguments, like printf(), and passes them to printf() to do most of the work? How can I write a function analogous to scanf(), that calls scanf() to do most of the work? How can I discover how many arguments a function was actually called with? How can I write a function which takes a variable number of arguments and passes them to some other function (which takes a variable number of arguments)? How can I call a function with an argument list built up at run time? C QUESTIONS What does static variable mean? What is a pointer? What is a structure? What are the differences between structures and arrays? In header files whether functions are declared or defined? What are the differences between malloc()

9181


Un-Answered Questions { C++ }

What is the basic structure of c++ program?

559


Is map ordered c++?

584


Do you need a main function in c++?

547


What is a static element?

567


What are the 3 principles of oop?

608






What is a virtual destructor? Explain the use of it?

539


What is a stl vector?

619


How should runtime errors be handled in c++?

605


Ask to write virtual base class code?

2145


I was a c++ code and was asked to find out the bug in that. The bug was that he declared an object locally in a function and tried to return the pointer to that object. Since the object is local to the function, it no more exists after returning from the function. The pointer, therefore, is invalid outside.

599


What is the maximum combined length of command line arguments including the space between adjacent arguments?

586


What is RTTI and why do you need it?

587


How many types of scopes are there in c++?

568


What is a local variable?

567


Why is c++ considered difficult?

637