Bird
Raised Fist0
Goprogramming~20 mins

Defining structs in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Struct Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
}
AAlice
B30
CPerson{Name: "Alice", Age: 30}
DCompilation error
Attempts:
2 left
💡 Hint
Look at which field is printed with fmt.Println.
❓ Predict Output
intermediate
2: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)
}
Anil nil
BCompilation error: missing initialization
C0 0
DRandom values
Attempts:
2 left
💡 Hint
Uninitialized int fields default to zero.
🔧 Debug
advanced
2: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)
}
ASyntax error: mixed field names and values
BCompilation error: missing field Year
CRuntime panic: invalid type conversion
DNo error, prints {Toyota 2020}
Attempts:
2 left
💡 Hint
Check how struct fields are initialized with names and values.
❓ Predict Output
advanced
2: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)
}
ANY
BNYC
C{City: NYC, State: NY}
DCompilation error
Attempts:
2 left
💡 Hint
Look at which nested field is printed.
🧠 Conceptual
expert
2: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
  }
}{}
A3
B5
C2
D4
Attempts:
2 left
💡 Hint
Count only the top-level fields of the struct.

Practice

(1/5)
1. What is the purpose of defining a struct in Go?
easy
A. To create a new function
B. To group related data fields into a single unit
C. To declare a package
D. To import external libraries

Solution

  1. Step 1: Understand the role of structs

    Structs are used to group related data fields together in one unit, like a blueprint for an object.
  2. Step 2: Compare with other options

    Functions, packages, and imports serve different purposes unrelated to grouping data.
  3. Final Answer:

    To group related data fields into a single unit -> Option B
  4. Quick Check:

    Struct = grouped data [OK]
Hint: Structs bundle related info like a real-world object [OK]
Common Mistakes:
  • Confusing structs with functions
  • Thinking structs declare packages
  • Mixing structs with imports
2. Which of the following is the correct way to define a struct named Person with fields Name (string) and Age (int)?
easy
A. type Person { Name string; Age int }
B. struct Person { Name string; Age int }
C. type Person struct { Name string; Age int }
D. Person struct { Name string; Age int }

Solution

  1. Step 1: Recall Go struct syntax

    In Go, structs are defined using the syntax: type Name struct { fields }.
  2. Step 2: Check each option

    type Person struct { Name string; Age int } matches the correct syntax. Options A, B, and C miss the 'type' keyword or 'struct' keyword placement.
  3. Final Answer:

    type Person struct { Name string; Age int } -> Option C
  4. Quick Check:

    Correct struct syntax = type Person struct { Name string; Age int } [OK]
Hint: Remember: 'type Name struct { fields }' [OK]
Common Mistakes:
  • Omitting 'type' keyword
  • Missing 'struct' keyword
  • Incorrect order of keywords
3. What will be the output of the following Go code?
package main
import "fmt"
type Car struct {
  Brand string
  Year  int
}
func main() {
  c := Car{Brand: "Toyota", Year: 2020}
  fmt.Println(c.Brand)
}
medium
A. Car{Brand: "Toyota", Year: 2020}
B. 2020
C. Compilation error
D. Toyota

Solution

  1. Step 1: Understand struct initialization

    The struct Car is initialized with Brand="Toyota" and Year=2020.
  2. Step 2: Identify printed field

    The code prints c.Brand, which is "Toyota".
  3. Final Answer:

    Toyota -> Option D
  4. Quick Check:

    Print c.Brand = Toyota [OK]
Hint: Print field with dot: variable.FieldName [OK]
Common Mistakes:
  • Printing the whole struct instead of a field
  • Confusing field names
  • Expecting Year instead of Brand
4. Identify the error in the following Go code snippet:
package main
import "fmt"
type Book struct {
  Title string
  Pages int
}

func main() {
  b := Book{Title: "Go Guide", Pages: "300"}
  fmt.Println(b)
}
medium
A. Pages field has wrong type, should be int not string
B. Missing import for fmt package
C. Struct fields must be lowercase
D. Cannot print struct directly

Solution

  1. Step 1: Check field types in struct initialization

    The Pages field is declared as int but assigned a string "300" which is a type mismatch.
  2. Step 2: Verify other possible errors

    Import for fmt package is present. Struct fields can be uppercase. Printing structs directly is allowed.
  3. Final Answer:

    Pages field has wrong type, should be int not string -> Option A
  4. Quick Check:

    Type mismatch in struct field assignment [OK]
Hint: Match field types exactly when initializing structs [OK]
Common Mistakes:
  • Assigning string to int field
  • Expecting compilation error from printing struct
  • Thinking struct fields must be lowercase
5. Given the struct definition:
type Employee struct {
  Name   string
  Salary int
  Active bool
}
Which of the following code snippets correctly creates a slice of Employee structs with only active employees having salary above 50000?
hard
A. emps := []Employee{{"Alice", 60000, true}, {"Bob", 45000, true}, {"Carol", 70000, false}} var filtered []Employee for _, e := range emps { if e.Active && e.Salary > 50000 { filtered = append(filtered, e) } }
B. emps := []Employee{{"Alice", 60000, true}, {"Bob", 45000, true}, {"Carol", 70000, false}} filtered := []Employee{} for i := range emps { if emps[i].Salary > 50000 { filtered = append(filtered, emps[i]) } }
C. emps := []Employee{{"Alice", 60000, true}, {"Bob", 45000, true}, {"Carol", 70000, false}} filtered := []Employee{} for _, e := range emps { if e.Active || e.Salary > 50000 { filtered = append(filtered, e) } }
D. emps := []Employee{{"Alice", 60000, true}, {"Bob", 45000, true}, {"Carol", 70000, false}} filtered := []Employee{} for _, e := range emps { if e.Active && e.Salary < 50000 { filtered = append(filtered, e) } }

Solution

  1. Step 1: Understand filtering criteria

    We want only employees who are active and have salary above 50000.
  2. Step 2: Check each code snippet logic

    emps := []Employee{{"Alice", 60000, true}, {"Bob", 45000, true}, {"Carol", 70000, false}} var filtered []Employee for _, e := range emps { if e.Active && e.Salary > 50000 { filtered = append(filtered, e) } } uses 'e.Active && e.Salary > 50000' which matches criteria. emps := []Employee{{"Alice", 60000, true}, {"Bob", 45000, true}, {"Carol", 70000, false}} filtered := []Employee{} for i := range emps { if emps[i].Salary > 50000 { filtered = append(filtered, emps[i]) } } misses active check. emps := []Employee{{"Alice", 60000, true}, {"Bob", 45000, true}, {"Carol", 70000, false}} filtered := []Employee{} for _, e := range emps { if e.Active || e.Salary > 50000 { filtered = append(filtered, e) } } uses '||' which includes inactive employees. emps := []Employee{{"Alice", 60000, true}, {"Bob", 45000, true}, {"Carol", 70000, false}} filtered := []Employee{} for _, e := range emps { if e.Active && e.Salary < 50000 { filtered = append(filtered, e) } } uses salary < 50000 which is wrong.
  3. Final Answer:

    The code that filters using e.Active && e.Salary > 50000 -> Option A
  4. Quick Check:

    Filter with '&&' for both conditions [OK]
Hint: Use && to combine multiple conditions correctly [OK]
Common Mistakes:
  • Using || instead of && for filtering
  • Ignoring the Active field in filter
  • Using wrong salary comparison operator
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard