0
0
Goprogramming~10 mins

Defer execution order in Go - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to defer printing "First".

Go
package main
import "fmt"
func main() {
    [1] fmt.Println("First")
    fmt.Println("Second")
}
Drag options to blanks, or click blank then click option'
Ago
Bdefer
Cfunc
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'go' instead of 'defer' causes the function to run concurrently.
Forgetting 'defer' causes immediate execution.
2fill in blank
medium

Complete the code to print "Third" last using defer.

Go
package main
import "fmt"
func main() {
    defer fmt.Println("[1]")
    fmt.Println("First")
    fmt.Println("Second")
}
Drag options to blanks, or click blank then click option'
AThird
BFirst
CSecond
DLast
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'First' or 'Second' will print them immediately, not deferred.
Using 'Last' is not the exact string to print.
3fill in blank
hard

Fix the error in the defer statement to print "Done" last.

Go
package main
import "fmt"
func main() {
    defer fmt.Println([1])
    fmt.Println("Start")
}
Drag options to blanks, or click blank then click option'
Adone
BDone
C"Done"
D'Done'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted Done causes a compile error.
Using single quotes 'Done' is invalid for strings.
4fill in blank
hard

Fill both blanks to defer two prints so "Second" prints before "First".

Go
package main
import "fmt"
func main() {
    defer fmt.Println([1])
    defer fmt.Println([2])
    fmt.Println("Start")
}
Drag options to blanks, or click blank then click option'
A"First"
B"Start"
C"Second"
D"End"
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the strings will print in the wrong order.
Using unrelated strings confuses the output order.
5fill in blank
hard

Fill all three blanks to defer prints so output order is: Start, Third, Second, First.

Go
package main
import "fmt"
func main() {
    fmt.Println("Start")
    defer fmt.Println([1])
    defer fmt.Println([2])
    defer fmt.Println([3])
}
Drag options to blanks, or click blank then click option'
A"Start"
B"First"
C"Second"
D"Third"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing the order of deferred prints changes output order.
Not quoting strings causes compile errors.