Bird
Raised Fist0
Goprogramming~20 mins

Return values in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Go Return Values 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 Go function with multiple return values?

Consider the following Go code. What will be printed when main runs?

Go
package main
import "fmt"

func divideAndRemainder(a, b int) (int, int) {
    return a / b, a % b
}

func main() {
    q, r := divideAndRemainder(17, 5)
    fmt.Println(q, r)
}
A2 3
B3 3
C3 2
DError: cannot return two values
Attempts:
2 left
💡 Hint

Remember that / is integer division and % is remainder.

❓ Predict Output
intermediate
2:00remaining
What does this Go function return when called?

Look at this Go function and its call. What is the output?

Go
package main
import "fmt"

func greet(name string) string {
    return "Hello, " + name + "!"
}

func main() {
    message := greet("Alice")
    fmt.Println(message)
}
AHello, Alice!
BHello Alice!
CError: missing return statement
Dgreet Alice
Attempts:
2 left
💡 Hint

Check the string concatenation and punctuation.

❓ Predict Output
advanced
2:00remaining
What is the output of this Go function with named return values?

Analyze this Go code using named return values. What will it print?

Go
package main
import "fmt"

func swap(a, b int) (x int, y int) {
    x = b
    y = a
    return
}

func main() {
    a, b := 10, 20
    x, y := swap(a, b)
    fmt.Println(x, y)
}
A20 10
BCompilation error: missing return values
C0 0
D10 20
Attempts:
2 left
💡 Hint

Named return values can be returned by just return without arguments.

❓ Predict Output
advanced
2:00remaining
What error does this Go function produce?

What error will occur when running this Go code?

Go
package main

func add(a, b int) int {
    return a + b
    return a - b
}

func main() {}
ARuntime error: multiple returns
BSyntax error: unreachable code after return
CNo error, returns sum
DCompilation error: missing return statement
Attempts:
2 left
💡 Hint

Check if code after a return statement is allowed.

🧠 Conceptual
expert
2:00remaining
How many values does this Go function return?

Consider this Go function signature. How many values does it return?

func process(data []int) (result int, err error)
AThree values: int, error, and a boolean
BOne value: a struct containing int and error
CNo values, only modifies data in place
DTwo values: an int and an error
Attempts:
2 left
💡 Hint

Look at the parentheses and types in the return signature.

Practice

(1/5)
1. What does a function's return value in Go do?
easy
A. It defines the function's name.
B. It comments the code for documentation.
C. It declares variables inside the function.
D. It sends a result back to the place where the function was called.

Solution

  1. Step 1: Understand the purpose of return values

    Return values are used to send results from a function back to the caller.
  2. Step 2: Identify the correct description

    Only It sends a result back to the place where the function was called. correctly describes this purpose.
  3. Final Answer:

    It sends a result back to the place where the function was called. -> Option D
  4. Quick Check:

    Return value = sends result back [OK]
Hint: Return values send results back to the caller [OK]
Common Mistakes:
  • Confusing return values with variable declarations
  • Thinking return values name the function
  • Assuming return values are comments
2. Which of the following is the correct way to declare a function in Go that returns an integer?
easy
A. func add() int { return 5 }
B. func add() { return 5 }
C. func add int() { return 5 }
D. func add() int return 5

Solution

  1. Step 1: Check function syntax for return type

    In Go, the return type is specified after parentheses: func name() type { ... }
  2. Step 2: Verify each option

    func add() int { return 5 } correctly declares a function returning int. Others have syntax errors.
  3. Final Answer:

    func add() int { return 5 } -> Option A
  4. Quick Check:

    Return type after () = int [OK]
Hint: Return type goes after parentheses in function declaration [OK]
Common Mistakes:
  • Placing return type before function name
  • Omitting return type when returning a value
  • Incorrect function syntax
3. What is the output of this Go code?
func multiply(a, b int) int {
    return a * b
}

func main() {
    result := multiply(3, 4)
    fmt.Println(result)
}
medium
A. 7
B. 34
C. Error: missing import
D. 12

Solution

  1. Step 1: Check for required imports

    The code uses fmt.Println(result) but lacks import "fmt", causing a compile error: undefined fmt.
  2. Step 2: Verify function logic

    The multiply function correctly computes 3*4=12, but the program won't compile or run without the import.
  3. Final Answer:

    Error: missing import -> Option C
  4. Quick Check:

    fmt.Println needs import "fmt" [OK]
Hint: fmt.Println requires import "fmt" [OK]
Common Mistakes:
  • Forgetting import "fmt"
  • Computing 3*4=12 but ignoring missing import
  • Assuming code runs without imports
4. Identify the error in this Go function that returns two integers:
func divide(a, b int) (int, int) {
    return a / b
}
medium
A. Division operator is incorrect.
B. Function returns only one value but declares two return types.
C. Function parameters are missing types.
D. Function name is invalid.

Solution

  1. Step 1: Check return type declaration

    The function declares two return values (int, int) but returns only one value.
  2. Step 2: Identify mismatch in return statement

    Returning a single value causes a compile error because two values are expected.
  3. Final Answer:

    Function returns only one value but declares two return types. -> Option B
  4. Quick Check:

    Return count must match declared types [OK]
Hint: Return values count must match declared types [OK]
Common Mistakes:
  • Returning fewer values than declared
  • Wrong operator usage confusion
  • Ignoring function signature
5. How can you write a Go function that returns both the sum and product of two integers?
hard
A. func calc(a, b int) (int, int) { return a+b, a*b }
B. func calc(a, b int) int { return a+b, a*b }
C. func calc(a, b int) (int) { return a+b, a*b }
D. func calc(a, b int) (int, int) { return a+b + a*b }

Solution

  1. Step 1: Define function with two return types

    The function must declare two return types (int, int) for sum and product.
  2. Step 2: Return sum and product as two values

    Return both values separated by a comma: return a+b, a*b.
  3. Final Answer:

    func calc(a, b int) (int, int) { return a+b, a*b } -> Option A
  4. Quick Check:

    Two return values with matching types [OK]
Hint: Return multiple values separated by commas [OK]
Common Mistakes:
  • Returning multiple values with single return type
  • Returning combined expression instead of two values
  • Incorrect return type syntax
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