Challenge - 5 Problems
Go Compilation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of a simple Go program with init and main
What is the output of this Go program when compiled and run?
Go
package main import "fmt" func init() { fmt.Println("Init function called") } func main() { fmt.Println("Main function called") }
Attempts:
2 left
๐ก Hint
Remember that init functions run before main in Go.
โ Incorrect
In Go, the init function runs automatically before main. So the output first prints from init, then from main.
โ Predict Output
intermediate2:00remaining
Output of Go program with multiple init functions
What is the output of this Go program with two init functions in the same package?
Go
package main import "fmt" func init() { fmt.Println("First init") } func init() { fmt.Println("Second init") } func main() { fmt.Println("Main function") }
Attempts:
2 left
๐ก Hint
Multiple init functions run in the order they appear in the source file.
โ Incorrect
Go runs multiple init functions in the order they are declared in the source file before main.
โ Predict Output
advanced2:00remaining
Output of Go program with package-level variable initialization
What is the output of this Go program considering package-level variable initialization order?
Go
package main import "fmt" var a = initializeA() var b = initializeB() func initializeA() int { fmt.Println("Initializing A") return 1 } func initializeB() int { fmt.Println("Initializing B") return 2 } func main() { fmt.Println("a =", a, ", b =", b) }
Attempts:
2 left
๐ก Hint
Package-level variables are initialized in the order they are declared.
โ Incorrect
Variables a and b are initialized in order. So initializeA runs first, then initializeB, then main prints values.
โ Predict Output
advanced2:00remaining
Output of Go program with multiple packages and init functions
Given two packages, main and util, what is the output when running the main package?
Go
package util import "fmt" func init() { fmt.Println("util init") } func UtilFunc() { fmt.Println("util function") } --- package main import ( "fmt" "util" ) func init() { fmt.Println("main init") } func main() { fmt.Println("main function") util.UtilFunc() }
Attempts:
2 left
๐ก Hint
Package init functions run before main, and dependencies init first.
โ Incorrect
Go initializes imported packages first, so util's init runs before main's init, then main runs.
๐ง Conceptual
expert2:00remaining
Order of compilation and linking in Go build process
Which of the following correctly describes the order of steps in Go's compilation and execution flow when building and running a program?
Attempts:
2 left
๐ก Hint
Think about how source code is processed step-by-step before running.
โ Incorrect
Go first parses source code, then checks types, compiles to object files, links them, then runs the program.