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.

105 comments:

rashgom said...

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

Anonymous said...

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

shiva said...

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

Anonymous said...

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

Anonymous said...

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

yogesh_madhuri said...

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

yogesh_madhuri said...

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

Anonymous said...

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

Avi said...

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

Unknown said...

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

Anonymous said...

where is answer?????????????

Anonymous said...

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

Anonymous said...

where r answers?

Anonymous said...

no use without answers!!!!!!!!!!!!

Anonymous said...

no use without answers!!!!!!!!!!!!

Anonymous said...

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

Unknown said...

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

Anamika chhabra said...

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

gagan said...

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

Priyanka kumari said...

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

RameshReddy said...

very good bosss

ram said...

give the ans of 18 q

bohemia said...

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

Anonymous said...

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

sunil kumar verma said...

thanks sir
i m very happy to see this problem.

Anonymous said...

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

a said...

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

Priyanka kumari said...

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.

uday said...

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

Anonymous said...

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

Anonymous said...

LOL

pravin said...

hi all q r good

Unknown said...

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

amit tomar said...

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

Anonymous said...

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

Anonymous said...

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

Thanks

madhav said...

good question......

Unknown said...

how the key-word in c language

Rahil said...

good work boss.
i enjoyed the questions :)

-Rahil

sharad said...

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

Unknown said...

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

nitesh said...

really great-:)

adil said...

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

Unknown said...

this are really good cleard my concept

c said...

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

Mithun said...

Superb one's No words seriously

saurabh said...

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

Anonymous said...

great job
keep it up

yalu said...

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

Anonymous said...

good work..

Anonymous said...

importance of return type in main function

kiran said...

very very very usefull this type of obj qns in interview
thanks anamika

milan said...

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

Unknown said...

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

Tz Of Death said...

Thanks a lot! I enjoyed doing it!

prem said...

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

Unknown said...

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

Anonymous said...

thanks.....

geniushkg said...

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

SINTHIA said...
This comment has been removed by the author.
Anonymous said...

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

Aarav koshik said...

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

AARAV

jithin jose said...

Good post but you need much care for answers

rathod sanjay said...

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

Nirwair said...

thanx,good work

Meenu said...

thank you ....

das said...

correct ra 16 que ans is 6

turkey tour said...

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.

sweet said...

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

Anonymous said...

hey its to good,thx a lot

Anonymous said...

AWESOME WORK DUDE......bless you!!

ashu said...

how to make a quiz programme in c language ?

Anonymous said...

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

vasanthakumar said...

good work.

Anonymous said...

Lai Bhari

suri23112gmail.com said...

awesome question

Arun Diwaker said...

i=0;
i= i++ + ++i + i++ + i;

output=?

Anonymous said...

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

Anonymous said...

pleas post more question

Raj Kumar said...

#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..

www.ashugpb.com said...

dear dasu answer is correct 9

priya said...

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

Anonymous said...

# 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

Priyanka kumari said...

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

Vamshi said...

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 :)

Anonymous said...

#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

Anonymous said...

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

sandeep said...

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



in question 18 the answer will be 9 not 34

anknits said...

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.

Anonymous said...

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

Unknown said...

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

Anonymous said...

your explanation are very well. nice work done

Debayan Banerjee said...

Superb material

i have a doubt .

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

Ajathashathru said...

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

tanya said...

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

etc....

Anonymous said...

these questions are really awsome

Ashis said...

nice.....superb..........

Unknown said...

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

soni said...

1.() left to right
2.++ -- ! right to left
3.* / % left to right

soni said...

ans is 3
explanation:
i++=1
++i=1
i++ + ++i=2
1++=1
i++ + ++i + ++i=3
i=0;3+0=3

Unknown said...

Sir Question No 16, I think I am not able to understand what the right answer would be 8 if you can tell me the correct answer

Unknown said...

Write a C++ program that will print the pattern as shown below:
Output:
*
***
*****
*******
*********
*********
*******
*****
***
*
plz tell me the ans in this email juxt.fawad880@gmail.com

Unknown said...

plz explain the question no. 7 and 18....

Unknown said...

Write a C program for a restaurant given the following: The restaurant has a policy for large parties of dinners. If a party has 10 or more people, a 15% service charge is automatically added to the bill. If there are fewer than 10 people, no service charge is added. Write a program that reads the number of people in the party and the amount of money that each person is to pay alone, and then calculates and prints the total bill including service charge (if there is a charge). can you answer this question please?

Unknown said...

wap to enter five number and print them , but only when number is non zero ( while & if elde)