0
0
Cprogramming~20 mins

Using return codes - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Return Code 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 program using return codes?

Consider the following C program. What will it print when run?

C
#include <stdio.h>

int check(int x) {
    if (x > 0) return 1;
    else if (x == 0) return 0;
    else return -1;
}

int main() {
    int result = check(-5);
    if (result == 1) printf("Positive\n");
    else if (result == 0) printf("Zero\n");
    else printf("Negative\n");
    return 0;
}
ANegative
BZero
CPositive
DCompilation error
Attempts:
2 left
💡 Hint

Look at the return values of the check function and what main does with them.

Predict Output
intermediate
2:00remaining
What is the return value of this function?

What value does the function process return when called with process(10)?

C
int process(int n) {
    if (n % 2 == 0) return 0;
    else return 1;
}
A0
B1
C10
D-1
Attempts:
2 left
💡 Hint

Check if 10 is even or odd.

🔧 Debug
advanced
2:00remaining
What error does this code produce?

What error will this C code produce when compiled?

C
int check(int x) {
    if (x > 0) return 1;
    else if (x == 0) return 0;
    // Missing return for negative x
}

int main() {
    int r = check(-1);
    return 0;
}
ASegmentation fault at runtime
BWarning: control reaches end of non-void function
CSyntax error: missing semicolon
DNo error, runs fine
Attempts:
2 left
💡 Hint

Think about what happens if x is negative.

🚀 Application
advanced
2:00remaining
How many different return codes does this function produce?

How many distinct return codes can the function status produce?

C
int status(int code) {
    switch(code) {
        case 0: return 100;
        case 1: return 200;
        case 2: return 300;
        default: return 400;
    }
}
A3
B2
C5
D4
Attempts:
2 left
💡 Hint

Count all unique return values possible from the switch cases including default.

🧠 Conceptual
expert
2:00remaining
What is the best way to use return codes for error handling in C?

Which option correctly describes the best practice for using return codes in C functions to indicate success or failure?

AReturn positive numbers for success and negative for failure
BAlways return -1 for any error and 1 for success
CReturn 0 for success and non-zero for different error types
DUse return codes only for success, errors should be printed inside the function
Attempts:
2 left
💡 Hint

Think about common C conventions for return codes.