0
0
Cprogramming~20 mins

Return values in C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Return Value Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this C function call?

Consider the following C code snippet. What will be printed when printf("%d", add(3, 4)); is executed?

C
int add(int a, int b) {
    return a + b;
}

int main() {
    printf("%d", add(3, 4));
    return 0;
}
ACompilation error
B34
C0
D7
Attempts:
2 left
💡 Hint

Remember that the return statement sends back the sum of the two numbers.

Predict Output
intermediate
2:00remaining
What value does this function return?

What is the return value of the function mystery when called with mystery(5)?

C
int mystery(int x) {
    if (x > 0) {
        return x * 2;
    } else {
        return -1;
    }
}
A10
B-1
C0
DUndefined behavior
Attempts:
2 left
💡 Hint

Check the condition and what happens when x is positive.

Predict Output
advanced
2:00remaining
What is the output of this recursive function?

What will be printed when printf("%d", factorial(4)); is executed?

C
int factorial(int n) {
    if (n == 0) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

int main() {
    printf("%d", factorial(4));
    return 0;
}
A24
B10
C0
DStack overflow error
Attempts:
2 left
💡 Hint

Recall that factorial of 4 is 4 * 3 * 2 * 1.

Predict Output
advanced
2:00remaining
What error does this function cause?

What happens when int result = no_return(); is executed given the function below?

C
int no_return() {
    int x = 5;
    x += 3;
    // Missing return statement
}

int main() {
    int result = no_return();
    printf("%d", result);
    return 0;
}
APrints 8
BCompilation warning or undefined behavior
CPrints 0
DRuntime error: segmentation fault
Attempts:
2 left
💡 Hint

Functions declared to return a value must have a return statement.

🧠 Conceptual
expert
2:00remaining
What is the value of x after this function call?

Given the code below, what is the value of x after x = update(10); is executed?

C
int update(int a) {
    a = a + 5;
    return a;
}

int main() {
    int x = 0;
    x = update(10);
    return 0;
}
A0
B10
C15
DCompilation error
Attempts:
2 left
💡 Hint

The function returns the updated value of a.