Bird
Raised Fist0
C++programming~10 mins

Increment and decrement operators in C++ - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to increment the variable count by 1.

C++
int count = 5;
count[1];
Drag options to blanks, or click blank then click option'
A++
B--
C+=
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using the decrement operator -- instead of increment.
Using += without specifying the amount.
2fill in blank
medium

Complete the code to decrement the variable value by 1.

C++
int value = 10;
value[1];
Drag options to blanks, or click blank then click option'
A--
B++
C+=
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using the increment operator ++ instead of decrement.
Using -= without specifying the amount.
3fill in blank
hard

Fix the error in the code to correctly increment num.

C++
int num = 7;
num = num[1];
Drag options to blanks, or click blank then click option'
A++
B- 1
C--
D+ 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using ++ after assignment operator.
Using decrement operators instead of increment.
4fill in blank
hard

Fill both blanks to create a map of numbers to their squares using increment in the loop.

C++
std::map<int, int> squares;
for (int i = 1; i [1] 5; i[2]) {
    squares[i] = i * i;
}
Drag options to blanks, or click blank then click option'
A<=
B<
C++
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using i <= 5 causes the loop to run one extra time.
Using decrement operator -- in the loop.
5fill in blank
hard

Fill all three blanks to create a map of uppercase letters to their ASCII codes, incrementing the letter.

C++
std::map<char, int> ascii_map;
for (char ch = 'A'; ch [1] 'Z'; ch[2]) {
    ascii_map[[3]] = static_cast<int>(ch);
}
Drag options to blanks, or click blank then click option'
A<=
B++
Cch
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using < code < 'Z' > causes 'Z' to be excluded.
Using decrement operator -- instead of increment.
Using wrong variable as map key.

Practice

(1/5)
1. What does the ++ operator do in C++?
easy
A. Multiplies the variable by 2
B. Subtracts 1 from the variable
C. Adds 1 to the variable
D. Divides the variable by 2

Solution

  1. Step 1: Understand the increment operator

    The ++ operator increases the value of a variable by 1.
  2. Step 2: Compare with other options

    Other options describe different operations like subtraction or multiplication, which are not what ++ does.
  3. Final Answer:

    Adds 1 to the variable -> Option C
  4. Quick Check:

    Increment (++) means add 1 [OK]
Hint: Remember ++ means add one, -- means subtract one [OK]
Common Mistakes:
  • Confusing ++ with --
  • Thinking ++ multiplies or divides
  • Mixing prefix and postfix effects
2. Which of the following is the correct syntax for prefix decrement of a variable x by 1 using the decrement operator?
easy
A. --x;
B. x--;
C. x-1;
D. x--1;

Solution

  1. Step 1: Identify prefix decrement syntax

    The prefix decrement operator is --x;, which decrements the value first.
  2. Step 2: Check other options

    x--; is postfix. x-1; subtracts but does not modify x. x--1; is invalid.
  3. Final Answer:

    --x; -> Option A
  4. Quick Check:

    Prefix decrement is -- before variable [OK]
Hint: Prefix decrement: -- before variable [OK]
Common Mistakes:
  • Writing x--1 which is invalid
  • Using x-1; which does not change x
  • Confusing decrement with subtraction
3. What is the output of the following code?
int x = 5;
int y = x++ + ++x;
std::cout << y;
medium
A. 11
B. 12
C. 13
D. 14

Solution

  1. Step 1: Evaluate x++ and ++x values

    Initially, x = 5. x++ uses 5 then increments to 6. Then ++x increments 6 to 7 and uses 7.
  2. Step 2: Calculate the sum

    Sum is 5 (from x++) + 7 (from ++x) = 12.
  3. Step 3: Confirm final value of y

    So, y = 12.
  4. Final Answer:

    12 -> Option B
  5. Quick Check:

    Postfix uses old value, prefix increments first [OK]
Hint: Postfix uses old value; prefix increments before use [OK]
Common Mistakes:
  • Adding incremented values twice
  • Mixing prefix and postfix effects
  • Ignoring order of operations
4. Find the error in the following code snippet:
int a = 3;
int b = ++a + a--;
std::cout << b;
medium
A. Undefined behavior due to modifying variable twice without sequence point
B. No error; output is 8
C. Syntax error due to invalid operator usage
D. Runtime error due to decrement on uninitialized variable

Solution

  1. Step 1: Analyze variable modifications

    The variable a is modified twice in the same expression: once by ++a and once by a--.
  2. Step 2: Understand sequence point rules

    In C++, modifying a variable more than once between sequence points causes undefined behavior.
  3. Final Answer:

    Undefined behavior due to modifying variable twice without sequence point -> Option A
  4. Quick Check:

    Multiple ++/-- on same var in one expression causes undefined behavior [OK]
Hint: Avoid multiple ++/-- on same variable in one expression [OK]
Common Mistakes:
  • Assuming output is always predictable
  • Ignoring undefined behavior rules
  • Thinking syntax error occurs here
5. Given the code below, what will be the final value of sum?
int sum = 0;
int i = 0;
while (i++ < 5) {
sum += i;
}
hard
A. 10
B. 20
C. 25
D. 15

Solution

  1. Step 1: Trace the loop iterations and values of i

    Initially, i = 0. Condition i++ < 5 uses i then increments. Iterations:
    1) i=0 (check 0<5 true), then i=1, sum+=1
    2) i=1 (check 1<5 true), then i=2, sum+=2
    3) i=2 (check 2<5 true), then i=3, sum+=3
    4) i=3 (check 3<5 true), then i=4, sum+=4
    5) i=4 (check 4<5 true), then i=5, sum+=5
    6) i=5 (check 5<5 false), loop ends
  2. Step 2: Calculate sum

    Sum = 1+2+3+4+5 = 15
  3. Final Answer:

    15 -> Option D
  4. Quick Check:

    Postfix increments after comparison; sum 1 to 5 [OK]
Hint: Postfix increments after check; sum counts 1 to 5 [OK]
Common Mistakes:
  • Assuming i increments before comparison
  • Adding wrong range of numbers
  • Confusing prefix and postfix in loop condition
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard