question of c programming with solution

1. Point out error, if any, in the following program main() { int i=1; switch(i) { case 1: printf("\nRadioactive cats have 18 half-lives"); break; case 1*2+4: printf("\nBottle for rent -inquire within"); break; } } Ans. No error. Constant expression like 1*2+4 are acceptable in cases of a switch. 2. Point out the error, if any, in the following program main() { int a=10,b; a>= 5 ? b=100 : b=200; printf("\n%d",b); } Ans. lvalue required in function main(). The second assignment should be written in parenthesis as follows: a>= 5 ? b=100 : (b=200); 3. We should not read after a write to a file without an intervening call to fflush(), fseek() or rewind() < TRUE/FALSE> Ans. True 4. In the following code, is p2 an integer or an integer pointer? typedef int* ptr ptr p1,p2; Ans. Integer pointer 5. Point out the error in the following program main() { const int x; x=128; printf("%d",x); } Ans. x should have been initialized where it is declared. 6. What is the difference between the following declarations? const char *s; char const *s; Ans. No difference 7. What is the difference between the following declarations? const char *const s; char const *const s; Ans. No difference 8. Point out the error in the following program main() { int a=10; void f(); a=f(); printf("\n%d",a); } void f() { printf("\nHi"); } Ans. The program is trying to collect the value of a "void" function into an integer variable. 9. In the following program how would you print 50 using p? main() { int a[]={10, 20, 30, 40, 50}; char *p; p= (char*) a; } Ans. printf("\n%d",*((int*)p+4)); 10. Point out the error in the following program main() { const char *fun(); *fun()='A'; } const char *fun() { return "Hello"; } Ans. fun() returns to a "const char" pointer which cannot be modified 11. For the following C program

int x(char *a) {a=(char *) malloc(10*sizeof(char)); *a="hello"; }

main() {char *a="new"; x(a); printf("%s",a); }

The output is

a) Hello b) New c) Hello new d) Run time error

Ans. (b)
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? Ans: None.
14. void main() { int i=7; printf("%d",i++*i++); } Ans: 56
15. #define one 0 #ifdef one printf("one is defined "); #ifndef one printf("one is not defined "); Ans: "one is defined" 16. void main() { intcount=10,*temp,sum=0; temp=&count; *temp=20; temp=&sum; *temp=count; printf("%d %d %d ",count,*temp,sum); } Ans: 20 20 20 17. There was question in c working only on unix machine with pattern matching. 18. what is alloca()
Ans : It allocates and frees memory after use/after getting out of scope
19. main() { static i=3; printf("%d",i--); return i>0 ? main():0; } Ans: 321
20. char *foo() { char result[100]); strcpy(result,"anything is good"); return(result); } void main() { char *j; j=foo() printf("%s",j); } Ans: anything is good.
21. void main() { char *s[]={ "dharma","hewlett-packard","siemens","ibm"}; char **p; p=s; printf("%s",++*p); printf("%s",*p++); printf("%s",++*p); }

Ans: "harma" (p->add(dharma) && (*p)->harma) "harma" (after printing, p->add(hewlett-packard) &&(*p)->harma) "ewlett-packard"

22. void main() { int d=5; printf("%f",d); }

Ans: Undefined

23. 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); }

Ans: 1,2,3,4

24. void main() { char *s="\12345s\n"; printf("%d",sizeof(s)); }

Ans: 6

25. 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(i<j) printf("less"); else if(i>j) printf("greater"); else if(i==j) printf("equal"); }

Ans: less

26. void main() { float j; j=1000*1000; printf("%f",j); } 1. 1000000 2. Overflow 3. Error 4. None

Ans: 4

27.How do you declare an array of N pointers to functions returningpointers to functions returning pointers to characters? Ans: The first part of this question can be answered in at leastthree ways: 28. #define MAN(x,y) (x)>(y)?(x):(y) { inti=10;j=5;k=0; k= MAX(i++,++j) printf(%d %d %d %d,i,j,k) }

Ans. 10 5 0

29.What is the output of the following ? int i; i=1; i=i+2*i++; printf(%d,i);

Ans. 4

30. FILE *fp1,*fp2; fp1=fopen("one","w") fp2=fopen("one","w") fputc('A',fp1) fputc('B',fp2) fclose(fp1) fclose(fp2)} a.error
b.
c.
d.

Ans. no error. But It will over writes on same file.

31. For the following C program

#define AREA(x)(3.14*x*x) main() {floatr1=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); }

32.What is the output?

Ans. Area of the circle is 122.656250 Area of the circle is 19.625000
33 main() { static i=3; printf("%d",i--); return i>0 ? main():0; } Ans: 321
34 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); } 35.What are the number of syntax errors in the above? Ans: None.
36 . void main() { int i=7; printf("%d",i++*i++); } Ans: 56
37. #define one 0 #ifdef one printf("one is defined "); #ifndef one printf("one is not defined "); Ans: "one is defined" 38. void main() { intcount=10,*temp,sum=0; temp=&count; *temp=20; temp=&sum; *temp=count; printf("%d %d %d ",count,*temp,sum); } Ans: 20 20 20 39. For the following C program
void main()
{
unsigned char c;
for(c=0;c!=256;c=c+2)

printf("%d",c);

getch();

}

(a) 127 (b) 128 (c) 256 (d) infinitely Ans: (d)

40. For the following program

int i; i=2; i++; if(i==4) {printf(i=4); } else {printf(i=3); }

41.Output of the program ? a) 4 b) 3 c) unpredictable d) none Ans. (b)

42. What is FAT?. a) File Allocation Table b) File Access Table c) FDD Allocation Table d) None of the above Ans. (a)
28.
struct list{
int x;
struct list *next;
}*head;
struct head.x =100
Is the above assignment to pointer is correct or wrong ? Ans. Wrong

No comments: