0
0
Goprogramming~20 mins

Go compilation and execution flow - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Go Compilation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2: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")
}
AInit function called\nMain function called
BMain function called\nInit function called
CInit function called
DMain function called
Attempts:
2 left
๐Ÿ’ก Hint
Remember that init functions run before main in Go.
โ“ Predict Output
intermediate
2: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")
}
AMain function\nFirst init\nSecond init
BFirst init\nMain function\nSecond init
CSecond init\nFirst init\nMain function
DFirst init\nSecond init\nMain function
Attempts:
2 left
๐Ÿ’ก Hint
Multiple init functions run in the order they appear in the source file.
โ“ Predict Output
advanced
2: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)
}
AInitializing A\nInitializing B\na = 1 , b = 2
Ba = 0 , b = 0
Ca = 1 , b = 2\nInitializing A\nInitializing B
DInitializing B\nInitializing A\na = 1 , b = 2
Attempts:
2 left
๐Ÿ’ก Hint
Package-level variables are initialized in the order they are declared.
โ“ Predict Output
advanced
2: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()
}
Amain function\nutil init\nmain init\nutil function
Butil init\nmain init\nmain function\nutil function
Cmain init\nutil init\nmain function\nutil function
Dutil function\nutil init\nmain init\nmain function
Attempts:
2 left
๐Ÿ’ก Hint
Package init functions run before main, and dependencies init first.
๐Ÿง  Conceptual
expert
2: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?
AExecution โ†’ Linking โ†’ Compilation to object files โ†’ Type checking โ†’ Source code parsing
BLinking โ†’ Compilation to object files โ†’ Type checking โ†’ Source code parsing โ†’ Execution
CSource code parsing โ†’ Type checking โ†’ Compilation to object files โ†’ Linking โ†’ Execution
DCompilation to object files โ†’ Source code parsing โ†’ Type checking โ†’ Linking โ†’ Execution
Attempts:
2 left
๐Ÿ’ก Hint
Think about how source code is processed step-by-step before running.