Quiz on Increment and Decrement Operators in C

Quiz on Increment and Decrement Operators in C | Increment and decrement operators are also known as unary operators’ because they operate on a single operand. The increment operator (++) adds 1 to its operand and decrement operator (`--`) subtracts one.

Syntax:-
`++variable;` // pre-increment operator
`variable++; `//post-increment operator
`--variable;` // pre-decrement operator
`variable--; `//post-decrement operator

Recommended Reading:- Operators in C

C Quiz on Increment and Decrement

Q1) What will be the output of below C program?

``````#include<stdio.h>
int main()
{
int x, y;
x = 5;
y = x++ / 2;
printf("%d", y);
return 0;
}``````

a) 3
b) Compile-time error
c) None of these
d) 2

Ans:- d) 2

The given expression is:- y=x++ / 2; x++ so post-increment, y=5/2=2. Hence, x=6 and y=2.

Q2) What will be the output of below C program?

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

a) 3 3 2
b) 2 3 2
c) 3 2 2
d) 2 3 3

Ans:- d) 2 3 3

The first expression is b=–a; so, a becomes 3 (a=3) and b=3. Now, c=a–; so, c=3 and a=2. Finally, a=2, b=3 and c=3.

Q3) What will be the output of below C program?

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

a) 6
b) 5
c) Compile-time error
d) 7

Ans:- c) Compile-time error

Compile time error: lvalue required as increment operand

Q4) What will be the output of below C program?

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

a) Compile-time error
b) 29
c) 28
d) None of these

Ans:- a) Compile-time error

Error: lvalue required as increment operand; The operand of increment and decrement operators must be a variable, not a constant or expression.

Q5) What will be the output of below C program?

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

a) 21 10 10
b) Compile-time error
c) 21 10 11
d) 21 11 11

Ans:- b) Compile-time error

Compile time error: lvalue required as increment operand
c=a+++++b;
^~

Q6) What will be the output of below C program?

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

a) 3 1 3
b) Compile-time error
c) 2 2 4
d) None of these

Ans:- a) 3 1 3

The expression c=a+++b; is treated as c=(a++)+b; so, a=3, b=1, c=3

Q7) What will be the output of below C program?

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

a) 5 4 4
b) 5 5 4
c) 5 4 3
d) 5 5 5

Ans:- b) 5 5 4

Q8) What will be the output of below C program?

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

a) Compile-time error
b) 10
c) 12
d) 11

Ans:- a) Compile-time error

Compile time error: lvalue required as increment operand
b=++++a;
^~

Q9) What will be the output of below C program?

``````#include<stdio.h>
int main()
{
printf("%d ", + +9);
printf("%d ", + -9);
printf("%d ", - +9);
printf("%d ", - -9);
return 0;
}``````

a) 9 -9 -9 9
b) 10 9 9 9
c) 10 -9 -9 8
d) 10 9 9 8

Ans:- a) 9 -9 -9 9

These are not increment or decrement operators.

Q10) What will be the output of below C program?

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

a) 5 5
b) 6 6
c) 6 5
d) 5 6

Ans:- c) 6 5

x=5 and y=x++; from y=x++, it is postfix increment so first value of x copies to variable y and now the value of x incremented by 1. Hence y=5 and x=6.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!