C Questions
C Questions
h> int main() { register a,b,x; scanf("%d %d",&a,&b); x=a+~b; printf("%d",x); return 0; }
(A) 0 (B) It will be difference of a and b (C) It will be addition of a and b (D) Compilation error Explanation: Register variables are stored in CPU. So it has no memory address. Hence it is incorrect to write &a. Ans:D
2.What will be the output if you execute the following c code? #include<stdio.h> int main(){ int x,a=3;
Explanation: Consider on expression: + +a Here both + are unary plus operation. So = + +a+ + +a+ + +5; = + +3+ + +3+ + 5 = 3+ 3+ 5 = 11 Ans: B
3.What will be the output if you compile and execute the following c code? void main() { int i=10; static int x=i;
if(x==i) printf("Equal"); else if(x>i) printf("Greater than"); else printf("Less than"); } (a) Equal (b) Greater than (c) Less than (d) Compiler error Answer: (d) Explanation: Static variables are load time entity while auto variables are run time entity. We cannot initialize any load time variable by the run time variable. In this example i is run time variable while x is load time variable.
4. What will be the output if you compile and execute the following c code? void main() { printf("%s","Code" "Crackers"); } (a) Code (b) Crackers (c) Code Crackers
5. What will be output if you will compile and execute the following c code? void main() { static main; int x; x=call(main); clrscr(); printf("%d ",x); getch(); } int call(int address) { address++; return address; } (a) 0 (b) 1 (c) Garbage value (d) Compiler error Answer: (b) Explanation: As we know main is not keyword of c but is special type of function. Word main can be name variable in the main and other functions.
6. What will be the output if you compile and execute the following c code? int extern x; void main() { printf("%d",x); x=2; getch(); } int x=23; (a) 0 (b) 2 (c) 23 (d) Compiler error Answer: (c) Explanation: extern variables can search the declaration of variable any where in the program. 7.What will be the output if you compile and execute the following c code? #define x 5+2 void main() { int i; i=x*x*x; printf("%d",i); }
Ans:C
8.What will be the output if you compile and execute the following c code? void main() { char c=125; c=c+10; printf("%d",c); } A)135 B)-121 C)-8 D)Error Ans:B
9.What will be the output if you compile and execute the following c code? void main() { int a=10; printf("%d %d %d",a,a++,++a); } A) 10 11 12 B) 12 11 11 C) 12 10 10 D) 10 10 12 Ans: B
10.What will be the output if you compile and execute the following c code? void main() { char *str="Hello world"; printf("%d",printf("%s",str)); } A) 10Hello world B) 11Hello world C) Hello world10 D) Hello world11 Ans:D
11.What will be the output if you compile and execute the following c code? #include "stdio.h" #include "string.h" void main() { char *str=NULL; strcpy(str,"codecrackers"); printf("%s",str); } A)codecrackers B)codecrackers\0 C)Null D)Compiler Error Ans:C
12.What will be output if you will compile and execute the following c code? #include "stdio.h" #include "string.h" void main() { char c='\08'; printf("%d",c); }
A) 8 B) 8 C) 9 D) Error Ans:D
13.What is the error in following declaration? struct outer { int a; struct inner { char c; }; }; A) B) C) D) Nesting of structures is not allowed in C It is necessary to initialize the member variable Outer structure must have a name No error
14.What will be the output if you compile and execute the following c code?
void main() { int array[]={10,20,30,40}; printf("%d",-2[array]); } A)-20 B)Garbage Value C)-30 D) Error Ans: -30
15.What will be the output if you compile and execute the following c code? #define max 5; void main() { int i=0; i=max++; printf("%d",i++); } (A) 5 (B) 6 (C) 0x (D) Error Ans:D 16.In the following program how long will the for loop get executed?
int main() { int i; for(;scanf("%d",&i);printf("%d\n",i)); return 0; } A. For loop will not get executed at all. B. For loop will get executed only once. C. For loop will get executed infinite times. D. None Of The Above. Answer: C
17. void main() { int d=5; printf("%f",d); } Answer: Error. Cannot expand type.
18.Which of the following is the correct output for the program given below? int main() { static char *s[]={black,white,pink,violet}; char** ptr[]={s+3,s+2,s+1,s},***p; p=ptr; ++p; printf(%s,**p+1); return 0; }
{ enum color{red,green,blue}; typedef enum color mycolor; mycolor m; printf(%d\n,m); return 0; } A) 1 B) 0 C) 2 D) Error
Ans:B
20.What is the output of the following snippet? void main() { int k=ret(sizeof(float)); printf("\n here value is %d",++k); } int ret(int ret) { ret += 2.5; return(ret);