c programming quiz with solution

C programming language quiz questions and answers for  beginners 


Time: 6 minute

(1)
void main(){

float a=30.3f;

int y=5;

clrscr();

printf("%d",a%y);

getch();

}

What will be output when you will compile above code?

(a) 6

(b) 6.0

(c) 7

(d) Compiler error

(e) None of these


(2)

void main(){

int x=5;

float r=5.0;

clrscr();

printf("%x %X %o",sizeof(x,r),sizeof(r,x),sizeof(sizeof(5.0)));

getch();

}


What will be output when you will compile above code?

(a) 4 2 2

(b) 2 4 4

(c) 2 4 2

(d) Compiler error

(e) None of these


(3)

void main(){

int y=15,z=25;

function(&y,&z);

clrscr();

printf("%d\t%d",z,y);

getch();

}

function (int *p,int *q){

return(*p=(*p+*q)-(*q=*p));

}


What will be output when you will compile above code?

(a) 25 15

(b) 15 25

(c) 25 25

(d) Compiler error

(e) None of these


(4)

void main(){

int a=-1;

static int count;

clrscr();

while(a){

count++;

a&=a-1;

}

printf("%d",count);

getch();

}


What will be output when you will compile above code?

(a) 15

(b) 16

(c) 17

(d) Compiler error

(e) None of these


(5)

void main(){

typedef float arr[10];

arr b={2,4,6};

clrscr();

printf("%d",sizeof(b));

getch();

}


What will be output when you will compile above code?

(a) 40

(b) 12

(c) 4

(d) Compiler error

(e) None of these


(6)

void change(int const *p){

*((int *)p)=20;

}

void main(){

int const x=10;

change(&x);

clrscr();

printf("%d",x);

getch();

}


What will be output when you will compile above code?

(a) 10

(b) 20

(c) 30

(d) Compiler error

(e) None of these


Solution:

(1)(d) Modular division is only possible for integral data type

(2)(a)

(3)(b)

(4)(b)

(5)(a)

(6)(b)

Note: If you want to explanation of any questions you can ask through comment.


If you have any quires or suggestions on above c programming language quizzes or quiz with solution online test, please share us.  

22 comments:

An said...

the 3rd question answer cant be right.I read in a book that addition of two pointers is wrong

Priyanka kumari said...

hi anu
Pointer alway refer to some value or address .When it is referring to some value (integral) means we are performing addition operation between two number. So answer of question 3 is correct.

angie said...

Kindly explain number 4. I dont undestrand how it turned out to be 16. How come that it looped 16 times?

Priyanka kumari said...

hi angie

For if condition zero means false and any non zero means true. Initial value of variable a is -1 i.e. and while loop will execute until variable a became zero.
a =a &(a-1) always double the value of a i.e. -2 ,-4, -8, -16, ….
After the beyond of range of signed int it became zero due to cyclic nature of int data type. More details of cyclic nature visit this link: http://cquestionbank.blogspot.com/2009/06/cyclic-nature-of-data-type-in-c.html

veronika said...

can u explain hw does the value of a gets double in num 4? wts dat expression?

Swetanshu said...

can u plz tell how can we use c programming to just print a statement
on dos shell by writing a program without using even a single semi-colon in the whole code...???

Priyanka kumari said...

Solution 1:

void main(){
while(!printf("Hello world"));
}


Output: Hello world

Solution 2:

void main(){
if(printf("Hello world")){}
}


Output: Hello world

Solution 3:

void main(){
switch(printf("Hello world")){}
}

Output: Hello world

Prateek Dhuper said...

can u pls explain ues. no. 2

Anonymous said...

Question number 2 output is 4 4 4. The answer is incorrect. Plus, this really weird way of using sizeof operator. Why in the heck you want to overload this operator? I have developed C code for the past 17 years and never used the sizeof in this fashion. What a stupid and really irrelevant C pop-quiz question.

3G_beginner said...

hi anu 3rd is absolutely right n u r also right pointers cant be added bt here we r nt adding pointer we r adding da values on which pointers r pointing nw got it sweetie

Sriram said...

Can anyone tell me wat the 1st question declaration float f=33.3f; mean?

Then in the 4th how is that sizeof() operator acccepts 2 arguments?

Anonymous said...

Regarding question 3:
How can you claim that answer b is right, since C99 standard is not guarantying order of evaluation operands in statement. So, you can't be sure that (*p+*q) will be evaluated before (*q=*p).

Gururaj B Patil said...

Can u Plz explain How 4th answer is correct?

amee said...

anyone can give me solution of all these questions?

Nani said...

hey i can't get u'r explanation 2 4th one. the statment a&=a-1;... how it works?

Priyanka kumari said...

hi Nani,
a&=a-1
a=a&(a-1)
here & is bitwise and operator
For example: 5&3
binary of 5 is: 101
binary of 3 is: 011

in bitwise and operator:
0&0=0
0&1=0
1&0=0
1&1=1

I think now you can find 5 &3

If your doubt is something different then you can ask again.

Anonymous said...

hello this is sathish

i want tht answers wit explanation

pls send to me

pgdcasathish@gmail.com

vidya said...

hi i can't undarstand the ans 4 4th ques.so pls xplain it me clearly

Anonymous said...

give theanswer with explanation of 2 and 4 question.

Anonymous said...

I think the 2> is questionable. First, the usage of sizeof is not in good style. Second, its result is compiler dependent.

Anonymous said...

The questions are fine

ramesh said...

Explain 6th one.Const value can not be changed