Bird
Raised Fist0
Cprogramming~20 mins

Using return codes - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does a return code of 0 usually mean in C functions?
easy
A. The function completed successfully
B. The function encountered an error
C. The function did not run
D. The function returned a pointer

Solution

  1. Step 1: Understand return codes meaning

    In C, a return code of 0 typically means success or no error.
  2. Step 2: Interpret the code 0

    Since 0 means success, the function completed its task without problems.
  3. Final Answer:

    The function completed successfully -> Option A
  4. Quick Check:

    Return code 0 = success [OK]
Hint: Remember: 0 means success, non-zero means error [OK]
Common Mistakes:
  • Thinking 0 means error
  • Confusing return code with function output
  • Assuming non-zero means success
2. Which of the following is the correct way to return an error code from a C function?
easy
A. return 0;
B. return 'error';
C. return -1;
D. return true;

Solution

  1. Step 1: Identify valid return types for error codes

    Return codes are integers, so returning a number like -1 is valid for error.
  2. Step 2: Check each option

    return 0; returns 0 (success), B returns a multi-char constant (invalid), C returns true (boolean, invalid in C), D returns -1 (valid error code).
  3. Final Answer:

    return -1; -> Option C
  4. Quick Check:

    Use integer codes for errors [OK]
Hint: Return integer codes; negative often means error [OK]
Common Mistakes:
  • Returning strings instead of integers
  • Using boolean values in C return codes
  • Returning 0 for errors
3. What will be the output of this C program?
#include <stdio.h>
int check(int x) {
    if (x > 0) return 0;
    else return 1;
}
int main() {
    int result = check(-5);
    printf("Return code: %d\n", result);
    return 0;
}
medium
A. Return code: 1
B. Return code: -5
C. Return code: 0
D. No output

Solution

  1. Step 1: Analyze the check function behavior

    If x > 0, it returns 0; otherwise, it returns 1.
  2. Step 2: Apply input value -5

    Since -5 is not greater than 0, the function returns 1.
  3. Final Answer:

    Return code: 1 -> Option A
  4. Quick Check:

    Input -5 <= 0, so return 1 [OK]
Hint: Check conditions carefully to find returned code [OK]
Common Mistakes:
  • Assuming negative input returns 0
  • Confusing return value with input
  • Ignoring else branch
4. Identify the error in this C function that uses return codes:
int process(int val) {
    if (val == 0)
        return 0;
    else
        return;
}
medium
A. Using 0 as success code
B. No error, code is correct
C. Function should return void
D. Missing return value in else branch

Solution

  1. Step 1: Check function return type and return statements

    The function returns int, so every return must have an int value.
  2. Step 2: Identify missing return value

    The else branch has 'return;' without a value, which is invalid for int return type.
  3. Final Answer:

    Missing return value in else branch -> Option D
  4. Quick Check:

    int functions must return int always [OK]
Hint: Every return in int function must return int value [OK]
Common Mistakes:
  • Returning nothing in non-void function
  • Confusing void and int return types
  • Ignoring compiler warnings
5. You want to write a function int saveData() that returns 0 on success, 1 if file not found, and 2 if write error. Which is the best way to handle these return codes in main()?
hard
A. Ignore the return code and always print "Success"
B. Check if return code is 0, else print error message based on code
C. Return the code from main without checking
D. Use return codes only for debugging, not in main

Solution

  1. Step 1: Understand return codes purpose

    Return codes help main know if saveData succeeded or failed and why.
  2. Step 2: Choose proper handling in main

    Main should check if return code is 0 (success), else print specific error messages for 1 or 2.
  3. Final Answer:

    Check if return code is 0, else print error message based on code -> Option B
  4. Quick Check:

    Use return codes to handle errors clearly [OK]
Hint: Check return codes to handle errors clearly [OK]
Common Mistakes:
  • Ignoring return codes in main
  • Printing success without checking
  • Using return codes only for debugging
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard