Huge collection of c programming questions with solutions

53. 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) 54. For the following program int i; i=0; repeat i=i+1; <====== PASCAL PROGRAM print i; until(i<10) end No. of times the loop is executed? 55. For the following program Convert (int A,var ,int B;int c) {A=10; B=4-; C=120; } Convert (inta,b,c) { <====== PASCAL PROGRAM a=1; b=4; c=12; } 56. For the following program Procedure A Begin -------- end <====== PASCAL PROGRAM Procedure B No. Of errors in the program ?(1,2,3,none) Begin ----------- end 57. For the following program int i; i=2; i++; if(i==4) {printf(i=4); } else {printf(i=3); } Output of the program ? a) 4 b) 3 c) unpredictable d) none Ans. (b) 58. What is FAT?. a) File Allocation Table b) File Access Table c) FDD Allocation Table d) None of the above Ans. (a) . What does the following program print? #include int sum,count; void main(void) {< BR> for(count=5;sum+=--count;) printf("%d",sum); } a. The pgm goes to an infinite loop b. Prints 4791010974 c. Prints 4791001974 d. Prints 5802112085 e. Not sure 2. What is the output of the following program? #include void main(void) { int i;< BR> for(i=2;i<=7;i++) printf("%5d",fno()); } fno() { staticintf1=1,f2=1,f3; return(f3=f1+f2,f1=f2,f2=f3); } a. produce syntax errors b. 2 3 5 8 13 21 will be displayed c. 2 2 2 2 2 2 will be displayed d. none of the above e. Not sure 3. What is the output of the following program? #include void main (void) { int x = 0x1234; int y = 0x5678; x = x & 0x5678; y = y | 0x1234; x = x^y; printf("%x\t",x); x = x | 0x5678; y = y & 0x1234; y = y^x; printf("%x\t",y); } a. bbb3 bbb7 b. bbb7 bbb3 c. 444c 4448 d. 4448 444c e. Not sure 4. What does the following program print? #include void main (void) { int x; x = 0; if (x=0) printf ("Value of x is 0"); else printf ("Value of x is not 0"); } a. print value of x is 0 b. print value of x is not 0 c. does not print anything on the screen d. there is a syntax error in the if statement e. Not sure 5. What is the output of the following program? #include #include int foo(char *); void main (void) { char arr[100] = {"Welcome to Mistral"}; foo (arr); } foo (char *x) { printf ("%d\t",strlen (x)); printf ("%d\t",sizeof(x)); return0; } a. 100 100 b. 18 100 c. 18 18 d. 18 2 e. Not sure 6. What is the output of the following program? #include display() { printf ("\n Hello World"); return 0; } void main (void) { int (* func_ptr) (); func_ptr = display; printf ("\n %u",func_ptr); (* func_ptr) (); } a. it prints the address of the function display and prints Hello World on the screen b. it prints Hello World two times on the screen c. it prints only the address of the fuction display on the screen d. there is an error in the program e. Not sure 7. What is the output of the following program? #include void main (void) { int i = 0; char ch = 'A'; do putchar (ch); while(i++ < 5 || ++ch <= 'F'); } a. ABCDEF will be displayed b. AAAAAABCDEF will displayed c. character 'A' will be displayed infinitely d. none e. Not sure 8. What is the output of the following program? #include #define sum (a,b,c) a+b+c #define avg (a,b,c) sum(a,b,c)/3 #define geq (a,b,c) avg(a,b,c) >= 60 #define lee (a,b,c) avg(a,b,c) <= 60 #define des (a,b,c,d) (d==1?geq(a,b,c):lee(a,b,c)) void main (void) { int num = 70; char ch = '0'; float f = 2.0; if des(num,ch,f,0) puts ("lee.."); else puts("geq..."); } a. syntax error b. geq... will be displayed c. lee.. will be displayed d. none e. Not sure 9. Which of the following statement is correct? a. sizeof('*') is equal to sizeof(int) b. sizeof('*') is equal to sizeof(char) c. sizeof('*') is equal to sizeof(double) d. none e. Not sure 10. What does the following program print? #include char *rev(int val); void main(void) { extern char dec[]; printf ("%c", *rev); } char *rev (int val) { char dec[]="abcde"; return dec; } a. prints abcde b. prints the address of the array dec c. prints garbage, address of the local variable should not returned d. print a e. Not sure 11. What does the following program print? void main(void) { int i; static int k; if(k=='0') printf("one"); else if(k== 48) printf("two"); else printf("three"); } a. prints one b. prints two c. prints three d. prints one three e. Not sure 12. What does the following program print? #include void main(void) { enum sub { chemistry, maths, physics }; struct result { char name[30]; enum sub sc; }; struct result my_res; strcpy (my_res.name,"Patrick"); my_res.sc=physics; printf("name: %s\n",my_res.name); printf("pass in subject: %d\n",my_res.sc); } a. name: Patrick b. name: Patrick c. name: Patrick pass in subject: 2 pass in subject:3 pass in subject:0 d. gives compilation errors e. Not sure 13. What does printf("%s",_FILE_); and printf("%d",_LINE_); do? a. the first printf prints the name of the file and the second printf prints the line no: of the second printf in the file b. _FILE_ and _LINE_ are not valid parameters to printf function c. linker errors will be generated d. compiler errors will be generated e. Not sure 14. What is the output of the following program? #include void swap (int x, int y, int t) { t = x; x = y; y = t; printf ("x inside swap: %d\t y inside swap : %d\n",x,y); } void main(void) { int x; int y; int t; x = 99; y = 100; swap (x,y,t); printf ("x inside main:%d\t y inside main: %d",x,y); } a. x inside swap : 100 y inside swap : 99 x inside main : 100 y inside main : 99 b. x inside swap : 100 y inside swap : 99 x inside main : 99 y inside main : 100 c. x inside swap : 99 y inside swap : 100 x inside main : 99 y inside main : 100 d. x inside swap : 99 y inside swap : 100 x inside main : 100 y inside main : 99 e. Not sure 15. Consider the following statements: i) " while loop " is top tested loop ii) " for loop " is bottom tested loop iii) " do - while loop" is top tested loop iv) " while loop" and "do - while loop " are top tested loops. Which among the above statements are false? a. i only b. i & ii c. iii & i d. ii, iii & iv e. Not sure 16. Consider the following piece of code: char *p = "MISTRAL"; printf ("%c\t", *(++p)); p -=1; printf ("%c\t", *(p++)); Now, what does the two printf's display? a. M M b. M I c. I M d. M S e. Not sure 17. What does the following program print? #include struct my_struct { int p:1; int q:1; int r:6; int s:2; }; struct my_struct bigstruct; struct my_struct1 { char m:1; }; struct my_struct1 small struct; void main (void) { printf ("%d %d\n",sizeof (bigstruct),sizeof (smallstruct)); } a. 10 1 b. 2 2 c. 2 1 d. 1 1 e. Not sure 18. Consider the following piece of code: FILE *fp; fp = fopen("myfile.dat","r"); Now fp points to a. the first character in the file. b. a structure which contains a char pointer which points to the first character in the file. c. the name of the file. d. none of the above. e. Not sure. 19. What does the following program print? #include #define SQR (x) (x*x) void main(void) { int a,b=3; a = SQR (b+2); } a. 25 b. 11 c. 17 d. 21 e. Not sure. 20. What does the declaration do? int (*mist) (void *, void *); a. declares mist as a function that takes two void * arguments and returns a pointer to an int. b. declares mist as a pointer to a function that has two void * arguments and returns an int. c. declares mist as a function that takes two void * arguments and returns an int. d. there is a syntax error in the declaration. e. Not sure. 21. What does the following program print? #include void main (void) { int mat [5][5],i,j; int *p; p = & mat [0][0]; for (i=0;i<5;i++) for (j=0;j<5;j++) mat[i][j] = i+j; printf ("%d\t", sizeof(mat)); < BR> i=4;j=5; printf( "%d", *(p+i+j)); } a. 25 9 b. 25 5 c. 50 9 d. 50 5 e. Not sure 22. What is the output of the following program? #include void main (void) { short x = 0x3333; short y = 0x4321; long z = x; z = z << 16; z = z | y; printf("%1x\t",z); z = y; z = z >> 16; z = z | x; printf("%1x\t",z); z = x; y = x && y; z = y; printf("%1x\t",z); } a. 43213333 3333 1 b. 33334321 4321 4321 c. 33334321 3333 1 d. 43213333 4321 4321 e. Not sure 23. What is the output of the following program? #include void main (void) { char *p = "Bangalore"; #if 0 printf ("%s", p); #endif } a. syntax error #if cannot be used inside main function b. prints Bangalore on the screen c. does not print anything on the screen d. program gives an error "undefined symbol if" e. Not sure 24. If x is declared as an integer, y is declared as float, consider the following expression: y = *(float *)&x; Which one of the following statments is true? a. the program containing the expression produces compilation errors; b. the program containing the expression produces runtime errors; c. the program containing the expression compiles and runs without any errors; d. none of the above e. Not sure 25. What is the return type of calloc function? a. int * b. void * c. no return type: return type is void d. int e. Not sure 1.Max value of SIGNED int 2. A long C program is given -- try to be familiar with few of the concepts listed below int *num={10,1,5,22,90}; main() { int *p,*q; int i; p=num; q=num+2; i=*p++; print the value of i, and q-p, and some other operations are there. } how the values will change? 3. One pointer diff is given like this: int *(*p[10])(char *, char*) Explain the variable assignment 4. char *a[4]={"jaya","mahe","chandra","buchi"}; What is the value of sizeof(a) /sizeof(char *) 5. For the following C program void fn(int *a, int *b) {int *t; t=a; a=b; b=t; } main() {int a=2; int b=3; fn(&a,&b); printf("%d,%d", a,b); } What is the output? a) Error at runtime b) Compilation error c) 2 3 d) 3 2 6. For the following C program #define scanf "%s is a string" main() {printf(scanf,scanf); } What is the output. Ans. %s is string is string 7. For the following C program {char *p="abc"; char *q="abc123"; while(*p=*q) print("%c %c",*p,*q); } a) aabbcc b) aabbcc123 c) abcabc123 d) infinate loop 8. What is the value of the following: printf("%u",-1) a) -1 b) 1 c) 65336 9. For the following C program #define void int int i=300; void main(void) {int i=200; {int i=100; print the value of i;} print the value of i;} What is the output? 10. For the following C program int x=2; x=x<<2; printf("%d ",x); Ans. 8 11. For the following C program int a[]={0,0X4,4,9}; /*some values are given*/ int i=2; printf("%d %d",a[i],i[a]); What is the value? SATYM PLACEMENT QUESTION WITH SOLUTION on c Which of following operator can't be overloaded. == b) ++ c) ?! d) <= 2. For the following C program #include main() {printf("Hello World");} The program prints Hello World without changing main() ,the output should be intialisation Hello World Desruct The changes should be a) IOstream operator<<(iostream os, char*s) os<<'intialisation'<<(Hello World)<x; 2)abc[0]-->x; 3)abc.x; 4)(abc)-->x; a )1, 2, 3 b) 2 & 3 c) 1 & 2 d) 1, 3, 4 SATYAM INTERVIEW QUESTION WITH SOLUTION on c IBM PLACEMENT QUESTION WITH SOLUTION on c Ibm 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); } } 0,5,9,13,17 b) 5,9,13,17 c) 12,17,22 d) 16,21 e) Syntax error Ans. (d) 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=max) /*error table if full */ else r[n++].k=key; } This on executing, enables a Basic sequential search b) Binary search c) Interpolation search d) None 10. Find the outpur of the following C program void f(char *p) {p=(char *) malloc(6); strcpy(p,"hello"); } void main( ) {char *P="bye"; f(p); printf("%s',p); } 6. What is the fallacy in the following program segment ? int *f1() { int a=5; return &a; } f() int *b=f1() int c=*b; } 7. Give the C language equivalents of the following a)Function returning an int pointer b)Function pointer returning an int pointer c)Function pointer returning an array of integers d)Array of function pointer returning an array of integers 8. Find the fallacy in the following program segment? int a; short b; b=a; 9. Define function ? Explain arguments in functions ? 10. How does C pass variables to a function ? 11. Explain the following program segment. f(){ int *b; *b=2; } 9. Find the output for the following C program int array[4][4] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; for (i=2;i<0;i--) for(j=2;j<=0;j--) printf("%d", arr[i][j]); 10. Find the output for the following C program #include void main() {int i,x,sum=0; int arr[6]=[1,2,3,4,5,6] for (i=0;i<4;i++) sum+ = func(arr[i]); printf("%d", sum); } func(int x) { int val,x; val = 2; return(x+ val++); } 28. For the following C progralm int d=0; for(int i=0;i<31;i++) for(int j=0;j<31;j++) for(int k=0;k<31;k++) if (((i+j+k) % 3)==0) d=d+1; Find value of d IBM INTERVIEW QUESTION WITH SOLUTION on c 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); } } 0,5,9,13,17 b) 5,9,13,17 c) 12,17,22 d) 16,21 e) Syntax error Ans. (d) 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(cnext,x++; freelist(x); } freelist(x) {free(x); return } 8. Pick up the correct function declaration. void *[] name(); 2. void int[][] name(); 3. void ** name(); 4. none of the above. For the following program. struct XXX {int a; float b; char *s; }X; If optimization :X not used in compiler then unused bits_________________. Give your assumption_______________. 2. Give the output of the following program struct XXX {int a:6; float b:4; char s; }structure; size of (structure); SUN MICRO SYSTEM INTERVIEW QUESTION WITH SOLUTION on c We have pretty sketchy information on the exact pattern of the SUN written test. There is a technical section which is pretty tough. Question based on C, UNIX, Computer Networks, Data Structures and Operating Systems can be expected. For the following program. struct XXX {int a; float b; char *s; }X; If optimization :X not used in compiler then unused bits_________________. Give your assumption_______________. 2. Give the output of the following program struct XXX {int a:6; float b:4; char s; }structure; size of (structure); 3.Class used for the multiple inheritence in JAVA_________________ anonymous class (b) inner class (c) abstreet class (d) none 4. XDR fixes in which part of OS1 stack. 5. LDAP is____________service protocol. 6. Given definition for a function which returns a array of pointers with argument of int*. 7. Give a function declaration with no arguments which refers a two dimensional array 8. Pick up the correct function declaration. void *[] name(); 2. void int[][] name(); 3. void ** name(); 4. none of the above. 9. Give the difference between monolithic and microlithic kernal: monolithic large b. microlithic used in embedded systems. c. none. 10. rPC coresponds to_______________in OSI stack. 11. Find the no.of page faults using LRU stack. eg.3 4 4 6 7 8 1 2 .. .. 12.The inorder representation of a tree 41523 and preorder is 211513 Draw it? 13. When does a stack member will be initialised when object is created (b) when object is initialised. (c) doesnot depend on object. (d) none. 14. Number of CPU in a multiprocess is contrassed by RISC nohere of CPU (b) memory (c) both (a) and (b) (d) None of the above 15. Give the output of the following program main() {char *s; s="hot java"; strcpy(s,"solarrs java") } 16. Give the output of the following program main() {printf("hot java"); fork() exit(0); } (i). When redirected to a screen what will be printed. (ii). When redirected to file what will be printed. 17. Give the output of the following program main() {int ret; ret=fork();ret=fork();ret=fork();ret=fork(); if(!ret) printf("sun"); else printf("solaris"); 18. Give the output of the following program main() {char *p='a'; int *i=100/*p; } what will be the value of *i= 1 19. Which data structure gives efficient search 1 B-tree 2 binary tree 3 array 4 linked list 20. Find the error in the following program struct point {struct point *next; int data; } x; main() {int i; for(x=p;x!=0;) x=x->next,x++; freelist(x); } freelist(x) {free(x); return } 6. Write one statement equivalent to the following two statements: x=sqr(a); return(x); Choose from one of the alternatives (a) return(sqr(a)); (b) printf("sqr(a)"); (c) return(a*a*a); (d) printf("%d",sqr(a)); 7. Which of the following about the C comments is incorrect ? (a) Comments can go over multiple lines (b) Comments can start any where in the line (c) A line can contain comments with out any language statements (d) Comments can occur within comments 8. What is the value of y in the following code? x=7; y=0; if(x=6) y=7; else y=1; (a) 7 (b) 0 (c) 1 (d) 6 9. Read the function conv() given below conv(int t) { int u; u=5/9 * (t-32); return(u); } What is returned (a) 15 (b) 0 (c) 16.1 (d) 29 10. Which of the following represents true statement either x is in the range of 10 and 50 or y is zero (a) x >= 10 && x <= 50 || y = = 0 (b) x<50 (c) y!=10 && x>=50 (d) None of these 11. Which of the following is not an infinite loop ? (a) while(1)\{ ....} (b) for(;;){...} (c) x=0; (d) # define TRUE 0 do{ /*x unaltered within the loop*/ ... .....}while(x = = 0); while(TRUE){ ....} 12. What does the following function print? func(int i) { if(i%2)return 0; else return 1; } main() { int =3; i=func(i); i=func(i); printf("%d",i); } (a) 3 (b) 1 (c) 0 (d) 2 13. How does the C compiler interpret the following two statements p=p+x; q=q+y; (a) p= p+x; (b)p=p+xq=q+y; (c)p= p+xq; (d)p=p+x/q=q+y; q=q+y; q=q+y; For questions 14,15,16,17 use the following alternatives: a.int b.char c.string d.float 14. '9' 15. "1 e 02" 16. 10e05 17. 15 18. Read the folllowing code # define MAX 100 # define MIN 100 .... .... if(x>MAX) x=1; else if(x>=b (d) a**=b 27. What is y value of the code if input x=10 y=5; if (x==10) else if(x==9) else y=8; (a)9 (b)8 (c)6 (d)7 28. What does the following code do? fn(int n, int p, int r) { static int a=p; switch(n) { case 4:a+=a*r; case 3:a+=a*r; case 2:a+=a*r; case 1:a+=a*r; } } (a) computes simple interest for one year (b) computes amount on compound interest for 1 to 4 years (c) computes simple interest for four year (d) computes compound interest for 1 year 29. a=0; while(a<5) printf("%d\\n",a++); How many times does the loop occurs? (a) infinite (b)5 (c)4 (d)6 30. How many times does the loop iterated ? for(i=0;i=10;i+=2) printf("Hi\\n"); (a)10 (b) 2 (c) 5 (d) None of these 31. What is incorrect among the following A recursive function (a) calls itself (b) is equivalent to a loop (c) has a termination condition (d) does not have a return value at all 32. Which of the following go out of the loop if expn 2 becoming false (a) while(expn 1)\{...if(expn 2)continue;} (b) while(!expn 1)\{if(expn 2)continue;...} (c) do{..if(expn 1)continue;..}while(expn 2); (d) while(!expn 2)\{if(expn 1)continue;..\} 33. Consider the following program main() { unsigned int i=10; while(i>=0) { printf("%u",i) i--; } } How many times the loop will get executed (a)10 (b)9 (c)11 (d) infinite 34.Pick out the odd one out (a) malloc() (b) calloc() (c) free() (d) realloc() 35.Consider the following program main() { int a[5]={1,3,6,7,0}; int *b; b=&a[2]; } The value of b[-1] is (a) 1 (b) 3 (c) -6 (d) none 36. # define prod(a,b)=a*b main() { int x=2; int y=3; printf("%d",prod(x+2,y-10)); } the output of the program is (a) 8 (b) 6 (c) 7 (d) None 37.Consider the following program segment int n,sum=1; switch(n) { case 2:sum=sum+2; case 3:sum*=2; break; default:sum=0; } If n=2, what is the value of sum (a) 0 (b) 6 (c) 3 (d) None of these 38. Identify the incorrect one 1.if(c=1) 2.if(c!=3) 3.if(a0) { x=func(i); i--; } int func(int n) { static sum=0; sum=sum+n; return(sum); } } The final value of x is (a) 6 (b) 8 (c) 1 (d) 3 43. Int *a[5] refers to (a) array of pointers (b) pointer to an array (c) pointer to a pointer (d) none of these 44.Which of the following statements is incorrect (a) typedef struct new { int n1; char n2; } DATA; (b) typedef struct { int n3; char *n4; }ICE; (c) typedef union { int n5; float n6; } UDT; (d) #typedef union { int n7; float n8; } TUDAT; Q1. typedef struct{ char *; nodeptr next; } * nodeptr ; What does nodeptr stand for? Q2. What does. int *x[](); means ? Q3. struct list{ int x; struct list *next; }*head; the struct head.x =100 Is the above assignment to pointer is correct or wrong ? Ans. Wrong Q4.What is the output of the following ? int i; i=1; i=i+2*i++; printf(%d,i); Ans. 4 Q5. 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. What are the output(s) for the following ? Q6. #include char *f() {char *s=malloc(8); strcpy(s,"goodbye")} main() { char *f(); printf("%c",*f()='A'); } Q7. #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 Q8. a=10;b= 5;c=3;d=3; if(a show(int t,va_list ptr1) { int a,x,i; a=va_arg(ptr1,int) printf("\n %d",a) } display(char) { int x; listptr; va_star(otr,s); n=va_arg(ptr,int); show(x,ptr); } main() { display("hello",4,12,13,14,44); } Q10. main() { printf("hello"); fork(); } Q11. main() { int i = 10; printf(" %d %d %d \n", ++i, i++, ++i); } Q12. #include main() { int *p, *c, i; i = 5; p = (int*) (malloc(sizeof(i))); printf("\n%d",*p); *p = 10; printf("\n%d %d",i,*p); c = (int*) calloc(2); printf("\n%d\n",*c); } Q13. #define MAX(x,y) (x) >(y)?(x):(y) main() { inti=10,j=5,k=0; k= MAX(i++,++j); printf("%d..%d..%d",i,j,k); } Q14. #include main() { enum _tag{ left=10, right, front=100, back}; printf("left is %d, right is %d, front is %d, back is %d",left,right,front,back); } Q15. main() { inta=10,b=20;
a>=5?b=100:b=200; printf("%d\n",b); } Q16. #define PRINT(int) printf("int = %d ",int) main() {< BR> intx,y,z;
x=03;y=02;z=01; PRINT(x^x); z<<=3;PRINT(x); y>>=3;PRINT(y); } Q17. #include main() { char s[] = "Bouquets and Brickbats"; printf("\n%c, ",*(&s[2])); printf("%s, ",s+5); printf("\n%s",s); printf("\n%c",*(s+2)); } Q18. main() { struct s1 { char *str; struct s1 *ptr; }; static struct s1 arr[] = { {"Hyderabad",arr+1}, {"Bangalore",arr+2}, {"Delhi",arr} }; struct s1 *p[3]; int i; < BR> for(i=0;i<=2;i++) p[i] = arr[i].ptr; printf("%s\n",(*p)->str); printf("%s\n",(++*p)->str); printf("%s\n",((*p)++)->str); } Q19. .main() { char *p = "hello world!"; p[0] = 'H'; printf("%s",p); } MICOSOFT PLACEMENT QUESTION WITH SOLUTION on c MICROSOFT INTERVIEW QUESTION WITH SOLUTION on c Sonata s/w PLACEMENT QUESTION 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. In the following code, in which order the functions would be called? a= f1(23,14)*f2(12/4)+f3(); a) f1, f2, f3 b) f3, f2, f1 c) The order may vary from compiler to compiler d) None of the above 4. What would be the output of the following program? main() { int i=4; switch(i) { default: printf("\n A mouse is an elephant built by the Japanese"); case 1: printf(" Breeding rabbits is a hair raising experience"); break; case 2: printf("\n Friction is a drag"); break; case 3: printf("\n If practice make perfect, then nobody's perfect"); } } a) A mouse is an elephant built by the Japanese b) Breeding rabbits is a hare raising experience c) All of the above d) None of the above 5. What is the output of the following program? #define SQR(x) (x*x) main() { int a,b=3; a= SQR(b+2); printf("%d",a); } a) 25 b) 11 c) error d) garbage value 6. In which line of the following, an error would be reported? 1. #define CIRCUM(R) (3.14*R*R); 2. main() 3. { 4. float r=1.0,c; 5. c= CIRCUM(r); 6. printf("\n%f",c); 7. if(CIRCUM(r))==6.28) 8. printf("\nGobbledygook"); 9. } a) line 1 b) line 5 c) line 6 d) line 7 7. What is the type of the variable b in the following declaration? #define FLOATPTR float* FLOATPTR a,b; a) float b) float pointer c) int d) int pointer 8. In the following code; #include main() { FILE *fp; fp= fopen("trial","r"); } fp points to: a) The first character in the file. b) A structure which contains a "char" pointer which points to the first character in the file. c) The name of the file. d) None of the above. 9. We should not read after a write to a file without an intervening call to fflush(), fseek() or rewind() < TRUE/FALSE> Ans. True 10. If the program (myprog) is run from the command line as myprog 1 2 3 , What would be the output? main(int argc, char *argv[]) { int i; for(i=0;i0) printf("%s",*++argv); } a) myprog monday tuesday wednesday thursday b) monday tuesday wednesday thursday c) myprog tuesday thursday d) None of the above 13. In the following code, is p2 an integer or an integer pointer? typedef int* ptr ptr p1,p2; Ans. Integer pointer 14. 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. 15. What would be the output of the following program? main() { int y=128; const int x=y; printf("%d",x); } a) 128 b) Garbage value c) Error d) 0 16. What is the difference between the following declarations? const char *s; char const *s; Ans. No difference 17. What is the difference between the following declarations? const char *const s; char const *const s; Ans. No difference 18. What would be the output of the following program? main() { char near * near *ptr1; char near * far *ptr2; char near * huge *ptr3; printf("%d %d %d",sizeof(ptr1),sizeof(ptr2),sizeof(ptr3)); } a) 1 1 1 b) 1 2 4 c) 2 4 4 d) 4 4 4 19. If the following program (myprog) is run from the command line as myprog friday tuesday sunday, What would be the output? main(int argc, char*argv[]) { printf("%c",**++argv); } a) m b) f c) myprog d) friday 20. If the following program (myprog) is run from the command line as myprog friday tuesday sunday, What would be the output? main(int argc, char *argv[]) { printf("%c",*++argv[1]); } a) r b) f c) m d) y 21. If the following program (myprog) is run from the command line as myprog friday tuesday sunday, What would be the output? main(int argc, char *argv[]) { while(sizeofargv) printf("%s",argv[--sizeofargv]); } a) myprog friday tuesday sunday b) myprog friday tuesday c) sunday tuesday friday myprog d) sunday tuesday friday 22. 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. 23. 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)); 24. Would the following program compile? main() { int a=10,*j; void *k;< BR> j=k=&a; j++; k++; printf("\n%u%u",j,k); } a) Yes b) No, the format is incorrect c) No, the arithmetic operation is not permitted on void pointers d) No, the arithmetic operation is not permitted on pointers 25. According to ANSI specifications which is the correct way of declaring main() when it receives command line arguments? a) main(int argc, char *argv[]) b) main(argc,argv) int argc; char *argv[]; c) main() {int argc; char *argv[]; } d) None of the above 26. What error would the following function give on compilation? f(int a, int b) { int a; a=20; return a; } a) missing parenthesis in the return statement b) The function should be declared as int f(int a, int b) c) redeclaration of a d) None of the above 27. 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 28. What would be the output of the following program? main() { const int x=5; int *ptrx; ptrx=&x; *ptrx=10; printf("%d",x); } a) 5 b) 10 c) Error d) Garbage value 29. A switch statement cannot include a) constants as arguments b) constant expression as arguments c) string as an argument d) None of the above 30. How long the following program will run? main() { printf("\nSonata Software"); main(); } a) infinite loop b) until the stack overflows c) All of the above d) None of the above 31. On combining the following statements, you will get char*p; p=malloc(100); a) char *p= malloc(100) b) p= (char*)malloc(100) c) All of the above d) None of the above 32. What is the output of the following program? main() { int n=5; printf("\nn=%*d",n,n); } a) n=5 b) n=5 c) n= 5 d) error Sonata s/w INTERVIEW QUESTION WITH SOLUTION ADITYA TECH. PLACEMENT QUESTION WITH SOLUTION Write the programs for the following problems in C. 1. Swap two variables x,y without using a temporary variable. 2. Write algorithm for finding the GCD of a number. 3.Write a program for reversing the given string. 4. The integers from 1 to n are stored in an array in a random fashion. but one integer is missing. Write a program to find the missing integer. Ans). Hint : The sum of n natural numbers is = n(n+1)/2. if we subtract the above sum from the sum of all the numbers in the array , the result is nothing but the missing number. 5. Some bit type of questions has been given on pointers asking to to find whether it is correct from syntax point of view. and if it is correct explain what it will do.(around 15 bits). SECTION-B 6. For the following C program #define AND && #define ARRANGE (a>25 AND a<50) main() {int a = 30; if (ARRANGE) printf("within range"); else printf("out of range"); } What is the output? 7. 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); } What is the output? Ans. Area of the circle is 122.656250 Area of the circle is 19.625000 8. What do the following statements indicate. Explain. int(*p)[10] int*f() int(*pf)() int*p[10] Refer to: -- Kernighan & Ritchie page no. 122 -- Schaum series page no. 323 9. Write a C program to find whether a stack is progressing in forward or reverse direction. 10. Write a C program that reverses the linked ADITYA TECH. INTERVIEW QUESTION WITH SOLUTION HCL PLACEMENT QUESTION WITH SOLUTION 1. Given the following statement enum day = { jan = 1 ,feb=4, april, may} What is the value of may? 4 (b) 5 (c) 6 (d) 11 (e) None of the above 2. Find the output for the following C program main {int x,j,k; j=k=6;x=2; x=j*k; printf("%d", x); 3. Find the output for the following C program fn f(x) { if(x<=0) return; else f(x-1)+x; } 4. Find the output for the following C program i=20,k=0; for(j=1;j9 && Y++!=10 && Y++>10) {printf("%d", Y); else printf("%d", Y); } Ans. 13 8. Find the output for the following C program f=(x>y)?x:y f points to max of x and y b) f points to min of x and y c)error Ans. (a) 9. What is the sizeof(long int) 4 bytes (b) 2 bytes (c) compiler dependent (d) 8 bytes 10. Which of the function operator cannot be over loaded <= (b) ?: (c) == (d) * 11. Find the output for the following C program main() {intx=2,y=6,z=6; x=y==z; printf(%d",x) } Section C (Programming Skills) Answer the questions based on the following program STRUCT DOUBLELIST { DOUBLE CLINKED INT DET; LIST VOID STRUCT PREVIOUS; (BE GIVEN AND A PROCEDURE TO DELETE) STRUCT NEW; (AN ELEMENT WILL BE GIVEN) } DELETE(STRUCT NODE) {NODE-PREV-NEXT NODE-NEXT; NODE-NEXT-PREV NODE-PREV; IF(NODE==HEAD) NODE } Q. In what case the prev was All cases (b) It does not work for the last element (c) It does not for the first element (d) None of these Answer the questions based on the following program VOID FUNCTION(INT KK) {KK+=20; } VOID FUNCTION (INT K) INT MM,N=&M KN = K KN+-=10; } Q. What is the output of the following program main() { int var=25,varp; varp=&var; varp p = 10; fnc(varp) printf("%d%d,var,varp); } 20,55 (b) 35,35 (c) 25,25 (d)55,55 HCL INTERVIEW QUESTION WITH SOLUTION RAMCO sys. PLACEMENT QUESTION WITH SOLUTION Find the output for the following C program main() { char *p1="Name"; char *p2; p2=(char *)malloc(20); while(*p2++=*p1++); printf("%s\n",p2); } Ans. An empty string 2) Find the output for the following C program main() { intx=20,y=35; x = y++ + x++; y = ++y + ++x; printf("%d %d\n",x,y); } Ans. 57 94 3) Find the output for the following C program main() { int x=5; printf("%d %d %d\n",x,x<<2,x>>2); } Ans. 5 20 1 4) Find the output for the following C program < P>#defineswap1(a,b)a=a+b;b=a-b;a=a-b; main() { intx=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; } Ans. 10 5 5) Find the output for the following C program main() { char *ptr = "Ramco Systems"; (*ptr)++; printf("%s\n",ptr); ptr++; printf("%s\n",ptr); } Ans. Samco Systems 6) Find the output for the following C program #include main() { char s1[]="Ramco"; char s2[]="Systems"; s1=s2; printf("%s",s1); } Ans. Compilation error giving it cannot be an modifiable 'lvalue' 7) 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); } Ans. RamcoSystems 8) Find the output for the following C program given that [1]. The following variable is available in file1.c static int average_float; Ans. All the functions in the file1.c can access the variable 9) Find the output for the following C program # define TRUE 0 some code while(TRUE) { some code } Ans. This won't go into the loop as TRUE is defined as 0 10) Find the output for the following C program main() { int x=10; x++; change_value(x); x++; Modify_value(); printf("First output: %d\n",x); } x++; change_value(x); printf("Second Output : %d\n",x); Modify_value(x); printf("Third Output : %d\n",x); } Modify_value() { return (x+=10); } change_value() { return(x+=1); } Ans. 12 1 1 11) Find the output for the following C program main() { intx=10,y=15; x=x++; y=++y; printf("%d %d\n",x,y); } Ans. 11 16 12) Find the output for the following C program main() { int a=0; if(a=0) printf("Ramco Systems\n"); printf("Ramco Systems\n"); } Ans. Ony one time "Ramco Systems" will be printed 13) Find the output for the following C program #include int SumElement(int *,int); void main(void) { int x[10]; int i=10; for(;i;) { i--; *(x+i)=i; } printf("%d",SumElement(x,10)); } int SumElement(int array[],int size) { int i=0; float sum=0; for(;i void main(void); int printf(const char*,...); void main(void) { inti=100,j=10,k=20; -- int sum; float ave; charmyformat[]="ave=%.2f"; sum=i+j+k; ave=sum/3.0; printf(myformat,ave); } Q15) Find the output for the following C program #include void main(void); { int a[10]; printf("%d",((a+9) + (a+1))); } Q16) Find the output for the following C program #include void main(void) { struct s{ int x; float y; }s1={25,45.00}; union u{ int x; float y; } u1; u1=(union u)s1; printf("%d and %f",u1.x,u1.y); } Q17) Find the output for the following C program #include void main(void) { unsigned int c; unsigned x=0x3; scanf("%u",&c); switch(c&x) { case 3: printf("Hello!\t"); case 2: printf("Welcome\t"); case 1: printf("To All\t"); default:printf("\n"); } } Q18) Find the output for the following C program #include int fn(void); void print(int,int(*)()); int i=10; void main(void) { int i=20; print(i,fn); } void print(int i,int (*fn1)()) { printf("%d\n",(*fn1)()); } int fn(void) { return(i-=5); } Q19) Find the output for the following C program #include void main(void); { char numbers[5][6]={"Zero","One","Two","Three","Four"}; printf("%s is %c",&numbers[4][0],numbers[0][0]); } Q20) Find the output for the following C program int bags[5]={20,5,20,3,20}; void main(void) { int pos=5,*next(); *next()=pos; printf("%d %d %d",pos,*next(),bags[0]); } int *next() { int i; for(i=0;i<5;i++) if (bags[i]==20) return(bags+i); printf("Error!"); exit(0); } Q21) Find the output for the following C program #include void main(void) { inty,z;
intx=y=z=10; int f=x; float ans=0.0; f *=x*y; ans=x/3.0+y/3; printf("%d %.2f",f,ans); } Q22) Find the output for the following C program #include void main(void); { doubledbl=20.4530,d=4.5710,dblvar3; double dbln(void); dblvar3=dbln(); printf("%.2f\t%.2f\t%.2f\n",dbl,d,dblvar3); } double dbln(void) { double dblvar3; dbl=dblvar3=4.5; return(dbl+d+dblvar3); } Q23) Find the output for the following C program #include static int i=5; void main(void) { int sum=0; do { sum+=(1/i); }while(0 void main(void) { intoldvar=25,newvar=-25; int swap(int,int); swap(oldvar,newvar); printf("Numbers are %d\t%d",newvar,oldvar); } int swap(int oldval,int newval) { int tempval=oldval; oldval=newval; newval=tempval; } Q25) Find the output for the following C program #include void main(void); { inti=100,j=20; i++=j; i*=j; printf("%d\t%d\n",i,j); } Q26) Find the output for the following C program #include void main(void); int newval(int); void main(void) { int ia[]={12,24,45,0}; int i; int sum=0; for(i=0;ia[i];i++) { sum+=newval(ia[i]); } printf("Sum= %d",sum); } int newval(int x) { static int div=1; return(x/div++); } Q27) Find the output for the following C program #include void main(void); { int var1,var2,var3,minmax; var1=5; var2=5; var3=6; minmax=(var1>var2)?(var1>var3)?var1:var3:(var2>var3)?var2:var3; printf("%d\n",minmax); Q28) Find the output for the following C program #include void main(void); { void pa(int *a,int n); int arr[5]={5,4,3,2,1}; pa(arr,5); } void pa(int *a,int n) { int i; for(i=0;i void main(void); void print(void); { print(); } void f1(void) { printf("\nf1():"); } Q30) Find the output for the following C program #include "6.c" void print(void) { extern void f1(void); f1(); } static void f1(void) { printf("\n static f1()."); } Q31) Find the output for the following C program #include void main(void); static int i=50; int print(int i); void main(void) { static int i=100; while(print(i)) { printf("%d\n",i); i--; } } int print(int x) { static int i=2; return(i--); } Q32) Find the output for the following C program #include void main(void); typedef struct NType { int i; char c; long x; } NewType; void main(void) { NewType *c; c=(NewType *)malloc(sizeof(NewType)); c->i=100; c->c='C'; (*c).x=100L; printf("(%d,%c,%4Ld)",c->i,c->c,c->x); } Q33) Find the output for the following C program #include void main(void); const int k=100; void main(void) { int a[100]; int sum=0; for(k=0;k<100;k++) *(a+k)=k; sum+=a[--k]; printf("%d",sum); }

No comments: