C LANGUAGE QUESTIONS WITH SOLUTION

-->
C language objective types questions and answers with solution or explanation

1.

#include<stdio.h>
int main(){
    int a=0;
    #if (a==0)
         printf("Equal");
    #else if
         printf("Not equal");
    #endif
    return 0;
}

(A) Equal
(B) Not equal
(C) Null
(D) Garbage
(E) Compilation error


Explanation:

Syntax of conditional preprocessor directive (if) is:

#if <Constant expression>
#else
#endif

In this code (a==0) is not constant expression. A constant expression mean expression doesn’t contain any variables.
Note: const int a; Here a is also variable     

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


Explanation:

Here NULL is micro constantan. Value of this symbolic constant is 0 or 0L as defined stdio.h:

#ifndef NULL
#  if defined(__TINY__) || defined(__SMALL__) || defined(__MEDIUM__)
#      define NULL    0
#  else
#      define NULL    0L
#  endif
#endif

So corresponding intermediate file of above code will be:


int main(){
    for(;0;)
    printf("cquestionbank");
    return 0;
}

As you know in c :
0: Means false
Non- zero: True

So for loop should not execute any time because intitial condtion is false. But it is bug of turbo c compiler. Loop will execute one time and it will print : cquestionbank

3.

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

#include<stdio.h>
int main(){
    int x=25;
    if(!!x)
         printf("%d",!x);
    else
         printf("%d",x);
    return 0;
}

(A) 0
(B) 25
(C) 1
(D) -1
(E) 2

Explanation:

! is negation operator.
!x = 0 if x is non-zero number.
!x = 1 if x is equal to zero.
So,
!!x
=! (! x)
=! (! 25)
=! (0)
=1
As we know in c:
Zero: It represents false.
Non-zero: It represents false.
if (1) means condition is true hence if part will execute.
!x =! 1 = 0
Hence printf statement will print zero in the console window.

4.

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

#include<stdio.h>
int main(){
    float a=0.5, b=0.9;
    if(a&&b>0.9)
         printf("Sachin");
    else
         printf("Rahul");
    return 0;
}

(A) Sachin
(B) Rahul
(C) null
(D) Run time error
(E) Compilation error


Explanation:

Consider on the expression:
a && b > 0.9
As we know > operator enjoy higher precedence than && operator.
So expression will be
a && (b > 0.9)
=0.5 && (0.9 > 0.9)
Properties of > (Greater than operator)
a > b returns 1 if a is greater than b.
a > b return 0 if a is less than or equal to b.
So our expression became:
= 0.5 && 0
In c zero represents false and non-zero represents true.
= true && false
= false
= 0
So, if (0) means condition is false. Hence else part will execute.

5.

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

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

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


Explanation:

Consider on expression:
! (! x) && x
=! (! 5) && 5
=! 0 && 5
=1 && 5
=1
So, if condition is true.

6.

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

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

(A) 321 #
(B) 65 A
(C) 321 !
(D) 66 B
(E) Compilation error


Explanation:

Number 321 is beyond the range of char variable ch. So ch will store corresponding cyclic value as shown in the figure:


Equivalent cyclic value of 321 = 321 % 128 = 65
65 is ASCII value of character 'A'

7.

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

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

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


Explanation:

- Operator in c has two forms:
- : Unary minus operator (It requires one operand)
- : Binary minus operator (It requires two operands)
Note: Unary minus operator has higher precedence than binary minus operator.

8.

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

#include<stdio.h>
int main(){
    int x;
    x= -2 + 11 - 7 * 9 % 6 / 12;
    printf("%d",x);
    return 0 ;
}

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


Explanation:

Consider on the expression
x = -2 + 11 - 7 * 9 % 6 / 12
There are seven different type of operator in the above expression:



Operator
Precedence
Associative
-
1
Right to left
* , % , /
2
Left to right
- , +
3
Left to right
=
4
Right to left

First of all unary minus will perform the operation
 x = (-2) + 11 - 7 * 9 % 6 / 12
 Now multiplication operator will perform the operation
 x = (-2) + 11 - (7 * 9) % 6 / 12
 x = (-2) + 11 - 63 % 6 / 12
 After this modular division operator will perform operation:
 x = (-2) + 11 - (63 % 6) / 12
 x = (-2) + 11 - 3  / 12
 After this division will perform the operation:
 x = (-2) + 11 - (3 / 12)
 x = (-2) + 11 - 0
 Now binary plus operator will perform the operation:
 x = ((-2) + 11) - 0
 x = 9 - 0
 Now binary minus operator will perform the operation:
 x = 9
 At the end assignment operator will perform the operation and it will assign 9 to variable x.

9.

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

#include<stdio.h>
int main(){
    int x=3, y=4, z=4;
    printf("%d", (z>=y>=x?100:200));
   
    return 0 ;
}

(A) 100
(B) 200
(C) 0
(D) 1
(E) Compilation error


Explanation:

Consider on arithmetical expression:
z>=y>=x? 100:200
= (z >= y >= x? 100: 200)
= (4 >= 4 >= 3? 100: 200)
= ((4 >= 4) >= 3? 100: 200)
= (1 >= 3? 100: 200)
= (0? 100: 200)
= 200

10.

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

#include<stdio.h>
int main(){
    int a=30, b=40, x;
    x=(a!=10) && (b=50);
    printf("%d",x);
    return 0 ;
}

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


Explanation:

(a!= 10) && (b = 50)
= (30! = 10) && (50)
= 1 && 50
= 1

11.

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

#include<stdio.h>
int main(){
    float x=12.25, y=13.65;
    if(x=y)
         printf("x and y are equal");
    else
         printf("x and y are not equal");
    return 0 ;
}

(A) x and y are equal
(B) x and y are not equal
(C) It will print nothing
(D) Run time error
(E) Compilation error


Explanation:

= is assignment operator.
So, x=y value of variable y will assign to variable x.
    x= 13.65
As we know in c:
Zero represents false.
Any non-zero number represents true.
Hence if (13.65) is true condition.

12.

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

#include<stdio.h>
int main(){
    int i=1, j=1;
    for(;j;printf("%d%d\t",i,j))
         j=i++ <= 5;
    return 0 ;
}
(A) 1 2 3 4 5
(B) 11 12 13 14
(C)
21 31 41 51 61 70
(D) Infinite loop
(E) Compilation error


Explanation:

Syntax of for loop is:

for (exp1;exp2;exp3){
    <Statements>
}
Note: all Exp1, Exp2, Exp3 and <statements> are optional.
Order of evaluation is:
In first cycle
1. Exp1
2. Exp2
3. <Statement>
4. Exp2
In rest cycles
1. Exp2
2. <Statements>
3. Exp3
Now we come to our question.
In first cycle
1. It has not exp1
2. Exp2 is j which is equal to 1 i.e. condition is true
3. <Statements> is j=i++ <= 5
j = 1 <= 5
j = 1
i = 2 (Due to postfix increment operator)
4. Exp3 is printf statement which will print i as 2 and j as 1

In second cycle
1. Exp2 is j which is equal to 1 i.e. condition is true
2. <Statements> is j=i++ <= 5
j = 2 <= 5
j = 1
i = 3 (Due to postfix increment operator)
3. Exp3 is printf statement which will print i as 3 and j as 1

In third cycle
1. Exp2 is j which is equal to 1 i.e. condition is true
2. <Statements> is j=i++ <= 5
j = 3 <= 5
j = 1
i = 4 (Due to postfix increment operator)
3. Exp3 is printf statement which will print i as 4 and j as 1

In fourth cycle
1. Exp2 is j which is equal to 1 i.e. condition is true
2. <Statements> is j=i++ <= 5
j = 4 <= 5
j = 1
i = 5 (Due to postfix increment operator)
3. Exp3 is printf statement which will print i as 4 and j as 1

In fifth cycle
1. Exp2 is j which is equal to 1 i.e. condition is true
2. <Statements> is j=i++ <= 5
j = 5 <= 5
j = 1
i = 6 (Due to postfix increment operator)
3. Exp3 is printf statement which will print i as 6 and j as 1

In sixth cycle
1. Exp2 is j which is equal to 1 i.e. condition is true
2. <Statements> is j=i++ <= 5
j = 6 <= 5
j = 0
i = 7 (Due to postfix increment operator)
3. Exp3 is printf statement which will print i as 7 and j as 0

In seventh cycle
1. Exp2 is j which is equal to 0 i.e. condition is false. So loop will terminate.

13.

What function is used to release the allocated memory space?

(A) deallocate()
(B) release ()
(C) free ()
(D) drop()
(E) empty ()


Explanation:
**

14.

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


Explanation:
**

15.

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


Explanation:
**

16.

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


Explanation:

a[++i+a[++i]]+a[++i]

As we know in any expression pre increment operator first increment the value of variable  then final value assign to all variable.
Variable i has incremented three times so final value of I will be 3. Now 3 will be assigned to all i. So expression will be:
= a [3+a [3] +a [3]
= a [3 + 3] + 3
=a [6] + 3
= 6 + 3
=9

17.

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


Explanation:

Consider on the expression:
sizeof f(i)+ +f(i=1)+ +f(i-1)  //i=3
= sizeof f(3)+ +f(i=1)+ +f(i-1)
= sizeof 15 + +f(i=1)+ +f(i-1) 
= 2 + +f(i=1)+ +f(i-1)
= 2 + f(1) +  +f(0)  //i=1
= 2 + 5 + f(0)
= 7 + 0
= 7

18.

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


Explanation:
**

19.

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


Explanation:

ASCII value of character ‘a’ is 97.

20.

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

#include<stdio.h>
int 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


Explanation:

Post decrement operator first assign the value then it decrements the value. 
num = -a—-
num = -5
Now value of variable a will be 4.

99 comments:

  1. Excellent work, Mr. Blogger. I am a fresher student. I am trying to understand C language. This post is MOST HELPFUL..!!!

    God bless you. Please keep up the good work

    ReplyDelete
  2. Write a program in C to delete all chars in a Text file before encountering word "WORD". after encountering word "WORD, copy 2 to 100 long long ints from file seperated by commas until you get word "/WORD"

    Adam

    ReplyDelete
  3. now i m very happy..it is useful to improve my c laguage skills... i can feel very happy to solve these questions.... thank u
    sanjapu shiva

    ReplyDelete
  4. where r the damn answers..
    how would we telly our answers...

    ReplyDelete
  5. great job!!
    really good questions! these really helped to clear up my concepts!
    Thanks!

    ReplyDelete
  6. how the answer of question number 19 is 10?
    Please Explain.

    ReplyDelete
  7. where are the answers given.can u explain answers of qno 23 and 39.?

    ReplyDelete
  8. nice questions,but did not know how to fuck u?

    ReplyDelete
  9. thank you sir, iam shashank tomar mca student,you are giving good questions.

    ReplyDelete
  10. sir these problems are very good . everyone can understand about c LAn through it.

    ReplyDelete
  11. but their is no programs like to see reverse programs etc so plz also add sum prgrm like dnt ask vch one is correct out put

    ReplyDelete
  12. where is answer?????????????

    ReplyDelete
  13. very easy questions .include pointers and linked lists.then may b cmplcatd

    ReplyDelete
  14. where r answers?

    ReplyDelete
  15. no use without answers!!!!!!!!!!!!

    ReplyDelete
  16. no use without answers!!!!!!!!!!!!

    ReplyDelete
  17. u r correct re.....no use without answers!!!!!

    ReplyDelete
  18. dam it this are easy ones, u can give bit complicated, if u add pointers and linked lists it will be useful

    ReplyDelete
  19. Anamika chhabra5/24/10, 2:24 PM

    answer of 16th ques would be 6 , not 9...

    ReplyDelete
  20. i am very happy you improve my c laguage skills... i can feel very happy to solve these questions.... thank u

    ReplyDelete
  21. Hi Anamika,
    I checked answer again. Answer of question 16. is correct. Please check the explanation

    ReplyDelete
  22. very good bosss

    ReplyDelete
  23. give the ans of 18 q

    ReplyDelete
  24. thanks...
    it revised me all things that i have read in the start of semester...
    and hopefully it will help me in my terminal!

    ReplyDelete
  25. hi sir
    these questions are very good and helpful in understanding C
    ur explanation way is great

    ReplyDelete
  26. sunil kumar verma6/4/10, 10:58 PM

    thanks sir
    i m very happy to see this problem.

    ReplyDelete
  27. Hi sir...
    Really its a awesome explanation about this C language...

    ReplyDelete
  28. good job bro....can u think abt that...

    printf("%d", (z>=y>=x?100:200));

    if the z>=y>=x is true than it print outs 100 not 200

    ReplyDelete
  29. Hi Aman verma,
    Yes if z>=y>=x is true then it will print 100.But in questions 9, it is not true. Please look the explanation carefully.
    Hints: Read about return type of conditional operators,precedence and associate of operators.

    ReplyDelete
  30. thanx a lot i was searching on net in no.s of site but no one was better then this

    ReplyDelete
  31. 18 is not correct.. its actually "Lvalue required error" Compilation error

    ReplyDelete
  32. hi all q r good

    ReplyDelete
  33. Thank u so much..
    this s very useful to me..

    ReplyDelete
  34. Thnxxxxx
    These ques r very good and hardddd,.....

    ReplyDelete
  35. Good Questions!!!!!!
    You cleared my fundamentals
    Thanks alot
    Its good for a beginner

    ReplyDelete
  36. Request: Please provide the explanation of 14 & 15 questions.

    Thanks

    ReplyDelete
  37. good question......

    ReplyDelete
  38. how the key-word in c language

    ReplyDelete
  39. good work boss.
    i enjoyed the questions :)

    -Rahil

    ReplyDelete
  40. work done is good...
    but there are some mistakes in answers...
    question collection reflects some good concepts...

    ReplyDelete
  41. hi.....
    nice Q...but some Qustion are vry difficult/

    ReplyDelete
  42. really great-:)

    ReplyDelete
  43. Thanks dear, this blog is good to clear many concept.

    ReplyDelete
  44. this are really good cleard my concept

    ReplyDelete
  45. i want to mora and more Question
    ya to baccho wala khal tha.............
    plz give high level Question sirrrrrrrrrrrrrrr

    ReplyDelete
  46. Superb one's No words seriously

    ReplyDelete
  47. these questions are very good and helpful in understanding C
    ur explanation way is great

    ReplyDelete
  48. great job
    keep it up

    ReplyDelete
  49. ya its very useful to me to improve my skills in c...
    any way thanks for ur work blogger

    ReplyDelete
  50. good work..

    ReplyDelete
  51. importance of return type in main function

    ReplyDelete
  52. very very very usefull this type of obj qns in interview
    thanks anamika

    ReplyDelete
  53. thanks you are doing very good work for the student it is very helpful to every student.

    ReplyDelete
  54. its really a nice one which developes our skill in c programming......
    so i am always grateful to u

    ReplyDelete
  55. Thanks a lot! I enjoyed doing it!

    ReplyDelete
  56. Some of the questions have answers, but it won't compile in a gcc compiler. You should consider that!

    ReplyDelete
  57. you are very interesting one.i like you ,sir.

    ReplyDelete
  58. thanks.....

    ReplyDelete
  59. thank you very much ,you have done very good job,
    blessings in tons, may god bless you.

    if you have any doubt or query
    please contact me at geniushkg@gmail.com

    ReplyDelete
  60. This comment has been removed by the author.

    ReplyDelete
  61. 17 th is not clear plzzzzzzz explain......

    ReplyDelete
  62. SIR KEEP IT UP THAT'S THE NICE WAY TO BUILT KNOWLEDGE OF A PROGRAMMER.

    AARAV

    ReplyDelete
  63. Good post but you need much care for answers

    ReplyDelete
  64. rathod sanjay4/21/11, 12:00 AM

    I improve best programming knowledge using this quiz....

    ReplyDelete
  65. correct ra 16 que ans is 6

    ReplyDelete
  66. Here we can find multiple choice, this is great way and you are given to this information to us, help of this e book we study and gain some knowledge.

    ReplyDelete
  67. ans. of 18th question is wrong.it would be E option.

    ReplyDelete
  68. hey its to good,thx a lot

    ReplyDelete
  69. AWESOME WORK DUDE......bless you!!

    ReplyDelete
  70. how to make a quiz programme in c language ?

    ReplyDelete
  71. VERY GOOD QUESTION AND ANSWER BUT I NEED MORE TRICKY QUESTION. PLZZZZZZZZZZZZZZZZZZ

    ReplyDelete
  72. vasanthakumar7/16/11, 2:03 PM

    good work.

    ReplyDelete
  73. i=0;
    i= i++ + ++i + i++ + i;

    output=?

    ReplyDelete
  74. A very good work u r doing.Thanks .............
    plz. post more on pointers

    ReplyDelete
  75. pleas post more question

    ReplyDelete
  76. #include
    #include
    void main(){
    int i;
    (i=8)+=1;
    printf("%d",i);
    }


    In your answers its showing 34. But I executed this and got output 9..

    ReplyDelete
  77. dear dasu answer is correct 9

    ReplyDelete
  78. thanks...........my improve c....so thanks very good question

    ReplyDelete
  79. # include
    main()
    {
    int a ;
    if(a = 0)
    printf("true");
    else
    printf("false");
    }

    output: true.

    why the output is true when the value of a is 0

    ReplyDelete
  80. you should write if(a==0)
    a=0 means 0 is assigning to variable a and in c 0 means false and any non-zero values means true

    ReplyDelete
  81. Can I know the answer to:
    Write a program that reads a list of integers from the keyboard and creates the following information:
    a.)Finds and prints the sum and the average of the integers.
    b.)Find and prints the smallest integer.
    c.)Prints a Boolean(true or false) if some of them are less than 20.
    d.)Prints a Boolean (true or false) if all of them are between 10 and 90.
    Please help me out to solve this Problem :)

    ReplyDelete
  82. #include
    #include
    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);
    }



    why its ans is 9 i didn't get it

    ReplyDelete
  83. must say a good work indeed...
    your qs have really made me feel more comfortable....
    thanks!!

    ReplyDelete
  84. sir in question number 17 f shoul have a prototype ......



    in question 18 the answer will be 9 not 34

    ReplyDelete
  85. what the hell? in question 3, the ELSE part is executed which prints out the value of x. the value of x is still 25 and is unchanged by operator used inside the condition to be verified in the IF statement.

    ReplyDelete
  86. hi sir i am rohit you are giving good questions .thank
    s for this.....

    ReplyDelete
  87. these questions as well as answers are very much useful to freshers... thank u...keep posting further..

    ReplyDelete
  88. your explanation are very well. nice work done

    ReplyDelete
  89. Superb material

    i have a doubt .

    in an expression , operation will be left to right or right to left ?

    ReplyDelete
    Replies
    1. it depends on the order of precedence...
      like:
      1. ( ) left to right
      2. ++ -- ! -(unary minus) right to left
      3. * / % left to right

      etc....

      Delete
  90. thanq admin... you made me know some unknown facts in 'C'

    ReplyDelete
  91. these questions are really awsome

    ReplyDelete
  92. Can u mail these question to me plz..
    harish.wheeler@gmail.com

    ReplyDelete