0
0
C++programming~10 mins

Return values in C++ - Interactive Code Practice

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

Complete 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'
Aa * b
Ba - b
Cb - a
Da + b
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Returning one of the parameters alone.
2fill in blank
medium

Complete 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'
Astr.size()
Bstr.length
Cstr.count()
Dstr.length()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count()' which is not a string method.
Using 'length' without parentheses.
3fill in blank
hard

Fix 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'
Ax
Bx > y
Cy
Dy > x
Attempts:
3 left
💡 Hint
Common Mistakes
Returning y inside the if block.
Returning a boolean expression instead of a variable.
4fill in blank
hard

Fill 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'
A%
B==
C!=
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' instead of '%'.
Using '!=' instead of '=='.
5fill in blank
hard

Fill 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'
A0
B1
Cresult
Ds
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'.