Bird
Raised Fist0
C++programming~10 mins

Arithmetic 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 add two numbers and print the result.

C++
#include <iostream>

int main() {
    int a = 5, b = 3;
    int sum = a [1] b;
    std::cout << sum << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using minus - instead of plus +.
Using multiplication * or division / by mistake.
2fill in blank
medium

Complete the code to calculate the remainder of division.

C++
#include <iostream>

int main() {
    int x = 10, y = 3;
    int remainder = x [1] y;
    std::cout << remainder << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A/
B+
C%
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using division / instead of remainder %.
Using multiplication * or addition + by mistake.
3fill in blank
hard

Fix the error in the code to multiply two numbers correctly.

C++
#include <iostream>

int main() {
    int a = 4, b = 7;
    int product = a [1] b;
    std::cout << product << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A*
B/
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using plus + or minus - instead of multiplication.
Using division / by mistake.
4fill in blank
hard

Fill both blanks to create a map of numbers to their squares for numbers greater than 3.

C++
#include <iostream>
#include <map>

int main() {
    std::map<int, int> squares;
    for (int num = 1; num <= 5; ++num) {
        if (num [1] 3) {
            squares[num] = num [2] num;
        }
    }
    for (const auto& [key, value] : squares) {
        std::cout << key << ": " << value << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A>
B<
C**
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than < instead of greater than >.
Using ** which is not a valid operator in C++.
5fill in blank
hard

Fill all three blanks to create a map of uppercase letters to their ASCII codes for letters with codes greater than 65.

C++
#include <iostream>
#include <map>
#include <cctype>

int main() {
    std::map<char, int> letter_codes;
    for (char ch = 'A'; ch <= 'E'; ++ch) {
        if (static_cast<int>(ch) [1] 65) {
            letter_codes[[2]] = static_cast<int>([3]);
        }
    }
    for (const auto& [letter, code] : letter_codes) {
        std::cout << letter << ": " << code << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A>
Bch
Ctoupper(ch)
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than < instead of greater than >.
Using lowercase ch as value without conversion.

Practice

(1/5)
1. Which arithmetic operator in C++ is used to find the remainder of a division?
easy
A. -
B. /
C. %
D. *

Solution

  1. Step 1: Understand the meaning of each operator

    The operator % returns the remainder after division, while / returns the quotient.
  2. Step 2: Identify the operator for remainder

    Since remainder is asked, the correct operator is %.
  3. Final Answer:

    % -> Option C
  4. Quick Check:

    Remainder operator = % [OK]
Hint: Remainder uses % symbol, not division / [OK]
Common Mistakes:
  • Confusing / with %
  • Using * for remainder
  • Using - for remainder
2. Which of the following is the correct syntax to multiply two integers a and b in C++?
easy
A. a * b;
B. a x b;
C. a multiply b;
D. a . b;

Solution

  1. Step 1: Recall multiplication operator syntax

    In C++, multiplication uses the asterisk symbol * between operands.
  2. Step 2: Check each option

    Only a * b; uses the correct operator and syntax.
  3. Final Answer:

    a * b; -> Option A
  4. Quick Check:

    Multiplication operator = * [OK]
Hint: Use * for multiplication, not words or symbols [OK]
Common Mistakes:
  • Using x instead of *
  • Writing multiply as word
  • Using dot operator for math
3. What is the output of the following C++ code?
int a = 10, b = 3;
std::cout << a / b << std::endl;
medium
A. 3.33333
B. 3
C. 4
D. Error

Solution

  1. Step 1: Understand integer division in C++

    Both a and b are integers, so division truncates decimals.
  2. Step 2: Calculate integer division result

    10 divided by 3 is 3.333..., but integer division returns 3.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    Integer division truncates decimals = 3 [OK]
Hint: Integer division drops decimals, result is integer [OK]
Common Mistakes:
  • Expecting decimal result
  • Using floating-point division without casting
  • Thinking output is error
4. Identify the error in this C++ code snippet:
int x = 5;
int y = 2;
int z = x / y.0;
std::cout << z << std::endl;
medium
A. Syntax error due to decimal point in divisor
B. Using floating-point literal with integer division
C. Incorrect variable declaration
D. Missing semicolon

Solution

  1. Step 1: Analyze the divisor y.0

    The expression y.0 is invalid syntax in C++ because variables cannot have decimal points.
  2. Step 2: Identify the syntax error

    The decimal point after y causes a syntax error; correct would be y * 1.0 or static_cast<double>(y).
  3. Final Answer:

    Syntax error due to decimal point in divisor -> Option A
  4. Quick Check:

    Variable names cannot have decimal points [OK]
Hint: Variable names can't have dots; causes syntax error [OK]
Common Mistakes:
  • Thinking y.0 is valid float
  • Ignoring syntax error
  • Confusing variable and literal
5. Given int a = 7, b = 4;, which expression correctly calculates the average as a floating-point number in C++?
hard
A. a / b + 2
B. (a + b) / 2
C. a + b / 2
D. (a + b) / 2.0

Solution

  1. Step 1: Understand integer vs floating-point division

    Using 2 divides integers, truncating decimals. Using 2.0 forces floating-point division.
  2. Step 2: Evaluate each option

    (a + b) / 2 does integer division, result 5. (a + b) / 2.0 divides by 2.0, giving 5.5 as float. Options C and D do not calculate average correctly.
  3. Final Answer:

    (a + b) / 2.0 -> Option D
  4. Quick Check:

    Use floating-point divisor for decimal average [OK]
Hint: Divide by 2.0 to get decimal average, not integer [OK]
Common Mistakes:
  • Using integer 2 causing truncation
  • Wrong operator precedence
  • Misinterpreting average formula
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