Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a function named add that takes two integers and returns their sum.
C++
int add(int a, int b) {
return a [1] b;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' will subtract instead of add.
Using '*' or '/' will multiply or divide, which is not correct here.
✗ Incorrect
The plus sign (+) adds two integers together.
2fill in blank
mediumComplete the code to overload the add function to accept two doubles and return their sum.
C++
double add(double a, double b) {
return a [1] b;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' will subtract instead of add.
Using '*' or '/' will multiply or divide, which is not correct here.
✗ Incorrect
The plus sign (+) adds two double values together.
3fill in blank
hardFix the error in the overloaded add function that takes three integers and returns their sum.
C++
int add(int a, int b, int c) {
return a [1] b [2] c;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different operators between variables causes errors or wrong results.
Using only one operator and missing the other will cause syntax errors.
✗ Incorrect
Use '+' to add all three integers together.
4fill in blank
hardComplete the code to overload the add function that takes two strings and returns their concatenation.
C++
std::string add(std::string a, std::string b) {
return a [1] b;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using operators other than '+' will cause compilation errors for strings.
Confusing with output operators like <<.
✗ Incorrect
In C++, the + operator is used to concatenate std::string objects.
5fill in blank
hardFill all three blanks to overload the add function that takes one integer and one double, returning their sum as a double.
C++
double add([1] a, [2] b) { return a [3] b; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing parameter types incorrectly.
Using wrong operator instead of '+'.
✗ Incorrect
The function takes an int and a double, and uses '+' to add them.