Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the sum of two integers.
C++
int add(int a, int b) {
return [1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Returning one of the parameters alone.
✗ Incorrect
The function should return the sum of a and b, which is 'a + b'.
2fill in blank
mediumComplete the code to return the length of a string.
C++
#include <string> int getLength(const std::string& str) { return [1]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count()' which is not a string method.
Using 'length' without parentheses.
✗ Incorrect
Both 'str.size()' and 'str.length()' return the length of the string.
3fill in blank
hardFix the error in the function to correctly return the maximum of two numbers.
C++
int max(int x, int y) { if (x > y) { return [1]; } else { return y; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning y inside the if block.
Returning a boolean expression instead of a variable.
✗ Incorrect
If x is greater than y, the function should return x.
4fill in blank
hardFill both blanks to create a function that returns true if a number is even.
C++
bool isEven(int num) {
return num [1] 2 [2] 0;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' instead of '%'.
Using '!=' instead of '=='.
✗ Incorrect
To check if a number is even, use the modulus operator '%' and compare the result to 0 with '=='.
5fill in blank
hardFill all three blanks to create a function that returns a new string with the first character capitalized.
C++
#include <string> #include <cctype> std::string capitalize(const std::string& s) { if (s.empty()) return s; std::string result = s; result[[1]] = std::toupper(s[[2]]); return [3]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 instead of 0 for the first character.
Returning the original string 's' instead of the modified 'result'.
✗ Incorrect
The first character is at index 0. We capitalize s[0] and assign it to result[0], then return result.