Challenge - 5 Problems
Nested Structs Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested struct field access
What is the output of this Go program?
Go
package main import "fmt" type Address struct { City string Zip int } type Person struct { Name string Address Address } func main() { p := Person{Name: "Alice", Address: Address{City: "Paris", Zip: 75000}} fmt.Println(p.Address.City) }
Attempts:
2 left
💡 Hint
Look at how nested structs are accessed using dot notation.
✗ Incorrect
The field City is inside the nested Address struct. Accessing p.Address.City prints "Paris".
❓ Predict Output
intermediate2:00remaining
Output of nested struct pointer access
What will this Go program print?
Go
package main import "fmt" type Engine struct { Horsepower int } type Car struct { Model string Engine *Engine } func main() { e := Engine{Horsepower: 300} c := Car{Model: "Mustang", Engine: &e} fmt.Println(c.Engine.Horsepower) }
Attempts:
2 left
💡 Hint
Remember to dereference pointers automatically with dot notation.
✗ Incorrect
The Engine field is a pointer to Engine struct. Accessing c.Engine.Horsepower prints 300.
🔧 Debug
advanced2:00remaining
Identify the compilation error in nested struct initialization
This Go code does not compile. What is the cause of the error?
Go
package main import "fmt" type Point struct { X, Y int } type Rectangle struct { TopLeft, BottomRight Point } func main() { r := Rectangle{TopLeft: Point{X: 0, Y: 10}, BottomRight: Point{X: 5, Y: 0}} fmt.Println(r) }
Attempts:
2 left
💡 Hint
Check how nested structs are initialized in Go.
✗ Incorrect
In Go, nested struct literals must include the type name. Using {X:0, Y:10} alone causes a syntax error.
❓ Predict Output
advanced2:00remaining
Output of embedded struct field promotion
What does this Go program print?
Go
package main import "fmt" type Contact struct { Email string } type Employee struct { Name string Contact } func main() { e := Employee{Name: "Bob", Contact: Contact{Email: "bob@example.com"}} fmt.Println(e.Email) }
Attempts:
2 left
💡 Hint
Embedded structs promote their fields to the outer struct.
✗ Incorrect
The embedded Contact struct's Email field is promoted, so e.Email accesses it directly.
🧠 Conceptual
expert2:00remaining
Number of fields in nested and embedded structs
Given these struct definitions, how many fields does variable 'd' of type Developer have?
Go
type Address struct {
City string
Zip int
}
type Contact struct {
Email string
Phone string
}
type Developer struct {
Name string
Address
Contact
}Attempts:
2 left
💡 Hint
Count all fields including embedded struct fields promoted to Developer.
✗ Incorrect
Developer has Name plus all fields from Address (City, Zip) and Contact (Email, Phone), total 5 fields.