Sunteți pe pagina 1din 50

What will be output if you will execute following c code? #include<stdio.

h> int main(){ int i; for(i=0;i<5;i++){ int i=10; printf(" %d",i); i++; } return 0; } 10 11 12 13 14 10 10 10 10 10 0 1 2 3 4 Compilation error What will be output if you will execute following c code? #include<stdio.h> int main(){ register a,b,x; scanf("%d %d",&a,&b); x=a+~b; printf("%d",x); return 0; } 0 It will be difference of a and b It will be addition of a and b Compilation error What will be output if you will execute following c code? #include<stdio.h> auto int a=5; int main(){ int x; x=~a+a&a+a<<a; printf("%d",x); return 0; } 5 0 153 Compilation error What will be output if you will execute following c code? #include<stdio.h> int main(){ register int a,b; int c; scanf("%d %d",&a,&b); c=~a + ~b + ++a + b++; printf(" %d",c); return 0; } //User input is: 1 2 -1 0 1 Compilation error What will be output if you will execute following c code? #include<stdio.h> int main(){

int arr[3]={10,20,30}; +arr[++x] + ++x + arr[--x]; return 0; } 22 turbo c 23 linux gcc 43 turbo c 44 linux gcc

int x=0; x = + printf("%d ",x);

What will be output if you will execute following c code? #include<stdio.h> int main(){ int a[]={10,20,30,40}; int i=3,x; x=1*a[--i]+2*a[--i]+3*a[--i]; printf("%d",x); return 0; } 30 linux 60 turbo c 90 linux Compilation error What will be output if you will execute following c code? #include<stdio.h> int main(){ static int a[][2] [3]={0,1,2,3,4,5,6,7,8,9,10,11,12}; int i=-1; int d; d=a[i++][++i][++i]; printf("%d",d); return 0; } 9 10 Compilation error 11 What will be output if you will execute following c code? #include<stdio.h> int f(int); int main(){ int i=3,val; val=sizeof (f(i)+ +f(i=1)+ +f(i-1)); printf("%d %d",val,i); return 0; } int f(int num){ return num*5; } 2 3 Turbo C 4 3 linux GCC 3 2 Turbo C & linux GCC Compilation error

What will be output if you will execute following c code? #include<stdio.h> int main(){ int x,a=3; x=+ +a+ + +a+ + +5; printf("%d %d",x,a); return 0; } 10 3 11 3 10 5 Compilation error What will be output if you will execute following c code? #include<stdio.h> int main(){ int num,i=0; num=-++i+ ++-i; printf("%d",num); return 0; } 0 1 -2 Compilation error What will be output if you will execute following c code? #include<stdio.h> int main(){ int num,a=5; num=-a--+ +++a; printf("%d %d",num,a); return 0; } 1 5 -1 6 1 6 0 5 What will be output if you will execute following c code? #include<stdio.h> int main(){ int num,a=15; num=- - - -a--; printf("%d %d",num,a); return 0; } 15 14 14 15 14 14 15 15 What will be output if you will execute following c code? #include<stdio.h> int main(){

int x,a=2; x=++a,++a,a++; %d",x,a); return 0; } 5 5 3 5 4 5 5 4

printf("%d

What will be output if you will execute following c code? #include<stdio.h> int main(){ int x,i=2; x=~-!++i; printf("%d",x); return 0; } -2 -1 0 1 What will be output if you will execute following c code? #include<stdio.h> int main(){ static double *p,*q,*r,*s,t=5.0; double **arr[]={&p,&q,&r,&s}; int i; *p=*q=*r=*s=t; for(i=0;i<4;i++) printf("%.0f ",**arr[i]); return 0; } 5 5 5 5 5 5 6 7 8 9 Infinite loop Run time error What will be output if you will execute following c code? #include<stdio.h> int main(){ float x; x=0.35==3.5/10; printf("%f",x); return 0; } 0.000000 1.000000 0.350000 Compilation error #include<stdio.h> int main(){ int arr[]={6,12,18,24}; int x=0; x=arr[1]+(arr[1]=2); printf("%d",x); return 0; }

4 8 14 Compilation error What will be output if you will execute following c code? #include<stdio.h> int sq(int); int main(){ int a=1,x; x=sq(++a)+sq(a++)+sq(a++); printf("%d",x); return 0; } int sq(int num){ return num*num; } 15 16 17 18 What will be output if you will execute following c code? #include<stdio.h> int main(){ printf("%c",*"abcde"); return 0; } acbcd e a NULL What will be output if you will execute following c code? #include<stdio.h> int main(){ printf("%d","abcde"-"abcde"); return 0; } 0 -1 1 Garbage What will be output if you will execute following c code? #include<stdio.h> int main(){ int a=0; #if (a==0) printf("Equal"); #else if printf("Not equal"); #endif return 0; } Equal Not equal null

(A) (B) (C) (D) Garbage (E) Compilation error

2.
What will be output if you will execute following c code? #include<stdio.h> int main(){ for(;NULL;) printf("cquestionbank"); return 0; }

(A) c (B) bank (C) cquestionbank (D) Infinite loop (E) Compilation error

3.
What will be output if you will execute following c code? #include<stdio.h> auto int a=5; int main(){ int x; x=~a+a&a+a<<a; printf("%d",x); return 0;

(A) 0 (B) 1 (C) 154 (D) 155 (E) Compilation error

4.
What will be output if you will execute following c code? #include<stdio.h> void main(){ int a=5; { int b=10; ++b; ++a; { int a=20; ++a; a=++b; } ++a; ++b; printf("%d %d",a,b); } printf(" %d",a); }

(A) 7 13 7 (B) 13 13 13 (C) 13 13 5 (D) 6 13 5 (E) Compilation error

5.
What will be output if you will execute following c code? #include<stdio.h> #include<conio.h> void main(){ int a[]={0,1,2,3,4,5,6,7,8,9,10}; int i=0,num; num=a[++i+a[++i]]+a[++i]; printf("%d",num); }

(A) 6 (B) 7 (C) 8 (D) 9 (E) Compilation error

6.
What will be output if you will execute following c code? #include<stdio.h> #include<conio.h> void main(){ int i=3,val; val=sizeof f(i)+ +f(i=1)+ +f(i-1); printf("%d %d",val,i); } int f(int num){ return num*5; }

(A) 2 0 (B) 7 1 (C) 17 0 (D) 2 1 (E) Compilation error

7.
What will be output if you will execute following c code? #include<stdio.h> #include<conio.h> void main(){ int i; (i=8)+=1; printf("%d",i); }

(A) 9 (B) 10 (C) 32 (D) 34 (E) Compilation error

8.
What will be output if you will execute following c code? #include<stdio.h> #include<conio.h> void main(){ char c=-'a'; printf("%d",c); }

(A) 65 (B) -65 (C) -a (D) -97 (E) Compilation error

9.
What will be output if you will execute following c code? #include<stdio.h> #include<conio.h> void main(){ int num,a=5; num=-a--; printf("%d %d",num,a); }

(A) 5 4 (B) -4 4 (C) -5 4 (D) -4 5 (E) Compilation error

10.
What will be output if you will execute following c code? #include<stdio.h> #include<conio.h> void main(){ int num,a=10; num=a&&0+ +-1&&a; printf("%d %d",num,a);

(A) 1 1 (B) 0 0 (C) 1 10 (D) 0 10 (E) Compilation error

11.
What will be output if you will execute following c code? #include<stdio.h> #include<conio.h> void main(){ int x,y,z; y=(x=1,y=2,z=3); printf("%d %d %d",x,y,z); }

(A) 1 2 3 (B) 1 1 3 (C) 1 3 3 (D) 1 0 3 (E) Compilation error

12.
What will be output if you will execute following c code? #include<stdio.h> #include<conio.h> void main(){ int t,a=5,b=10,c=15; t=(++a&&++b,++a),++a||++c;

printf("%d

%d

%d %d",t,a,b,c);

(A) 7 8 11 15 (B) 6 7 10 14 (C) 1 8 10 15 (D) 6 18 11 15 (E) Compilation error

13.
What will be output if you will execute following c code? #include<stdio.h> #include<conio.h> int a=5; void main(){ int x; x=!a+change(); printf("%d",x); } int change(){ a=0; return 0; }

(A) 0 (B) 5.000000 (C) 0.000000 (D) 1.000000 (E) Compilation error

14.
What will be output if you will execute following c code?

#include<stdio.h> typedef struct cquestionbank{ int num; struct cquestionbank **p; struct cquestionbank ***q; }cqb; int main(){ static cqb *var1,**var2; cqb temp={5,&var1,&var2}; var2=&var1; var1->num=25; printf("%d %d ",**(temp.q),***(temp.q)); return 0; }

(A) 0 5 (B) 0 25 (C) 5 25 (D) 25 5 (E) Compilation error

15.
What will be output if you will execute following c code? #include<stdio.h> union cqbu{ int a; char b; }; struct cqbs{ int a; char b; }; int main(){ union cqbu u={25}; struct cqbs *ps=(struct cqbs *)&u;

union cqbu *pu=(union cqbu *)&u; printf("%d %d\n",ps->a,ps->b); printf("%d %d\n",pu->a,pu->b); return 0; }

(A) 25 0

25 25 25 0

(B) 25 25 (C) 0 0

25 25

(D) 25 25

25 25 (E) Compilation error

16.
What will be output if you will execute following c code? int main(){ float x; x=(float)3.5==3.5; printf("%f",x); return 0; }

(A) 0 (B) 0.000000 (C) 1.000000 (D) 3.000000 (E) Compilation error

17.
What will be output if you will execute following c code? int main(){ float x; x=(int)(int)2.5+5.8f; printf("%f",x); return 0; }

(A) 7.000000 (B) 7.800000 (C) 8.000000 (D) 8.100000 (E) Compilation error

18.
What will be output if you will execute following c code? int main(){ float x; x=(float)3.3==3.3; printf("%f",x); return 0; }

(A) 0.000000 (B) 1.000000 (C) 3.000000 (D) 3.300000 (E) Compilation error

19.
What will be output if you will execute following c code?
#include "string.h" typedef struct stu1{ int roll; char *name; double marks; }STU1; typedef struct stu2{ int roll; char *name; double marks; }STU2; void main(){ STU1 s1={25,"Rohit",87.43},*p1; static STU2 *p2; p1=&s1; memccpy(p2,p1,'\0',sizeof(STU1)); printf("Roll : %d\n",p2->roll); printf("Name : %s\n",p2->name); printf("Marks : %lf",p2->marks); } Roll : 25 : (null) Marks : 0.000000 Roll : 25 : rohit Marks : 0.000000 Roll : 25 : rohit Marks : 87.430000

(A) Name

(B) Name

(C) Name (D)

Roll : 0 Name : (null) Marks : 0.000000 (E) Compilation error

20.
What will be output if you will execute following c code? #include<stdio.h> #define value 30 int main(){ #if max printf("Defined"); #else printf("Not defined"); #endif return 0; }

(A) Defined (B) Not defined (C) null (D) Run time error (E) Compilation error
Your total score: 0 Total correct answer : 0 Total incorrect answer : 0 Total not attempted questions : 20 1. You haven't tried. Correct answer is :(E) 2. You haven't tried. Correct answer is :(C) 3. You haven't tried. Correct answer is :(E) 4. You haven't tried. Correct answer is :(A) 5. You haven't tried. Correct answer is :(D) 6. You haven't tried. Correct answer is :(B) 7. You haven't tried. Correct answer is :(D) 8. You haven't tried. Correct answer is :(D) 9. You haven't tried. Correct answer is :(C)

10. You haven't tried. Correct answer is :(C) 11. You haven't tried. Correct answer is :(C) 12. You haven't tried. Correct answer is :(A) 13. You haven't tried. Correct answer is :(D) 14. You haven't tried. Correct answer is :(B) 15. You haven't tried. Correct answer is :(A) 16. You haven't tried. Correct answer is :(C) 17. You haven't tried. Correct answer is :(B) 18. You haven't tried. Correct answer is :(A) 19. You haven't tried. Correct answer is :(A) 20. You haven't tried. Correct answer is :(B)

1.
What will be output of following c program? #include<stdio.h> #define max int main(){ printf("%d",max); return 0; }

(A) 0 (B) null (C) Garbage (D) -1 (E) Compilation error

2.
What will be output of following c program? #include<stdio.h> int main(){ for(printf("1");!printf("0");printf("2")) printf("Sachin"); return 0; }

(A) 10sachin2 (B) 10sachin (C) 10sachin210sachin2 (D) 10 (E) Compilation error

3.
What will be output of following c program? #include<stdio.h>

int main(){ int a=5; static int b=a; printf("%d %d",a,b); return 0; } 5 (A) 5 0 (B) 5 null (C) 5 Garbage (D) 5 (E) Compilation error

4.
What will be output of following c program? #include<stdio.h> void main(){ int i; for(i=0;i<5;i++){ int x=0; printf("%d",x); x++; } }

(A) 01234 (B) 001234 (C) 0000 (D) Infinite loop (E) Compilation error

5.
What will be output of following c program? #include<stdio.h> #include<conio.h> void main(){

int a[]={5,10,15}; int i=0,num; num=a[++i]+ ++i+(++i); printf("%d",num); }

(A) 6 (B) 17 (C) 16 (D) 12 (E) Compilation error

6.
What will be output of following c program? #include<stdio.h> #include<conio.h> void main(){ int i=3,val; val=f(i)+ +f(i=1)+ +f(i-1); printf("%d",val); } int f(int num){ return num*5; } (A) 30 (B) 20 (C) 21 (D) 31 (E) Compilation error

7.
What will be output of following c program? #include<stdio.h> #include<conio.h> long fu(int);

char vect[]={1,2,3,4,5}; void main(){ int i=1; i=fu(++i)+ ++vect[++i]+ ++i+fu(i++); printf("%d",i); } long fu(int x){ return x*3; }

(A) 31 (B) 32 (C) 33 (D) 34 (E) Compilation error

8.
What will be output of following c program? #include<stdio.h> #include<conio.h> void main(){ int a,i=4; a=- -i+- -i+- -5; printf("%d %d",a,i); } (A) 13 4 (B) -3 2 2 (C) 7 (D) -13 4 (E) Compilation error

9.
What will be output of following c program? #include<stdio.h> #include<conio.h>

void main(){ int num,a=5; num=---a; printf("%d %d",num,a); }

(A) -4 4 3 (B) 3 (C) -4 5 (D) -5 -5 (E) Compilation error

10.
What will be output of following c program? #include<stdio.h> #include<conio.h> void main(){ int num,a=10; num=a--- -a--; printf("%d %d",num,a); } (A) 0 8 (B) 0 10 (C) 20 8 (D) -1 10 (E) Compilation error

11.
What will be output of following c program? #include<stdio.h> #include<conio.h> void main(){ int z; z=(5,3,2); printf("%d",z);

(A) 5 (B) 3 (C) 2 (D) 10 (E) Compilation error

12.
What will be output of following c program? #include<stdio.h> #include<conio.h> void main(){ int i=5,j=10,num; num=(++i,++j,i+j); printf("%d %d %d",num,i,j); } (A) 17 6 11 6 11 (B) 6 (C) 15 6 11 (D) 15 5 10 (E) Compilation error

13.
What will be output of following c program? #include<stdio.h> #include<conio.h> float avg(float,float,float); void main(){ float p=1,q=2,r=-2,a; a=avg(p,(q=4,r=-12,q),r); printf("%f",a); } float avg(float x,float y,float z){

return (x+y+z)/3;

(A) 0.111111 (B) 1.000000 (C) -0.777777 (D) -1.000000 (E) Compilation error

14.
What will be output of following c program? #include<stdio.h> int main(){ float **(*ptr)[4]=(float **(*)[4])0; ptr+=5; printf("%d %d",ptr,sizeof ptr); return 0; }

(A) 0 2 (B) 5 2 (C) 4 2 (D) 40 2 (E) Compilation error

15.
What will be output of following c program? #include<stdio.h> struct myStruct{ int a; char b; }*ptr; int main(){ struct myStruct ms={400,'A'}; printf("%d %d",ptr->a,ptr->b);

return 0;

(A) 400 A (B) 400 65 (C) 400 97 (D) 0


0 (E) Compilation error

16.
What will be output of following c program? #include<stdio.h> int main(){ float x; x=(int)5.6f*3.2f/sizeof((int)6.6); printf("%f",x); return 0; }

(A) 8.960000 (B) 9.600000 (C) 8.000000 (D) 2.000000 (E) Compilation error

17.
What will be output of following c program? #include<stdio.h> #define plus + #define minus +plus int main(){ long x,i=3; x=+ + i; printf("%ld",x);

return 0;

(A) 4 (B) 3 (C) 0 (D) 6 (E) Compilation error

18.
What will be output of following c program? #include<stdio.h> int main(){ float x; x=(int)1.1,(float)2.2,(int)3.3 ,5.4; printf("%f",x); return 0; }

(A) 1.000000 (B) 5.400000 (C) 2.200000 (D) 3.300000 (E) Compilation error

19.
What will be output of following c program? #include<stdio.h> #include "string.h" typedef struct stu1{ int roll; char *name; double marks; }STU1; typedef struct stu2{ int roll;

char *name; double marks; }STU2; void main(){ STU1 s1={25,"Rohit",87.43},*p1; STU2 *p2; p1=&s1; memcpy(p2,p1,4); printf("Roll : %d\n",p2->roll); printf("Name : %s\n",p2->name); printf("Marks : %lf",p2->marks); }

(A) Roll

: 25 Name : Rohit Marks : 87.430000 Roll : 25 : Rohit Marks : 0.000000 Roll : 0 : Rohit Marks : 87.430000 Roll

(B) Name

(C) Name

: 0 : null Marks : 0.000000 (E) Compilation error

(D) Name

20.
What will be output of following c program? #include "string.h" typedef struct stu1{ char name1[6]; char name2[6]; double marks;

}STU1; void main(){ STU1 s1={"rohit","kumar",87.43},*p1; char *p; p1=&s1; p=memchr(p1,'u',sizeof(STU1)); printf("%s",p); } (A) r (B) rohit (C) umar (D) rohit kumar Compilation error

(E)

Your total score: 0 Total correct answer : 0 Total incorrect answer : 0 Total not attempted questions : 20 1. You haven't tried. Correct answer is :(E) 2. You haven't tried. Correct answer is :(D) 3. You haven't tried. Correct answer is :(E) 4. You haven't tried. Correct answer is :(C) 5. You haven't tried. Correct answer is :(A) 6. You haven't tried. Correct answer is :(B) 7. You haven't tried. Correct answer is :(D) 8. You haven't tried. Correct answer is :(A) 9. You haven't tried. Correct answer is :(E) 10. You haven't tried. Correct answer is :(C) 11. You haven't tried. Correct answer is :(C) 12. You haven't tried. Correct answer is :(A) 13. You haven't tried. Correct answer is :(B) 14. You haven't tried. Correct answer is :(D) 15. You haven't tried. Correct answer is :(D) 16. You haven't tried. Correct answer is :(C) 17. You haven't tried. Correct answer is :(B) 18. You haven't tried. Correct answer is :(A) 19. You haven't tried. Correct answer is :(B) 20. You haven't tried. Correct answer is :(C)

C Linux interview questions and answers


(1)What will be output if you will execute following program by gcc compiler in Linux?

#include<stdio.h> int main(){ int a=5; printf("%d %d %d",a++,a++,++a); return 0; } Output: In LINUX GCC compiler 7 6 8 In TURBO C 7 6 6 Hints: In Turbo c parameter is passed from right to left in printf function but not in the Linux. (2)What will be output if you will execute following program by gcc compiler in Linux? #include<stdio.h> int main(){ int a=5,b=10,c=15,d=20; printf("%d %d %d"); return 0; } Output: In LINUX GCC compiler Garbage values In TURBO C 5 10 15 Hints: Local variables stores in the stack. (3) What will be output if you will execute following program by gcc compiler in Linux? #include<stdio.h> int main(){ int i=5,j=5,y;

int x=++i + ++i + ++i; y=++j + ++j + ++j; printf("%d %d %d %d",x,y,i,j); return 0; } Output: In LINUX GCC compiler 22 22 8 8 In TURBO C 21 24 8 8 (4) What will be output if you will execute following program by gcc compiler in Linux? #include<stdio.h> int main(){ int near *p; int far *q; int huge *r; printf("%d %d %d",sizeof(p),sizeof(q),sizeof(r)); return 0; } Output: In LINUX GCC compiler Compilation error In TURBO C 2 4 4 Note: In Linux there is not any concept of near, far and huge pointers (5) What will be output if you will execute following program by gcc compiler in Linux? #include<stdio.h> int main(){ char *p; int *q; float **r;

printf("%d %d %d",sizeof(p),sizeof(q),sizeof(r)); return 0; } Output: In LINUX GCC compiler 4 4 4 In TURBO C 2 2 2 Hints: size of any type of pointer in Linux is 4 and in turbo c is 2. (6) What will be output if you will execute following program by gcc compiler in Linux? #include<stdio.h> int main(){ short int a=5; int b=5; long int c=5l; float d=5.0f; double e=5.0; long double f=5.0L; char g='5'; printf("Size of short int: %d\n",sizeof(a)); printf("Size of int: %d\n",sizeof(b)); printf("Size of long int: %d\n",sizeof(c)); printf("Size of float: %d\n",sizeof(d)); printf("Size of double: %d\n",sizeof(e)); printf("Size of long double: %d\n",sizeof(f)); printf("Size of char: %d\n",sizeof(g)); return 0; } Output: In LINUX GCC compiler Size of short int: 2 Size of int: 4 Size of long int: 4 Size of float: 4

Size of double: 8 Size of long double: 12 Size of char: 1 In TURBO C Size of short int: 2 Size of int: 2 Size of long int: 4 Size of float: 4 Size of double: 8 Size of long double: 10 Size of char: 1 (7) What will be output if you will execute following program by gcc compiler in Linux? #include<stdio.h> int main(){ int a=300; char *p=(char *)&a; printf("%d\n",*p); printf("%d",*++p); return 0; } Output: In LINUX GCC compiler 44 1 In TURBO C 44 1 (8) What will be output if you will execute following program by gcc compiler in Linux? #include<stdio.h> int main(){ char c='A'; printf("%d %d",sizeof(c),sizeof('A')); return 0; }

Output: In LINUX 1 4 In TURBO C 1 2 (9) What will be output if you will execute following program by gcc compiler in Linux? #include<stdio.h> int main(){ enum color{RED,BLUE,GREEN=-2,YELLOW,PINK}; printf("%d %d",BLUE,PINK); return 0; } Output: In LINUX GCC compiler 1 0 In TURBO C 1 0 (10) What will be output if you will execute following program by gcc compiler in Linux? #include<stdio.h> int main(){ char c=127; printf("%d",++c); printf(" %d",++c); return 0; } Output: In LINUX GCC compiler -128 -127 In TURBO C -128 -127 Hints: char data type cyclic property. (11) What will be output if you will execute following program by gcc compiler in Linux?

#include"stdio.h" struct info1{ char *title; long int size; double grade; }hero1; union info2{ char *title; long int size; double grade; }hero2; int main(){ printf("Size of structure: %d\n",sizeof(hero1)); printf("Size of union: %d",sizeof(hero2)); return 0; } Output: In LINUX GCC compiler Size of structure: 16 Size of union: 8 In TURBO C Size of structure: 14 Size of union: 8 (12) What will be output if you will execute following program by gcc compiler in Linux? #define size(x) (char *)(x+1)-(char *)x #include<stdio.h> int main(){ long int *p; long double *q; printf("Size of long int: %d\n",size(p)); printf("Size of long double: %d",size(q)); return 0; } Output: In LINUX GCC compiler

Size of long Size of long In TURBO C Size of long Size of long

int: 4 double: 12 int: 4 double: 10

(13) What will be output if you will execute following program by gcc compiler in Linux? #include<stdio.h> int main(){ int i=2,j=5,k=3; int a=i&&j>=k; printf("%d",a); return 0; } Output: In LINUX GCC compiler 1 In TURBO C 1 Hints: Any conditional or relational operator returns 1 if condition is true otherwise it returns 0.

Looping questions in c and answers


Looping questions and answers with explanation for written test exam and interview in c programming language (1) What will be output of following c code? #include<stdio.h> extern int x; int main(){ do{ do{ printf("%o",x);

} while(!-2); } while(0); return 0;

} int x=8;

EXPLANATION

Output: 10 Explanation: Here variable x is extern type. So it will search the definition of variable x. which is present at the end of the code. So value of variable x =8 There are two do-while loops in the above code. AS we know do-while executes at least one time even that condition is false. So program control will reach at printf statement at it will print octal number 10 which is equal to decimal number 8. Note: %o is used to print the number in octal format. In inner do- while loop while condition is ! -2 = 0 In C zero means false. Hence program control will come out of the inner do-while loop. In outer dowhile loop while condition is 0. That is again false. So program control will also come out of the outer do-while loop.

Hide

(2) What will be output of following c code? #include<stdio.h> int main(){

int i=2,j=2; while(i+1?--i:j++) printf("%d",i); return 0; }

EXPLANATION

Output: 1 Explanation: Consider the while loop condition: i + 1 ? -- i : + +j In first iteration: i + 1 = 3 (True) So ternary operator will return -i i.e. 1 In c 1 means true so while condition is true. Hence printf statement will print 1 In second iteration: i+ 1 = 2 (True) So ternary operator will return -i i.e. 0 In c zero means false so while condition is false. Hence program control will come out of the while loop.

Hide

(3) What will be output of following c code? #include<stdio.h> int main(){ int x=011,i; for(i=0;i<x;i+=3){ printf("Start "); continue; printf("End"); }

return 0;

EXPLANATION

Output: Start Start Start Explantion: 011 is octal number. Its equivalent decimal value is 9. So, x = 9 First iteration: i = 0 i < x i.e. 0 < 9 i.e. if loop condition is true. Hence printf statement will print: Start Due to continue keyword program control will come at the beginning of the for loop and value of variable i will be: i += 3 i = i + 3 = 3 Second iteration: i = 3 i < x i.e. 3 < 9 i.e. if loop condition is true. Hence printf statement will print: Start Due to continue keyword program control will come at the beginning of the for loop and value of variable i will be: i += 3 i = i + 3 = 6 Third iteration: i = 3 i < x i.e. 6 < 9 i.e. if loop condition is true. Hence printf statement will print: Start Due to continue keyword program control will come at the beginning of the for loop and value of variable i will be: i += 3 i = i + 3 = 9 fourth iteration: i = 6

i < x i.e. 9 < 9 i.e. if loop condition is false. Hence program control will come out of the for loop.

Hide

(4)What will be output of following c code? #include<stdio.h> int main(){ int i,j; i=j=2,3; while(--i&&j++) printf("%d %d",i,j); return 0; }

EXPLANATION

Output: 13 Explanation: Initial value of variable i = 2 j = 2 Consider the while condition : --i && j++ In first iteration: --i && j++ = 1 && 2 //In c any non-zero number represents true. = 1 (True) So while loop condition is true. Hence printf function will print value of i = 1 and j = 3 (Due to post increment operator)

In second iteration: --i && j++ = 0 && 3 //In c zero represents false = 0 //False So while loop condition is false. Hence program control will come out of the for loop.

Hide

(5)What will be output of following c code?

#include<stdio.h> int main(){ static int i; for(++i;++i;++i) { printf("%d ",i); if(i==4) break; } return 0; }

EXPLANATION

Output: 24 Explanation: Default value of static int variable in c is zero. So, initial value of variable i = 0 First iteration: For loop starts value: ++i i.e. i = 0 + 1 = 1 For loop condition: ++i i.e. i = 1 + 1 = 2 i.e. loop condition is true. Hence printf statement will print 2 Loop incrimination: ++I i.e. i = 2 + 1 =3

Second iteration: For loop condition: ++i i.e. i = 3 + 1 = 4 i.e. loop condition is true. Hence printf statement will print 4. Since is equal to for so if condition is also true. But due to break keyword program control will come out of the for loop.

Hide

(6)What will be output of following c code? #include<stdio.h> int main(){ int i=1; for(i=0;i=-1;i=1) { printf("%d ",i); if(i!=1) break; } return 0; }

EXPLANATION

Output: -1 Explanation: Initial value of variable i is 1. First iteration: For loop initial value: i = 0 For loop condition: i = -1 . Since -1 is non- zero number. So loop condition true. Hence printf function will print value of variable i i.e. -1 Since variable i is not equal to 1. So, if condition is true. Due to break keyword program control will come out of the for loop.

Hide

(7)What will be output of following c code? #include<stdio.h> int main(){ for(;;) { printf("%d ",10); } return 0; }

EXPLANATION

Output: Infinite loop Explanation: In for loop each part is optional.

Hide

(8)What will be output of following c code? #include<stdio.h> int r(); int main(){ for(r();r();r()) { printf("%d ",r()); } return 0; } int r(){ int static num=7; return num--; }

EXPLANATION

Output: 5 2 Explanation: First iteration: Loop initial value: r() = 7 Loop condition: r() = 6 Since condition is true so printf function will print r() i.e. 5 Loop incrimination: r() = 4 Second iteration: Loop condition: r() = 3 Since condition is true so printf function will print r() i.e. 2 Loop incrimination: r() = 1 Third iteration: Loop condition: r() = 0 Since condition is false so program control will come out of the for loop.

Hide

(9)What will be output of following c code? #include<stdio.h> #define p(a,b) a##b #define call(x) #x int main(){ do{ int i=15,j=3; printf("%d",p(i-+,+j)); } while(*(call(625)+3)); return 0; }

EXPLANATION

Output: 11 Explanation: First iteration: p(i-+,+j) =i-++j // a##b =i - ++j =15 4 = 11 While condition is : *(call(625)+ 3) = *(625 + 3) Note: # preprocessor operator convert the operand into the string. =*(It will return the memory address of character \0) = \0 = 0 //ASCII value of character null character Since loop condition is false so program control will come out of the for loop.

Hide

(10) #include<stdio.h> int main(){ int i; for(i=0;i<=5;i++); printf("%d",i) return 0; }

EXPLANATION

Output: 6 Explanation: It possible for loop without any body.

Hide

(11)What will be output of following c code? #include<stdio.h> int i=40; extern int i; int main(){ do{ printf("%d",i++); } while(5,4,3,2,1,0); return 0; }

EXPLANATION

Output: 40 Explanation: Initial value of variable i is 40 First iteration: printf function will print i++ i.e. 40 do - while condition is : (5,4,3,2,1,0) Here comma is behaving as operator and it will return 0. So while condition is false hence program control will come out of the for loop.

Hide

(12)What will be output of following c code? #include<stdio.h> char _x_(int,...); int main(){ char (*p)(int,...)=&_x_; for(;(*p)(0,1,2,3,4); ) printf("%d",!+2); return 0; } char _x_(int a,...){ static i=-1; return i+++a; }

EXPLANATION

Output: 0 Explanation: In c three continuous dot represents variable number of arguments. p is the pointer to the function _x_ First iteration of for loop: Initial value: Nothing // In c it is optional Loop condition: (*p)(0,1,2,3,4) = *(&_x_)(0,1,2,3,4) // p = &_x_ = _x_(0,1,2,3,4) //* and & always cancel to each other = return i+++a = return i+ ++a = return -1 + 1 = 0 Since condition is false. But printf function will print 0. It is bug of c language.

Hide

(13)What will be output of following c code? #include<stdio.h> int main(){ int i; for(i=10;i<=15;i++){ while(i){ do{ printf("%d ",1); if(i>>1) continue; }while(0); break; } } return 0; }

EXPLANATION

Output: 1 1 1 1 1 1 For loop will execute six times. Note: continue keyword in do-while loop bring the program its while condition (while(0)) which is always false.

Hide

(14)How many times this loop will execute? #include<stdio.h> int main(){ char c=125; do printf("%d ",c); while(c++); return 0;

EXPLANATION

Output: Finite times Explanation: If we will increment the char variable c it will increment as: 126,127,-128,-127,126 . . . . , 3, 2, 1, 0 When variable c = 0 then loop will terminate.

Hide

(15)What will be output of following c code? #include<stdio.h> int main(){ int x=123; int i={ printf("c" "++") }; for(x=0;x<=i;x++){ printf("%x ",x); } return 0; }

EXPLANATION

Output: c++0 1 2 3 Explanation: First printf function will print: c++ and return 3 to variable i. For loop will execute three time and printf function will print 0, 1, 2 respectively.

S-ar putea să vă placă și