This section on C++ programming questions and answers on “Increment and Decrement”. One shall practice these questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ programming questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.
Here is a listing of C++ programming questions on “Increment and Decrement” along with answers, explanations and/or solutions:
1. Which operator works only with integer variables?
a) increment
b) decrement
c) both increment & decrement
d) binary operator
Answer: c
Clarification: Because increment and decrement operator increases increasing and decreasing values of values and no such things define in strings so cannot be used with strings. Also they cannot be used with floats and doubles because there is no way to fix how much the value should be increased or decreased if increment or decrement operator is applied on such variables. That’s why both these operators only works with integer values.
2. How many types are there in increment/decrement operator?
a) 1
b) 2
c) 3
d) 4
Answer: b
Clarification: There are two types of increment/decrement. They are postfix and prefix.
3. Pick out the correct statement.
a) Increment operator ++ adds 1 to its operand
b) Increment operator ++ adds 2 to its operand
c) Decrement operator ++ subtracts 1 to its operand
d) Decrement operator ++ subtracts 3 to its operand
250+ TOP MCQs on Character Types and Answers
Answer: a
Clarification: Increment operator are used to increase the values of any integer variable by 1.
4. What will be the output of the following C++ code?
-
#include
-
#include
-
using namespace std;
-
int main()
-
{
-
int a = 21;
-
int c ;
-
c = a++;
-
cout << c;
-
return 0;
-
}
a) 21
b) 22
c) 23
d) 20
Answer: a
Clarification: value of ‘a’ will be stored in c and then only it will be incremented.
Output:
$ g++ incre.cpp
$ a.out
21
5. What will be the output of the following C++ code?
-
#include
-
#include
-
using namespace std;
-
int main()
-
{
-
int x = 5, y = 5;
-
cout << ++x << --y << endl;
-
return 0;
-
}
a) 55
b) 64
c) 46
d) 45
Answer: b
Clarification: The values will be pre increment and pre decrement, So it will print as 64.
Output:
$ g++ incre2.cpp
$ a.out
64
6. What will be the output of the following C++ code?
-
#include
-
#include
-
using namespace std;
-
int main()
-
{
-
int x = 5, y = 5, z;
-
x = ++x; y = --y;
-
z = x++ + y--;
-
cout << z;
-
return 0;
-
}
a) 10
b) 11
c) 9
d) 12
Answer: a
Clarification: In this program, the increment and decrement of evaluation of z will not be accounted because they are post.
Output:
$ g++ incre3.cpp
$ a.out
10
7. What will be the output of the following C++ code?
-
#include
-
#include
-
using namespace std;
-
int main()
-
{
-
int x = 5, y = 5, z;
-
x = ++x; y = --y;
-
z = x + ++x;
-
cout << z;
-
return 0;
-
}
a) 11
b) 12
c) 13
d) 14
Answer: d
Clarification: In this program, we are adding the x value after pre incrementing two times.
Output:
$ g++ incre4.cpp
$ a.out
14
8. What will be the output of the following C++ code?
-
#include
-
#include
-
using namespace std;
-
int main()
-
{
-
int num1 = 5;
-
int num2 = 3;
-
int num3 = 2;
-
num1 = num2++;
-
num2 = --num3;
-
cout << num1 << num2 << num3;
-
return 0;
-
}
a) 532
b) 235
c) 312
d) 311
Answer: d
Clarification: In this program, We are pre increment and post incrementing the operands and saving it.
Output:
$ g++ incre5.cpp
$ a.out
311
9. Pick out the correct statement.
a) Pre Increment is faster than post-increment
b) post-increment is faster than Pre Increment
c) pre increment is slower than post-increment
d) pre decrement is slower than post-increment
Answer: a
Clarification: Because Pre Increment take one-byte instruction & post increment takes two-byte instruction.
10. Which concepts does the Pre Increment use?
a) call by value
b) call by reference
c) queue
d) call by name
Answer: b
Clarification: call by reference because the changes are reflected back to the same memory cells/variables.