Challenge - 5 Problems
Struct Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of embedded struct field access
What is the output of this Go program?
Go
package main import "fmt" type Address struct { City string } type Person struct { Name string Address } func main() { p := Person{Name: "Alice", Address: Address{City: "Paris"}} fmt.Println(p.City) }
Attempts:
2 left
💡 Hint
Embedded structs allow direct access to their fields.
✗ Incorrect
In Go, embedding a struct allows you to access its fields directly from the outer struct. Here, p.City accesses Address.City.
❓ Predict Output
intermediate2:00remaining
Output of struct pointer modification
What will be printed by this Go program?
Go
package main import "fmt" type Counter struct { Count int } func (c *Counter) Increment() { c.Count++ } func main() { c := Counter{Count: 5} c.Increment() fmt.Println(c.Count) }
Attempts:
2 left
💡 Hint
Pointer receiver methods can be called on values; Go handles the address automatically.
✗ Incorrect
Go automatically takes the address of c to call the pointer receiver method Increment, which increments Count from 5 to 6.
🔧 Debug
advanced2:00remaining
Identify the cause of compilation error
This Go code fails to compile. What is the cause?
Go
package main import "fmt" type Data struct { value int } func main() { d := Data{value: 10} fmt.Println(d.Value) }
Attempts:
2 left
💡 Hint
Check the exact spelling and capitalization of struct fields.
✗ Incorrect
Go is case-sensitive. The struct field is named 'value' (lowercase), but the code tries to access 'Value' (uppercase), causing a compilation error.
🧠 Conceptual
advanced2:00remaining
Effect of struct value vs pointer receiver on method behavior
Consider a struct with two methods: one with a value receiver and one with a pointer receiver. Which statement is true about their behavior when called on a struct value?
Attempts:
2 left
💡 Hint
Think about how Go passes the struct to each method type.
✗ Incorrect
Value receiver methods get a copy of the struct, so changes do not affect the original. Pointer receiver methods get the address and can modify the original struct.
❓ Predict Output
expert2:00remaining
Output of struct embedding with method overriding
What is the output of this Go program?
Go
package main import "fmt" type Animal struct {} func (a Animal) Speak() string { return "..." } type Dog struct { Animal } func (d Dog) Speak() string { return "Woof!" } func main() { var a Animal = Dog{} fmt.Println(a.Speak()) }
Attempts:
2 left
💡 Hint
Consider how method sets and interface assignments work in Go.
✗ Incorrect
The variable 'a' is of type Animal, not an interface. Assigning Dog{} to Animal slices off the Dog part, so calling Speak calls Animal's method, printing "...".