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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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 1410 10 10 10 100 1 2 3 4Compilation 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; }0It will be difference of a and bIt will be addition of a and bCompilation 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; }50153Compilation 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-101Compilation error
What will be output if you will execute following c code? #include<stdio.h> int main(){
int arr[3]={10,20,30}; int x=0; x = ++arr[++x] + ++x + arr[--x]; printf("%d ",x); return 0; }22 turbo c23 linux gcc43 turbo c44 linux gcc
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 linux60 turbo c90 linuxCompilation 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; }910Compilation 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 C4 3 linux GCC3 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 311 310 5Compilation 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; }01-2Compilation 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 61 60 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 1414 1514 1415 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++; printf("%d %d",x,a); return 0; }
5 53 54 55 4
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-101
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 9Infinite loopRun 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.0000001.0000000.350000Compilation 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; } 4814Compilation 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; } 15161718
What will be output if you will execute following c code? #include<stdio.h> int main(){ printf("%c",*"abcde"); return 0; } acbcdeaNULL
What will be output if you will execute following c code? #include<stdio.h> int main(){ printf("%d","abcde"-"abcde"); return 0; }0-11Garbage
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; }EqualNot equalnull
(A)(B)(C)
(D) Garbage
(E) Compilation error
2.
What will be output if you will execute following c code?
What will be output if you will execute following c code?
#include<stdio.h>#define value 30int 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: 0Total correct answer : 0Total incorrect answer : 0Total not attempted questions : 201. 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)
Your total score: 0Total correct answer : 0Total incorrect answer : 0Total not attempted questions : 201. 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 compiler7 6 8In TURBO C7 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 compilerGarbage valuesIn TURBO C5 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?
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 compilerSize of short int: 2Size of int: 4Size of long int: 4Size of float: 4Size of double: 8Size of long double: 12Size of char: 1In TURBO CSize of short int: 2Size of int: 2Size of long int: 4Size of float: 4Size of double: 8Size of long double: 10Size 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 compiler441In TURBO C441
(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 LINUX1 4In TURBO C1 2
(9) What will be output if you will execute following program by gcc compiler in Linux?
In TURBO C-128 -127Hints: 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 compilerSize of structure: 16Size of union: 8In TURBO CSize of structure: 14Size 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 compilerSize of long int: 4Size of long double: 12In TURBO CSize of long int: 4Size of long 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 compiler1In TURBO C1Hints: 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
Output: 10Explanation: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 =8There 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 = 0In C zero means false. Hence program control will come out of the inner do-while loop. In outer do-while 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: 1Explanation:Consider the while loop condition: i + 1 ? -- i : ++jIn first iteration:i + 1 = 3 (True)So ternary operator will return -–i i.e. 1In c 1 means true so while condition is true. Hence printf statement will print 1In second iteration:i+ 1 = 2 (True)So ternary operator will return -–i i.e. 0In c zero means false so while condition is false. Hence program control will come out of the while loop.
Output: Start Start StartExplantion:011 is octal number. Its equivalent decimal value is 9.So, x = 9First iteration:i = 0i < x i.e. 0 < 9 i.e. if loop condition is true.Hence printf statement will print: StartDue to continue keyword program control will come at the beginning of the for loop and value of variable i will be:i += 3i = i + 3 = 3Second iteration:i = 3i < x i.e. 3 < 9 i.e. if loop condition is true.Hence printf statement will print: StartDue to continue keyword program control will come at the beginning of the for loop and value of variable i will be:i += 3i = i + 3 = 6Third iteration:i = 3i < x i.e. 6 < 9 i.e. if loop condition is true.
Hence printf statement will print: StartDue to continue keyword program control will come at the beginning of the for loop and value of variable i will be:i += 3i = i + 3 = 9fourth iteration:i = 6i < 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: 13Explanation:Initial value of variablei = 2j = 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 //FalseSo while loop condition is false. Hence program control will come out of the for loop.
Output: 24Explanation:Default value of static int variable in c is zero. So, initial value of variable i = 0First iteration:For loop starts value: ++i i.e. i = 0 + 1 = 1For loop condition: ++i i.e. i = 1 + 1 = 2 i.e. loop condition is true. Hence printf statement will print 2Loop incrimination: ++I i.e. i = 2 + 1 =3Second 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.
Initial value of variable i is 1.First iteration:For loop initial value: i = 0For 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. -1Since 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.
Output: Infinite loopExplanation: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 2Explanation:First iteration:Loop initial value: r() = 7Loop condition: r() = 6Since condition is true so printf function will print r() i.e. 5Loop incrimination: r() = 4Second iteration:Loop condition: r() = 3Since condition is true so printf function will print r() i.e. 2Loop incrimination: r() = 1Third iteration:Loop condition: r() = 0Since condition is false so program control will come out of the for loop.
Output: 11Explanation:First iteration:p(i-+,+j)=i-++j // a##b=i - ++j=15 – 4= 11While 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 characterSince 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: 6Explanation:It possible for loop without any body.
Initial value of variable i is 40First iteration:printf function will print i++ i.e. 40do - 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.
Output: 0Explanation: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 optionalLoop 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= 0Since condition is false. But printf function will print 0. It is bug of c language.
Output: 1 1 1 1 1 1For 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 timesExplanation:If we will increment the char variable c it will increment as:126,127,-128,-127,126 . . . . , 3, 2, 1, 0When 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 3Explanation: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.