0
0
Goprogramming~20 mins

Return inside loops in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Return Mastery in Go
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 code with return inside a loop?

Look at this Go function. What will it print when called?

Go
package main
import "fmt"

func findFirstEven(nums []int) int {
    for _, n := range nums {
        if n%2 == 0 {
            return n
        }
    }
    return -1
}

func main() {
    fmt.Println(findFirstEven([]int{1, 3, 5, 6, 8}))
}
A-1
B6
C0
D8
Attempts:
2 left
๐Ÿ’ก Hint

Remember, return exits the function immediately.

โ“ Predict Output
intermediate
1:30remaining
What does this Go function return?

What value does this function return when called with []int{7, 9, 11}?

Go
func findFirstEven(nums []int) int {
    for _, n := range nums {
        if n%2 == 0 {
            return n
        }
    }
    return -1
}
A11
B0
C7
D-1
Attempts:
2 left
๐Ÿ’ก Hint

If no even number is found, what does the function return?

๐Ÿ”ง Debug
advanced
2:30remaining
Why does this Go function always return 0?

Look at this function. It is supposed to return the first even number from the slice, but it always returns 0. Why?

Go
func findFirstEven(nums []int) int {
    for i := 0; i < len(nums); i++ {
        if nums[i]%2 == 0 {
            break
        }
    }
    return 0
}
AThe function returns inside the loop, so it exits too early.
BThe loop condition is wrong and never runs.
CThe function uses break instead of return, so it never returns the even number found.
DThe function returns -1 instead of 0.
Attempts:
2 left
๐Ÿ’ก Hint

What does break do inside a loop?

๐Ÿ“ Syntax
advanced
2:00remaining
Which option causes a compile error in Go?

Which of these code snippets will cause a compile error due to incorrect use of return inside a loop?

A
func findFirstEven(nums []int) int {
    for _, n := range nums {
        if n%2 == 0 {
            return n
        }
    }
}
B
1- nruter
}
}    
n nruter        
{ 0 == 2%n fi    
{ smun egnar =: n ,_ rof
C
for _, n := range nums {
    if n%2 == 0 {
        return n
    }
}
return -1
D
for i := 0; i &lt; len(nums); i++ {
    if nums[i]%2 == 0 {
        return nums[i]
    }
}
return -1
Attempts:
2 left
๐Ÿ’ก Hint

Check if the function always returns a value on all paths.

๐Ÿš€ Application
expert
3:00remaining
What is the output of this Go program with nested loops and return?

What will this Go program print?

Go
package main
import "fmt"

func findPair(nums []int, target int) (int, int) {
    for i := 0; i < len(nums); i++ {
        for j := i + 1; j < len(nums); j++ {
            if nums[i]+nums[j] == target {
                return nums[i], nums[j]
            }
        }
    }
    return -1, -1
}

func main() {
    a, b := findPair([]int{1, 3, 5, 7, 9}, 12)
    fmt.Println(a, b)
}
A3 9
B5 7
C-1 -1
D1 11
Attempts:
2 left
๐Ÿ’ก Hint

The function returns the first pair that sums to the target.