Control Flow & Functions Questions with Answers

10 practice questions on Control Flow & Functions (Go) with answers and step-by-step solutions. Try 10 samples below, then practise all 10 free in the AptiNest app.

Sample questions

Question 1Easy

Which loop keyword does Go provide?

  1. for, while and do-while
  2. for and while
  3. Only for
  4. for and foreach

Answer: C. Only for

Explanation

Go has just for. A condition-only for cond {} acts like while, and for range iterates collections.

Step-by-step solution

  1. for init; cond; post {} is the classic loop.
  2. for cond {} replaces while.
  3. for range x {} replaces foreach.

Why the other options are wrong

  • A. Go has neither while nor do-while.
  • B. There is no while keyword.
  • D. There is no foreach keyword.

Question 2Easy

What is printed?

for i := 0; i < 3; i++ {
    defer fmt.Print(i)
}
  1. 012
  2. 210
  3. 000
  4. Nothing is printed

Answer: B. 210

Explanation

Each defer runs when the function returns, in last-in-first-out order. The arguments are fixed when each defer statement runs, so the values 2, 1 and 0 print in that order.

Step-by-step solution

  1. Each loop pass runs a defer statement that saves the current value of i.
  2. The saved values are 0, 1 and 2.
  3. Deferred calls run last-in-first-out, so 2, 1 and 0 print in that order.

Why the other options are wrong

  • A. Deferred calls do not run first-in-first-out; the last one deferred runs first.
  • C. Each defer saves the value of i at that moment, so the values differ.
  • D. Deferred calls still run when the function returns.

Tip: Defers are LIFO, and their arguments are fixed when deferred.

Question 3Medium

What is printed?

x := 2
switch x {
case 1:
    fmt.Print("a")
case 2:
    fmt.Print("b")
case 3:
    fmt.Print("c")
}
  1. b
  2. bc
  3. abc
  4. Nothing

Answer: A. b

Explanation

Go switch cases do not fall through; only the matching case runs, then the switch ends.

Step-by-step solution

  1. x equals 2.
  2. case 2 prints b.
  3. No fall through to case 3.

Why the other options are wrong

  • B. There is no implicit fall through.
  • C. Earlier cases do not run.
  • D. A matching case runs.

Question 4Medium

How do you pass a slice nums to a function declared func sum(vals ...int) int?

  1. sum(nums)
  2. sum(...nums)
  3. sum(nums...)
  4. sum(nums[])

Answer: C. sum(nums...)

Explanation

Adding ... after a slice argument unpacks it into the variadic parameter.

Step-by-step solution

  1. The parameter vals ...int accepts any number of ints.
  2. Append ... after the slice.
  3. sum(nums...) passes the slice itself.

Why the other options are wrong

  • A. A []int is not an int, so it needs the unpacking suffix.
  • B. The ellipsis goes after the name.
  • D. nums[] is not valid syntax.

Question 5Medium

What is printed?

x := 1
defer fmt.Println("deferred:", x)
x = 2
fmt.Println("now:", x)
  1. now: 2
    deferred: 1

  2. deferred: 2
    now: 2

  3. deferred: 1
    now: 2

  4. now: 2
    deferred: 2

Answer: A.

Explanation

The arguments of a deferred call are evaluated when the defer statement runs, so x is 1 at that point. The deferred Println runs last, after now: 2 is printed.

Step-by-step solution

  1. The defer line evaluates its arguments right away, so x is 1 for that call.
  2. x = 2 changes the variable, but not the saved argument.
  3. now: 2 prints first; the deferred call prints deferred: 1 when the function ends.

Why the other options are wrong

  • B. The deferred call keeps the value 1 from when the defer ran.
  • C. The order is reversed: the deferred line runs at the end, after now: 2.
  • D. The deferred call keeps the value 1, not the later 2.

Tip: Defer arguments are evaluated at the defer line; the call runs later.

Question 6Medium

What is printed?

func f() (n int) {
    defer func() { n *= 2 }()
    return 5
}

func main() {
    fmt.Println(f())
}
  1. 5
  2. 0
  3. Compile error
  4. 10

Answer: D. 10

Explanation

return 5 sets the named result n to 5 before the deferred function runs. The deferred closure then doubles n, so f returns 10.

Step-by-step solution

  1. return 5 assigns 5 to the named result n.
  2. The deferred closure runs after the return and changes n to n * 2.
  3. f returns 10, which Println prints.

Why the other options are wrong

  • A. The deferred function runs after return and changes the named result.
  • B. The return statement sets n to 5 before the defer runs.
  • C. A deferred closure can change a named result; this compiles.

Tip: A defer can change a named result after return.

Question 7Medium

What is printed?

x := 1
switch x {
case 1:
    fmt.Print("a")
    fallthrough
case 2:
    fmt.Print("b")
case 3:
    fmt.Print("c")
}
  1. a
  2. abc
  3. b
  4. ab

Answer: D. ab

Explanation

fallthrough makes execution continue into the next case body, without testing its condition, and only that one case.

Step-by-step solution

  1. case 1 prints a.
  2. fallthrough moves into case 2.
  3. case 2 prints b and stops.

Why the other options are wrong

  • A. fallthrough does run the next body.
  • B. It does not run case 3 as well.
  • C. Case 1 still prints its a.

Question 8Hard

What is printed?

func count(xs ...int) {
    fmt.Println(xs == nil, len(xs))
}

func main() {
    count()
    count([]int{}...)
}
  1. true 0
    true 0

  2. false 0
    true 0

  3. true 0
    false 0

  4. false 0
    false 0

Answer: C.

Explanation

With no arguments, the variadic parameter xs is nil. Passing an existing empty slice with ... gives a non-nil slice of length 0, so xs == nil is false.

Step-by-step solution

  1. count() passes no values, so xs is nil: true and length 0.
  2. count([]int{}...) passes an existing empty slice, which is not nil.
  3. So the second line prints false 0.

Why the other options are wrong

  • A. Passing an empty slice with ... gives a non-nil slice, so the second line is false.
  • B. A call with no arguments gives nil, so the first line is true, and the order is not swapped.
  • D. A call with no arguments gives a nil slice, so the first line is true.

Tip: Nil variadic = no arguments; ... with an empty slice is non-nil.

Question 9Hard

What is printed?

func f() int {
    x := 0
    defer func() { x++ }()
    return x
}

func main() {
    fmt.Println(f())
}
  1. 1
  2. 0
  3. Compile error
  4. 2

Answer: B. 0

Explanation

return x copies the value 0 into the result before the deferred closure runs. The closure then increments the local x to 1, but the returned value is already fixed at 0.

Step-by-step solution

  1. return x copies x (0) into the result.
  2. The deferred closure increments the local x to 1.
  3. The result still holds 0, so Println prints 0.

Why the other options are wrong

  • A. The closure changes the local x, but the return value was copied before it ran.
  • C. x is used and the closure captures it; the code compiles.
  • D. The deferred closure runs once and adds 1, so the result is never 2.

Tip: A plain return copies the value; use a named result to change it in a defer.

Question 10Hard

With the language version 1.22 or newer, what is printed?

// go.mod says: go 1.22
var fs []func()
for i := 0; i < 3; i++ {
    fs = append(fs, func() { fmt.Print(i, " ") })
}
for _, f := range fs {
    f()
}
  1. 3 3 3
  2. 0 1 2
  3. 2 2 2
  4. 0 0 0

Answer: B. 0 1 2

Explanation

From Go 1.22 each iteration of a for loop has its own copy of the loop variable, so each closure sees its own value. Before 1.22 all closures shared one variable and printed 3 3 3.

Step-by-step solution

  1. Each iteration creates a fresh i.
  2. Each closure captures its own i.
  3. Calls print 0, 1, 2.

Why the other options are wrong

  • A. That was the pre 1.22 behaviour.
  • C. The closures do not all see the last value.
  • D. Values differ per iteration.

Practise all 10 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Go topics · All topics