Scroll to Top

Online C Test- Increment and Decrement Operators

C Programming Questions and Answers – Increment and Decrement Operators – 1

 

Here is a listing of online C test questions on “Increment and Decrement Operators” along with answers, explanations and/or solutions:

1. What is the difference between the following 2 codes?

#include <stdio.h> //Program 1
int main()
{
int d, a = 1, b = 2;
d = a++ + ++b;
printf(“%d %d %d”, d, a, b);
}

#include <stdio.h> //Program 2
int main()
{
int d, a = 1, b = 2;
d = a++ +++b;
printf(“%d %d %d”, d, a, b);
}
a) No difference as space doesn’t make any difference, values of a, b, d are same in both the case
b) Space does make a difference, values of a, b, d are different
c) Program 1 has syntax error, program 2 is not
d) Program 2 has syntax error, program 1 is not

Answer: d
Explanation: None.

2. What will be the output of the following C code?

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

Answer: b
Explanation: None.

3. What will be the output of the following C code?

#include <stdio.h>
int main()
{
int a = 1, b = 1, d = 1;
printf(“%d, %d, %d”, ++a + ++a+a++, a++ + ++b, ++d + d++ + a++);
}
a) 15, 4, 5
b) 9, 6, 9
c) 9, 3, 5
d) Undefined (Compiler Dependent)

Answer: d
Explanation: None.

4. For which of the following, “PI++;” code will fail?
a) #define PI 3.14
b) char *PI = “A”;
c) float PI = 3.14;
d) none of the Mentioned

Answer: a
Explanation: None.

5. What will be the output of the following C code?

#include <stdio.h>
int main()
{
int a = 10, b = 10;
if (a = 5)
b–;
printf(“%d, %d”, a, b–);
}
a) a = 10, b = 9
b) a = 10, b = 8
c) a = 5, b = 9
d) a = 5, b = 8

Answer: c
Explanation: None.

6. What will be the output of the following C code?

#include <stdio.h>
int main()
{
int i = 0;
int j = i++ + i;
printf(“%d\n”, j);
}
a) 0
b) 1
c) 2
d) Compile time error

Answer: b
Explanation: None.

7. What will be the output of the following C code?

#include <stdio.h>
int main()
{
int i = 2;
int j = ++i + i;
printf(“%d\n”, j);
}
a) 6
b) 5
c) 4
d) Compile time error

Answer: a
Explanation: None.

8. What will be the output of the following C code?

#include <stdio.h>
int main()
{
int i = 2;
int i = i++ + i;
printf(“%d\n”, i);
}
a) = operator is not a sequence point
b) ++ operator may return value with or without side effects
c) it can be evaluated as (i++)+i or i+(++i)
d) = operator is a sequence point

Answer: a
Explanation: None.

« Prev – C Programming Questions and Answers – Type Conversions – 2
» Next – C Programming Questions and Answers – Increment and Decrement Operators – 2

Related posts

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
%d bloggers like this: