Golang Structs Quiz
40 comprehensive questions on Golang structs, covering struct creation, methods, embedded structs, anonymous structs, and value vs reference behavior — with 20 code examples demonstrating struct operations and composition.
Question 1
What is a struct in Golang?
Question 2
How do you declare a struct type in Golang?
Question 3
What will this basic struct declaration and usage print?
package main
import "fmt"
type Person struct {
name string
age int
}
func main() {
p := Person{"Alice", 30}
fmt.Println(p.name, p.age)
}Question 4
What is a struct literal in Golang?
Question 5
What will this named field struct literal print?
package main
import "fmt"
type Point struct {
x, y int
}
func main() {
p := Point{y: 10, x: 5}
fmt.Println(p.x, p.y)
}Question 6
What happens to uninitialized fields in a struct literal?
Question 7
What is a struct field tag in Golang?
Question 8
What will this struct with JSON tags marshal to?
package main
import (
"encoding/json"
"fmt"
)
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
u := User{"John", 25}
data, _ := json.Marshal(u)
fmt.Println(string(data))
}Question 9
What is a method in Golang?
Question 10
What will this struct method example print?
package main
import "fmt"
type Rectangle struct {
width, height int
}
func (r Rectangle) area() int {
return r.width * r.height
}
func main() {
rect := Rectangle{3, 4}
fmt.Println(rect.area())
}Question 11
What is the difference between value and pointer receivers?
Question 12
What will this pointer receiver method do?
package main
import "fmt"
type Counter struct {
value int
}
func (c *Counter) increment() {
c.value++
}
func main() {
counter := Counter{0}
counter.increment()
fmt.Println(counter.value)
}Question 13
What is struct embedding in Golang?
Question 14
What will this embedded struct example print?
package main
import "fmt"
type Person struct {
name string
}
type Employee struct {
Person
salary int
}
func main() {
emp := Employee{
Person: Person{"John"},
salary: 50000,
}
fmt.Println(emp.name, emp.salary)
}Question 15
What happens to methods when you embed structs?
Question 16
What will this embedded method example do?
package main
import "fmt"
type Speaker interface {
speak()
}
type Person struct {
name string
}
func (p Person) speak() {
fmt.Println("Hello, I am", p.name)
}
type Employee struct {
Person
salary int
}
func main() {
emp := Employee{Person{"John"}, 50000}
emp.speak()
}Question 17
What is an anonymous struct in Golang?
Question 18
What will this anonymous struct example print?
package main
import "fmt"
func main() {
person := struct {
name string
age int
}{
name: "Alice",
age: 30,
}
fmt.Println(person.name, person.age)
}Question 19
What is the difference between value and reference semantics for structs?
Question 20
What will this struct assignment example show?
package main
import "fmt"
type Point struct {
x, y int
}
func main() {
p1 := Point{1, 2}
p2 := p1 // Copy
p2.x = 10
fmt.Println(p1.x, p2.x)
}Question 21
When should you use pointers to structs instead of struct values?
Question 22
What will this pointer to struct example do?
package main
import "fmt"
type Point struct {
x, y int
}
func modify(p *Point) {
p.x = 100
}
func main() {
pt := Point{1, 2}
modify(&pt)
fmt.Println(pt.x)
}Question 23
What is a struct field that is itself a struct called?
Question 24
What will this nested struct example print?
package main
import "fmt"
type Address struct {
street string
city string
}
type Person struct {
name string
address Address
}
func main() {
p := Person{
name: "John",
address: Address{"Main St", "NYC"},
}
fmt.Println(p.address.city)
}Question 25
What is a zero value struct?
Question 26
What will this zero value struct example print?
package main
import "fmt"
type Person struct {
name string
age int
}
func main() {
var p Person
fmt.Println(p.name, p.age)
}Question 27
Can structs contain methods as fields?
Question 28
What is the empty struct type in Golang?
Question 29
What will this empty struct example do?
package main
import "fmt"
func main() {
m := make(map[string]struct{})
m["key"] = struct{}{}
_, exists := m["key"]
fmt.Println(exists)
}Question 30
What is struct field visibility in Golang?
Question 31
What happens when you compare two structs?
Question 32
What will this struct comparison example print?
package main
import "fmt"
type Point struct {
x, y int
}
func main() {
p1 := Point{1, 2}
p2 := Point{1, 2}
p3 := Point{2, 3}
fmt.Println(p1 == p2, p1 == p3)
}Question 33
What is a struct tag used for in practice?
Question 34
What is the main advantage of composition over inheritance in Golang?
Question 35
What is a common pitfall with embedded structs?
Question 36
What will this field conflict in embedding example do?
package main
import "fmt"
type A struct {
x int
}
type B struct {
x int
}
type C struct {
A
B
}
func main() {
c := C{A{1}, B{2}}
fmt.Println(c.x) // Error: ambiguous
}Question 37
What is the difference between struct{} and interface{}?
Question 38
When should you use anonymous structs?
Question 39
What is the performance implication of passing large structs by value?
Question 40
What is the key principle for effective struct design in Golang?
