Challenge - 5 Problems
Struct Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of struct field access
What is the output of this Go program?
Go
package main import "fmt" type Person struct { Name string Age int } func main() { p := Person{Name: "Alice", Age: 30} fmt.Println(p.Name) }
Attempts:
2 left
💡 Hint
Look at which field is printed with fmt.Println.
✗ Incorrect
The program creates a Person struct with Name "Alice" and Age 30. It prints p.Name, so output is "Alice".
❓ Predict Output
intermediate2:00remaining
Output when struct fields are uninitialized
What is the output of this Go program?
Go
package main import "fmt" type Point struct { X int Y int } func main() { var p Point fmt.Println(p.X, p.Y) }
Attempts:
2 left
💡 Hint
Uninitialized int fields default to zero.
✗ Incorrect
In Go, int fields default to 0 if not initialized. So p.X and p.Y print as 0 0.
🔧 Debug
advanced2:00remaining
Identify the error in struct initialization
What error does this Go code produce?
Go
package main import "fmt" type Car struct { Brand string Year int } func main() { c := Car{Brand: "Toyota", Year: 2020} fmt.Println(c) }
Attempts:
2 left
💡 Hint
Check how struct fields are initialized with names and values.
✗ Incorrect
The struct is correctly initialized with named fields. This code compiles and prints {Toyota 2020}.
❓ Predict Output
advanced2:00remaining
Output of nested structs
What is the output of this Go program?
Go
package main import "fmt" type Address struct { City string State string } type User struct { Name string Address Address } func main() { u := User{Name: "Bob", Address: Address{City: "NYC", State: "NY"}} fmt.Println(u.Address.City) }
Attempts:
2 left
💡 Hint
Look at which nested field is printed.
✗ Incorrect
The program prints u.Address.City which is "NYC".
🧠 Conceptual
expert2:00remaining
Number of fields in an anonymous struct
How many fields does this struct have?
Go
var s = struct {
A int
B string
C struct {
D float64
E bool
}
}{}Attempts:
2 left
💡 Hint
Count only the top-level fields of the struct.
✗ Incorrect
The struct has three top-level fields: A, B, and C. The nested struct inside C is one field itself.