Golang Structs Quiz

Golang
0 Passed
0% acceptance

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.

40 Questions
~80 minutes
1

Question 1

What is a struct in Golang?

A
A composite data type that groups together variables under a single name
B
A function that returns multiple values
C
A type of loop
D
A built-in data type
2

Question 2

How do you declare a struct type in Golang?

A
type StructName struct { field1 Type1; field2 Type2 }
B
struct StructName { field1 Type1; field2 Type2 }
C
class StructName { field1 Type1; field2 Type2 }
D
var StructName struct { field1 Type1; field2 Type2 }
3

Question 3

What will this basic struct declaration and usage print?

go
package main

import "fmt"

type Person struct {
    name string
    age  int
}

func main() {
    p := Person{"Alice", 30}
    fmt.Println(p.name, p.age)
}
A
Alice 30
B
Alice
C
Compilation error
D
30 Alice
4

Question 4

What is a struct literal in Golang?

A
A way to create and initialize struct instances: TypeName{field1: value1, field2: value2}
B
A string representation of a struct
C
A pointer to a struct
D
A method on structs
5

Question 5

What will this named field struct literal print?

go
package main

import "fmt"

type Point struct {
    x, y int
}

func main() {
    p := Point{y: 10, x: 5}
    fmt.Println(p.x, p.y)
}
A
5 10
B
10 5
C
Compilation error
D
0 0
6

Question 6

What happens to uninitialized fields in a struct literal?

A
They get their zero values (0 for numbers, empty string for strings, nil for pointers, etc.)
B
They cause compilation errors
C
They get random values
D
They must all be initialized
7

Question 7

What is a struct field tag in Golang?

A
A string literal that appears after the field type, used by reflection and encoding packages
B
A comment on struct fields
C
A way to initialize fields
D
A method attached to fields
8

Question 8

What will this struct with JSON tags marshal to?

go
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))
}
A
{"name":"John","age":25}
B
{"Name":"John","Age":25}
C
Compilation error
D
John 25
9

Question 9

What is a method in Golang?

A
A function attached to a type via a receiver parameter
B
A special kind of function
C
A field in a struct
D
A built-in operation
10

Question 10

What will this struct method example print?

go
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())
}
A
12
B
7
C
Compilation error
D
0
11

Question 11

What is the difference between value and pointer receivers?

A
Value receivers work on copies, pointer receivers work on the original instance
B
They are identical
C
Value receivers are for reading, pointer for writing
D
Pointer receivers don't exist
12

Question 12

What will this pointer receiver method do?

go
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)
}
A
Prints 1
B
Prints 0
C
Compilation error
D
Runtime panic
13

Question 13

What is struct embedding in Golang?

A
Including one struct as a field in another without a field name, enabling composition
B
Creating nested structs
C
Inheriting methods from parent structs
D
Merging two structs into one
14

Question 14

What will this embedded struct example print?

go
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)
}
A
John 50000
B
John
C
Compilation error
D
50000
15

Question 15

What happens to methods when you embed structs?

A
Methods of embedded types are promoted and can be called on the outer type
B
Methods are not accessible
C
Only pointer methods are promoted
D
Methods must be redefined
16

Question 16

What will this embedded method example do?

go
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()
}
A
Prints "Hello, I am John"
B
Compilation error
C
Prints nothing
D
Runtime panic
17

Question 17

What is an anonymous struct in Golang?

A
A struct type defined inline without a name, used for one-off data structures
B
A struct without fields
C
A struct that cannot be assigned to variables
D
A built-in struct type
18

Question 18

What will this anonymous struct example print?

go
package main

import "fmt"

func main() {
    person := struct {
        name string
        age  int
    }{
        name: "Alice",
        age:  30,
    }
    fmt.Println(person.name, person.age)
}
A
Alice 30
B
Alice
C
Compilation error
D
30 Alice
19

Question 19

What is the difference between value and reference semantics for structs?

A
Value semantics means assignment/copy creates independent copies, reference semantics means sharing
B
Value semantics is for primitives, reference for structs
C
They are the same in Golang
D
Reference semantics doesn't exist in Golang
20

Question 20

What will this struct assignment example show?

go
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)
}
A
1 10
B
10 10
C
Compilation error
D
1 2
21

Question 21

When should you use pointers to structs instead of struct values?

A
When you need to modify the original struct or avoid copying large structs
B
Always, for better performance
C
Never, value semantics are better
D
Only for method receivers
22

Question 22

What will this pointer to struct example do?

go
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)
}
A
Prints 100
B
Prints 1
C
Compilation error
D
Runtime panic
23

Question 23

What is a struct field that is itself a struct called?

A
A nested struct or embedded struct (if anonymous)
B
A recursive struct
C
A pointer struct
D
An interface struct
24

Question 24

What will this nested struct example print?

go
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)
}
A
NYC
B
Main St
C
Compilation error
D
John
25

Question 25

What is a zero value struct?

A
A struct where all fields have their zero values
B
An empty struct
C
A nil struct
D
A struct with default values
26

Question 26

What will this zero value struct example print?

go
package main

import "fmt"

type Person struct {
    name string
    age  int
}

func main() {
    var p Person
    fmt.Println(p.name, p.age)
}
A
0
B
nil 0
C
Compilation error
D
undefined
27

Question 27

Can structs contain methods as fields?

A
No, methods are attached to types via receivers, not stored as fields
B
Yes, like function fields
C
Only pointer methods
D
Only value methods
28

Question 28

What is the empty struct type in Golang?

A
struct{} - a struct with no fields, useful for signaling and memory efficiency
B
A nil struct
C
An invalid struct
D
A struct with zero size
29

Question 29

What will this empty struct example do?

go
package main

import "fmt"

func main() {
    m := make(map[string]struct{})
    m["key"] = struct{}{}
    _, exists := m["key"]
    fmt.Println(exists)
}
A
true
B
false
C
Compilation error
D
nil
30

Question 30

What is struct field visibility in Golang?

A
Fields starting with capital letters are exported (public), lowercase are unexported (private)
B
All fields are public
C
All fields are private
D
Visibility is controlled by keywords
31

Question 31

What happens when you compare two structs?

A
Structs are comparable if all their fields are comparable, comparison is field-by-field
B
Structs cannot be compared
C
Only pointer comparison is allowed
D
Comparison always returns true
32

Question 32

What will this struct comparison example print?

go
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)
}
A
true false
B
false true
C
true true
D
Compilation error
33

Question 33

What is a struct tag used for in practice?

A
Controlling JSON field names, database column names, and validation rules
B
Adding comments to fields
C
Initializing field values
D
Creating methods
34

Question 34

What is the main advantage of composition over inheritance in Golang?

A
Composition is more flexible and avoids the problems of multiple inheritance
B
Composition is faster
C
Inheritance doesn't exist in Golang
D
Composition uses less memory
35

Question 35

What is a common pitfall with embedded structs?

A
Field name conflicts when multiple embedded structs have the same field names
B
Embedded structs cannot have methods
C
Embedding creates inheritance
D
Embedded fields are not accessible
36

Question 36

What will this field conflict in embedding example do?

go
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
}
A
Compilation error
B
Prints 1
C
Prints 2
D
Prints 0
37

Question 37

What is the difference between struct{} and interface{}?

A
struct{} is an empty struct type, interface{} is the empty interface that accepts any type
B
They are the same
C
struct{} is for data, interface{} is for behavior
D
interface{} doesn't exist
38

Question 38

When should you use anonymous structs?

A
For one-off data structures, JSON unmarshaling, or temporary aggregations
B
For all struct definitions
C
When you need reusable types
D
Never, named structs are better
39

Question 39

What is the performance implication of passing large structs by value?

A
It copies the entire struct, which can be expensive for large structs
B
It always uses pointers internally
C
It has no performance impact
D
It prevents method calls
40

Question 40

What is the key principle for effective struct design in Golang?

A
Group related data together, use composition over inheritance, and choose value vs pointer semantics carefully
B
Make all structs as large as possible
C
Avoid methods on structs
D
Use inheritance hierarchies

QUIZZES IN Golang