C++ Programming Questions and Answers – Increment and Decrement

This section on C++ programming questions and answers focuses 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 Multiple Choice Questions & Answers focuses 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: Increment operator are used to increase the values of any integer variable by 1.
advertisement
advertisement

4. What will be the output of the following C++ code?

  1.     #include <stdio.h> 
  2.     #include<iostream>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         int a = 21;
  7.         int c ;
  8.         c = a++;
  9.         cout << c;  
  10.         return 0;
  11.     }

a) 21
b) 22
c) 23
d) 20
View Answer

Answer: a
Explanation: value of ‘a’ will be stored in c and then only it will be incremented.
Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ g++ incre.cpp
$ a.out
21

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

advertisement
  1.     #include <stdio.h> 
  2.     #include<iostream>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         int x = 5, y = 5;
  7.         cout << ++x << --y << endl;
  8.         return 0;
  9.     }

a) 55
b) 64
c) 46
d) 45
View Answer

Answer: b
Explanation: The values will be pre increment and pre decrement, So it will print as 64.
Output:

advertisement
$ g++ incre2.cpp
$ a.out
64

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

  1.     #include <stdio.h> 
  2.     #include<iostream>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         int x = 5, y = 5, z;
  7.         x = ++x; y = --y;
  8.         z = x++ + y--;
  9.         cout << z;
  10.         return 0;
  11.     }

a) 10
b) 11
c) 9
d) 12
View Answer

Answer: a
Explanation: 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?

  1.     #include <stdio.h> 
  2.     #include<iostream> 
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         int x = 5, y = 5, z;
  7.         x = ++x; y = --y;
  8.         z = x + ++x;
  9.         cout << z;
  10.         return 0;
  11.     }

a) 11
b) 12
c) 13
d) 14
View Answer

Answer: d
Explanation: 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?

  1.     #include <stdio.h> 
  2.     #include<iostream>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         int num1 = 5;
  7.         int num2 = 3;
  8.         int num3 = 2;
  9.         num1 = num2++;
  10.         num2 = --num3;
  11.         cout << num1 << num2 << num3;
  12.         return 0;
  13.     }

a) 532
b) 235
c) 312
d) 311
View Answer

Answer: d
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: call by reference because the changes are reflected back to the same memory cells/variables.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.