c quiz questions with answers


C language quiz questions and answers with explanation  


(1) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  int a=5;
  float b;
  printf("%d",sizeof(++a+b));
  printf(" %d",a);
  return 0;
}


(a)2 6
(b)4 6
(c)2 5
(d)4 5
(e)Compiler error

Output: (d)
Explanation:
++a +b
=6 + Garbage floating point number
=Garbage floating point number
//From the rule of automatic type conversion
Hence sizeof operator will return 4 because size of float data type in c is 4 byte.
Value of any variable doesn’t modify inside sizeof operator. Hence value of variable a will remain 5.


(2) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  char *str;
  scanf("%[^\n]",str);
  printf("%s",str);
  return 0;
}

(a)It will accept a word as a string from user.
(b)It will accept a sentence as a string from user.
(c)It will accept a paragraph as a string from user.
(d)Compiler error
(e)None of above

Output: (b)
Explanation:
Task of % [^\t] is to take the stream of characters until it doesn’t receive new line character ‘\t’ i.e. enter button of your keyboard.

(3) What will be output if you will compile and execute the following c code?


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


(a)5 garbage garbage
(b)5 0 0
(c)5 null null
(d)Compiler error
(e)None of above

Output: (b)
Explanation:
Storage class of an array which initializes the element of the array at the time of declaration is static. Default initial value of static integer is zero.


(4) What will be output if you will compile and execute the following c code?


#include<stdio.h>
void call(int,int,int);
int main(){
  int a=10;
  call(a,a++,++a);
  return 0;
}

void call(int x,int y,int z){
  printf("%d %d %d",x,y,z);
}


(a)10 10 12
(b)12 11 11
(c)12 12 12
(d)10 11 12
(e)Compiler error

Output: (b)
Explanation:
Default parameter passing scheme of c is cdecl i.e. argument of function will pass from right to left direction.

First ++a will pass and a=11
Then a++ will pass and a=11
Then a will pass and a=12


(5) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  int x=5,y=10,z=15;
  printf("%d %d %d");
  return 0;
}


(a)Garbage Garbage Garbage
(b)5 10 15
(c)15 10 5
(d)Compiler error
(e)Run time error

Output: (c)
Explanation:
Auto variables are stored in stack as shown in following figure.




Stack follow LIFO data structure i.e. last come and first out. First %d will print then content of two continuous bytes from the top of the stack and so on.
(6) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  register int i,x;
  scanf("%d",&i);
  x=++i + ++i + ++i;
  printf("%d",x);
  return 0;
}


(a)17
(b)18
(c)21
(d)22
(e)Compiler error

Output: (e)
Explanation:
In c register variable stores in CPU it doesn’t store in RAM. So register variable have not any memory address. So it is illegal to write &a.


(7) What will be output if you will compile and execute the following c code?


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


(a)5 10
(b)6 11
(c)5 11
(d)6 10
(e)Compiler error

Output: (c)
Explanation:
Default storage class of local variable is auto. Scope and visibility of auto variable is within the block in which it has declared. In c, if there are two variables of the same name then we can access only local variable. Hence inside the inner block variable a is local variable which has declared and defined inside that block. When control comes out of the inner block local variable a became dead.


(8) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  float f=3.4e39;
  printf("%f",f);
  return 0;
}


(a)3.4e39
(b)3.40000…
(c)+INF
(d)Compiler error
(e)Run time error

Output: (c)
Explanation:
If you will assign value beyond the range of float data type to the float variable it will not show any compiler error. It will store infinity.


(9) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  enum color{
    RED,GREEN=-20,BLUE,YELLOW
  };
  enum color x;
  x=YELLOW;
  printf("%d",x);
  return 0;
}


(a)-22
(b)-18
(c)1
(d)Compiler error
(e)None of above

Output: (b)
Explanation:
Default value of enum constant = value of previous enum constant +1
Default value of first enum constant=0
Hence:
BLUE=GREEN+1=-20+1=-19
YELLOW=BLUE+1=-19+1=-18


(10) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  asm{
    mov bx,8;
    mov cx,10
    add bx,cx;
  }
  printf("%d",_BX);
  return 0;
}

(a)18
(b)8
(c)0
(d)Compiler error
(e)None of above

Output: (a)
Explanation:
asm keyword is used to write assembly language program in c. mov command stores the constants in the register bx, cx etc. add command stores the content of register and stores in first register i.e. in bx.


(11) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  enum xxx{
     a,b,c=32767,d,e
  };
  printf("%d",b);
  return 0;
}


(a)0
(b)1
(c)32766
(d)Compiler error
(e)None of above

Output: (d)
Explanation:
Size of enum constant is size of sign int. Since value of c=32767. Hence value of d will be 32767+1=32768 which is beyond the range of enum constant.


(12) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  signed int a=-1;
  unsigned int b=-1;
  if(a==b)
    printf("%d %d",a,b);
  else
    printf("Not equal");
  return 0;
}


(a)-1 -1
(b)-1 32767
(c)-1 -32768
(d)Not equal
(e)Compiler error






Output: (a)
Explanation:



(13) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  float f=5.5f;
  float x;
  x=f%2;
  printf("%f",x);
  return 0;
}


(a)1.500000
(b)1.000000
(c)5.500000
(d)Compiler error
(e)None of above

Output: (d)
Explanation:
(14) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  int a=-20;
  int b=-3;
  printf("%d",a%b);
  return 0;
}


(a)2
(b)-2
(c)18
(d)-18
(e)Compiler error


Output: (b)
Explanation:
Sign of resultant of modular division depends upon only the sign of first operand.
(15) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  char c='0';
  printf("%d %d",sizeof(c),sizeof('0'));
  return 0;
}


(a)1 1
(b)2 2
(c)1 2
(d)2 1
(e)None of above

Output: (c)



(16) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  char *url="c:\tc\bin\rw.c";
  printf("%s",url);
  return 0;
}


(a)c:\tc\bin\rw.c
(b)c:/tc/bin/rw.c
(c)c: c inw.c
(d)c:cinw.c
(e)w.c in

Output: (e)
Explanation:
1. \t is tab character which moves the cursor 8 space right.
2. \b is back space character which moves the cursor one space back.
3. \r is carriage return character which moves the cursor beginning of the line.



(17) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  goto abc;
  printf("main");
  return 0;
}

void dispaly(){
  abc:
    printf("display");
}

(a)main
(b)display
(c)maindisplay
(d)displaymain
(e)Compiler error

Output: (e)
Explanation:
Label of goto cannot be in other function because control cannot move from one function to another function directly otherwise it will show compiler error: unreachable label


(18) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  int i=3;
  if(3==i)
    printf("%d",i<<2<<1);
  else
    printf("Not equal");
}


(a)1
(b)48
(c)24
(d)Not equal
(e)Compiler error

Output: (c)
Explanation:
Associative of bitwise left shifting operator is left to right. In the following expression:
i<<2<<1
There are two bitwise operators. From rule of associative leftmost operator will execute first.
i <<><<>
After execution of leftmost bitwise left shifting operator:
so i=i*pow(2,2)
=3*


(19) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  int x=2,y=3;
  if(x+y<=5)
    printf("True");
  else
    printf("False");
}


(a)True
(b)False
(c)Compiler error: Lvalued required
(d)Compiler error: Invalid expression
(e)None of above

Output: (a)
Explanation:
Expression x+y<=5
=> 2+3 <=5
=> 5<=5 is true because 5 is either greater than 5 or equal to 5.
(20) What will be output if you will compile and execute the following c code?


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


(a)5
(b)6
(c)0
(d)Compiler error
(e)None of above

Output: (d)
Explanation:
We cannot modify the const variable by using increment operator.

(21) What will be output if you will compile and execute the following c code?


#include<stdio.h>

int main(){
  int i=11;
  int const * p=&i;
  p++;
  printf("%d",*p);
  return 0;
}


(a)11
(b) 12
(c)Garbage value
(d)Compiler error
(e)None of above

Output: (c)
Explanation:
In the following line:
int const * p=&i;
*p i.e. content of p is constant pointer p is not constant pointer. So we can modify the pointer p. After incrementing the pointer it will point next memory location and its content will any garbage value.



Note: We have assumed arbitrary memory address.

To make pointer p as constant pointer write:
int const * const p=&i;
(22) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  int a=15,b=10,c=5;
  if(a>b>c )
    printf("Trre");
  else
    printf("False");
  return 0;
}


(a)True
(b)False
(c)Run time error
(d)Compiler error
(e)None of above

Output: (b)
Explanation:
Relation operator in c always returns 1 when condition is true and 0 when condition is false. So in the following expression
a > b > c
Associative of relational operators are left to right order of execution will be following manner:




Hence in this expression first solve bolded condition: a > b > c
Since condition a>b is true so result will be 1. Now expression became:
1 > c
Since this condition is false so result will be 0. Thus else part will execute.
(23) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  float f;
  f=3/2;
  printf("%f",f);
  return 0;
}


(a)1.5
(b)1.500000
(c)1.000000
(d)Compiler error
(e)None of above

Output: (c)
Explanation:
In the following expression:
f=3/2 both 3 and 2 are integer constant hence its result will also be an integer constant i.e. 1.
(24) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  int a=sizeof(a);
  a=modify(a);
  printf("%d",a);
  return 0;
}

int modify(int x){
  int y=3;
  _AX=x+y;
  return;
}

(a)2
(b)3
(c)5
(d)Garbage value
(e)None of above

Output: (c)
Explanation:
_AX is register pseudo variable. It stores return type of function.
(25) What will be output if you will compile and execute the following c code?

#define PRINT printf("c");printf("c++");
int main(){
  float a=5.5;
  if(a==5.5)
    PRINT
  else
    printf("Not equal");
  return 0;
}

(a)c c++
(b)Not equal
(c)c
c++
(d)Compiler error
(e)None of above

Output: (d)
Explanation:
First see intermediate file:
try.c 1:
try.c 2: int main(){
try.c 3: float a=5.5;
try.c 4: if(a==5.5)
try.c 5: printf("c");printf("c++");
try.c 6: else
try.c 7: printf("Not equal");
try.c 8: }
try.c 9: return 0;
try.c 10:


If there are more than one statement in if block then it is necessary to write inside the { } otherwise it will show compiler error: misplaced else
Preprocessor tutorial with examples.



(26) What will be output if you will compile and execute the following c code?


#include<stdio.h>
int main(){
  int array[2][2][3]={0,1,2,3,4,5,6,7,8,9,10,11};
  printf("%d",array[1][0][2]);
  return 0;
}


(a)4
(b)5
(c)6
(d)7
(e)8

Output: 8
Explanation:
array[1][0][2] means 1*(2*3)+0*(3)+3=9th element of array starting from zero i.e. 8.

55 comments:

Anonymous said...

very good set of questions...it made me realize that i still need to study c alot..

Anonymous said...

Good questions. The blog is very knowledgeable. Please post explanations for preprocessor questions.
Thank you.

Anonymous said...

I THINK THESE ARE VERY EFFECTIVE

Unknown said...

superb questions........

Anonymous said...

Good questions....I think i need 2 work hard.....

Anonymous said...

great questions......thanks for designing this blog for learners like us......it is helping a lot...

Anonymous said...

commendable job done by you guyz... it helps in winning a quiz also.... all tx to you...keep up the gr8 work....

Anonymous said...

thanx...

nagarjuna said...

superb questions

ankit sarkar said...

Good questions on c...

Unknown said...

wonderful effort...but need to take some cautions..while printing the outputs..

otherwise...its a nice work done..keep it up...
:)

Anonymous said...

excellent question nd i feel tht still i need more to learn

Unknown said...

this is good for knowladge................................thanks

VIVEK DWIVEDI said...

gud................i lyk it

Unknown said...

the output wat i'm gettng is same as..12 11 11

Anonymous said...

these helps us a lot ...good work.......im very thankful to this blog

Anonymous said...

very good questions.. it will help me to improve my knowledge in c.. thank you...

Prashant Garg said...

better collection of question,,,,,,and answer method of these question is also very good.
it is best one website for enhance ur c skill........

Anonymous said...

very good question.....still i think u need to work hard....as this was below my level...appu(GATE TOPPER 2010 FROM CS)

Anonymous said...

excellent programming practice
rajni gupta

Anonymous said...

nice... its useful for who are all preparing for interviews... thanks a lot...

Anonymous said...

good questions

Anonymous said...

nic questions...

Anonymous said...

thnk u soo much it helps me 2 knw more abt c lang.

Anonymous said...

very very good questions
Thanks a lot

Anonymous said...

good collection of questions

Anonymous said...

nice set of questions.specially the escape chracter question(c:\\.....) was superb.
if possible send some more questions to my mail.
bbsm420024@gmail.com

Anonymous said...

superb collection still post som new ones

Anonymous said...

good collection of questions with nice explanation...keep it up....

Anonymous said...

thank u very much giving us a helping hand

Anonymous said...

NICE ,IT IS VERY USEFULL

Srimithra said...

hey can any one tell me why the output of the following program is 1000

#include
int main()
{
int i;
i=f();
printf("%d",i);
}
int f()
{
_AX=1000;
}

Anonymous said...

very nice questions.. i need to work hard

Anonymous said...

_AX is register pseudo variable. It stores return type of function.therefore in function f()AX returns value 1000,which in return store in i

Unknown said...

Good questions.Try to post more questions.Good job

mkv said...

very good set of question with nice explanation!

mkv said...

What will be output if you will compile and execute the following c code?
#include
#include
int main()
{
char str[]="India\0BX\0";
printf("%s",str);
return 0;
}

dharmendra said...

very useful questions....good work

Shashank Jain said...

There is error in 11th Question. answer will be "1" not compile error. you are printing b not d.. check it once more !!

Anonymous said...

this AX is name for Accumulator.. generally used in turbo C if you compile this code in GCC compiler.. it will be simple error. but in turbo C case. the Accumulator has 1000 in it.. and that value will be taken by i, the variable who called it. and answer will be 1000.

Anonymous said...

thank you it was really useful

Rohan Rawlani said...

good selection of questions..
it will probably cover the whole c language.

Unknown said...

nice questions.,
good work buddy,., !!

Anonymous said...

nice question,specially the explanation given is very easy and understandable,do post some more

Anonymous said...

good questions

Anonymous said...

plzz give more questions with answers

Anonymous said...

so..nicely written thank u..so much

mangala said...

nice to solve, it recollects our knowledge

Anonymous said...

good work guys.......keep it up :)

Unknown said...

knowledgeable and very short programs which will increase capacity of C language programming...
thank u

Anonymous said...

ITS NOT USEFUL FOR US
THANKING YOU!
YOUR FAITHFULLY
MT. NICHOLAS

Anonymous said...

ITS NOT USEFUL FOR US
THANKING YOU!
YOUR FAITHFULLY
MT. NICHOLAS

Unknown said...

Perfect questions
SOME TIME HARD
BUT NICE

Unknown said...


What will be output if you will compile and execute the following c code #include int main(){ int a=-20; int b=-3; printf("%d",a%b); return 0; }

Modxon said...

For Complete explanation of C++ please visit - SchoolingAxis