Bird
Raised Fist0
Goprogramming~20 mins

Interface definition in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of interface method call
What is the output of this Go program that uses an interface and a struct?
Go
package main
import "fmt"
type Speaker interface {
    Speak() string
}
type Dog struct {}
func (d Dog) Speak() string {
    return "Woof!"
}
func main() {
    var s Speaker = Dog{}
    fmt.Println(s.Speak())
}
AWoof!
BWoof
Cspeak
DCompilation error
Attempts:
2 left
💡 Hint
Check the Speak method's return value and how it's printed.
❓ Predict Output
intermediate
2:00remaining
Interface variable nil check
What will be printed by this Go program regarding interface nil checks?
Go
package main
import "fmt"
type Reader interface {
    Read() string
}
type File struct {}
func (f *File) Read() string {
    return "data"
}
func main() {
    var r Reader
    var f *File = nil
    r = f
    if r == nil {
        fmt.Println("r is nil")
    } else {
        fmt.Println("r is not nil")
    }
}
ARuntime panic
Br is nil
CCompilation error
Dr is not nil
Attempts:
2 left
💡 Hint
An interface holding a nil pointer is not itself nil.
🔧 Debug
advanced
2:00remaining
Why does this interface assignment fail?
This code fails to compile. What is the cause of the error?
Go
package main
import "fmt"
type Writer interface {
    Write(data string)
}
type Printer struct {}
func (p Printer) Print(data string) {
    fmt.Println(data)
}
func main() {
    var w Writer = Printer{}
    w.Write("Hello")
}
APrinter does not implement Writer because method has wrong parameter type
BPrinter does not implement Writer because method name is Print, not Write
CInterface Writer cannot be assigned to struct Printer
DMissing import for fmt package
Attempts:
2 left
💡 Hint
Check method names and signatures carefully.
🧠 Conceptual
advanced
2:00remaining
Interface embedding behavior
Given these interface definitions, which statement is true about interface C?
Go
type A interface {
    Foo()
}
type B interface {
    Bar()
}
type C interface {
    A
    B
}
AC requires only Foo() method to be implemented
BC requires only Bar() method to be implemented
CC requires both Foo() and Bar() methods to be implemented
DC does not require any methods to be implemented
Attempts:
2 left
💡 Hint
Interface embedding combines all methods from embedded interfaces.
❓ Predict Output
expert
2:00remaining
Interface method call with pointer vs value receiver
What is the output of this Go program?
Go
package main
import "fmt"
type Speaker interface {
    Speak() string
}
type Cat struct {}
func (c *Cat) Speak() string {
    return "Meow"
}
func main() {
    var s Speaker
    c := Cat{}
    s = c
    fmt.Println(s.Speak())
}
ACompilation error: Cat does not implement Speaker
BRuntime panic
CMeow
DEmpty string
Attempts:
2 left
💡 Hint
Check if Cat value implements Speaker when Speak has pointer receiver.

Practice

(1/5)
1. What does an interface in Go define?
easy
A. A concrete type with fields and methods
B. A variable that holds data
C. A set of method signatures without implementation
D. A function that returns multiple values

Solution

  1. Step 1: Understand what an interface is

    An interface in Go specifies method signatures but does not provide their implementation.
  2. Step 2: Differentiate from other types

    Concrete types have fields and method implementations, unlike interfaces.
  3. Final Answer:

    A set of method signatures without implementation -> Option C
  4. Quick Check:

    Interface = method signatures only [OK]
Hint: Interfaces list methods only, no code inside [OK]
Common Mistakes:
  • Thinking interfaces hold data
  • Confusing interfaces with structs
  • Assuming interfaces implement methods
2. Which of the following is the correct syntax to define an interface named Shape with a method Area() returning a float64?
easy
A. func Shape interface { Area() float64 }
B. interface Shape { func Area() float64 }
C. type Shape struct { Area() float64 }
D. type Shape interface { Area() float64 }

Solution

  1. Step 1: Recall Go interface syntax

    Interfaces are defined with the keyword type, name, then interface and method signatures inside braces.
  2. Step 2: Check each option

    type Shape interface { Area() float64 } matches correct syntax. Options B, C, and D misuse keywords or structure.
  3. Final Answer:

    type Shape interface { Area() float64 } -> Option D
  4. Quick Check:

    Correct interface syntax = type Shape interface { Area() float64 } [OK]
Hint: Use 'type Name interface { methods }' for interfaces [OK]
Common Mistakes:
  • Using 'interface' keyword alone without 'type'
  • Defining methods inside struct instead of interface
  • Placing 'func' keyword inside interface method
3. What will be the output of this Go code?
package main
import "fmt"
type Printer interface {
    Print()
}
type Document struct {
    content string
}
func (d Document) Print() {
    fmt.Println(d.content)
}
func main() {
    var p Printer
    d := Document{"Hello, Go!"}
    p = d
    p.Print()
}
medium
A. Runtime panic: nil pointer dereference
B. Hello, Go!
C. Compilation error: cannot assign Document to Printer
D. No output

Solution

  1. Step 1: Check interface implementation

    Document has a method Print() matching Printer interface, so Document implements Printer.
  2. Step 2: Assign and call method

    Variable p of type Printer is assigned d (Document), then p.Print() calls Document.Print(), printing content.
  3. Final Answer:

    Hello, Go! -> Option B
  4. Quick Check:

    Interface method calls concrete method [OK]
Hint: Assign concrete type with matching methods to interface [OK]
Common Mistakes:
  • Thinking interface needs explicit implementation declaration
  • Expecting compile error on assignment
  • Assuming interface variable is nil
4. Identify the error in this Go code snippet:
type Reader interface {
    Read(p []byte) (n int, err error)
}
type File struct {}
func (f File) Read() (int, error) {
    return 0, nil
}
medium
A. Method Read has wrong signature, missing parameter
B. File does not implement Reader because it lacks Read method
C. Interface Reader cannot have methods with parameters
D. No error, code is correct

Solution

  1. Step 1: Compare method signatures

    Reader interface expects Read with parameter []byte and two return values.
  2. Step 2: Check File's Read method

    File's Read method has no parameters, so signature does not match interface.
  3. Final Answer:

    Method Read has wrong signature, missing parameter -> Option A
  4. Quick Check:

    Method signature must match interface [OK]
Hint: Method signatures must exactly match interface methods [OK]
Common Mistakes:
  • Ignoring method parameters in implementation
  • Assuming method name alone is enough
  • Thinking interface methods can't have parameters
5. Given these interfaces and types:
type Reader interface {
    Read(p []byte) (n int, err error)
}
type Writer interface {
    Write(p []byte) (n int, err error)
}
type ReadWriter interface {
    Reader
    Writer
}
type MyFile struct {}
func (f MyFile) Read(p []byte) (int, error) {
    return len(p), nil
}
func (f MyFile) Write(p []byte) (int, error) {
    return len(p), nil
}

Which statement is true about MyFile and ReadWriter?
hard
A. MyFile implements ReadWriter because it has both Read and Write methods
B. MyFile only implements Reader, not ReadWriter
C. MyFile does not implement any interface
D. ReadWriter cannot embed other interfaces

Solution

  1. Step 1: Understand interface embedding

    ReadWriter embeds Reader and Writer, so it requires both methods.
  2. Step 2: Check MyFile methods

    MyFile has both Read and Write methods matching signatures.
  3. Step 3: Confirm implementation

    Since MyFile has all required methods, it implements ReadWriter.
  4. Final Answer:

    MyFile implements ReadWriter because it has both Read and Write methods -> Option A
  5. Quick Check:

    Embedded interfaces require all methods [OK]
Hint: Embedded interfaces combine methods; implement all to satisfy [OK]
Common Mistakes:
  • Thinking embedding interfaces is invalid
  • Assuming partial method implementation suffices
  • Confusing interface embedding with inheritance
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