0
0
Goprogramming~20 mins

Interface satisfaction in Go - Practice Problems & Coding Challenges

Choose your learning style9 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
What is the output of this Go program using interface satisfaction?
Consider the following Go code. What will it print when run?
Go
package main

import "fmt"

type Speaker interface {
	Speak() string
}

type Dog struct {}

func (d Dog) Speak() string {
	return "Woof!"
}

type Cat struct {}

func (c Cat) Speak() string {
	return "Meow!"
}

func saySomething(s Speaker) {
	fmt.Println(s.Speak())
}

func main() {
	d := Dog{}
	c := Cat{}

	saySomething(d)
	saySomething(c)
}
AWoof!\nWoof!
BWoof!\nMeow!
CMeow!\nWoof!
DCompilation error: Dog does not implement Speaker
Attempts:
2 left
💡 Hint
Check which types implement the Speak() method of the Speaker interface.
Predict Output
intermediate
2:00remaining
What error does this Go code produce regarding interface satisfaction?
Look at this Go code snippet. What error will it produce when compiled?
Go
package main

import "fmt"

type Reader interface {
	Read(p []byte) (n int, err error)
}

type MyReader struct {}

func (r *MyReader) Read(p []byte) (int, error) {
	return 0, nil
}

func main() {
	var r Reader = MyReader{}
	fmt.Println(r)
}
Acannot use MyReader{} (type MyReader) as type Reader in assignment: MyReader does not implement Reader (Read method has pointer receiver)
Bnil
CCompilation error: missing Read method
DProgram runs and prints: {}
Attempts:
2 left
💡 Hint
Check if the method receiver type matches the interface implementation.
🔧 Debug
advanced
2:00remaining
Why does this Go program panic at runtime when using interface satisfaction?
Examine the code below. It compiles but panics at runtime. Why?
Go
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
	p.Print()
}
Ap is nil interface, calling Print() causes runtime panic: nil pointer dereference
BCompilation error: Printer interface not implemented
CProgram prints empty line
DProgram prints zero value of Document
Attempts:
2 left
💡 Hint
What is the value of p before calling Print()?
📝 Syntax
advanced
2:00remaining
Which option correctly implements interface satisfaction with embedded interfaces?
Given these interfaces, which implementation satisfies the Composite interface?
Go
package main

type Reader interface {
	Read(p []byte) (n int, err error)
}

type Writer interface {
	Write(p []byte) (n int, err error)
}

type Composite interface {
	Reader
	Writer
}

// Which struct and methods satisfy Composite?
Atype RW struct{}\nfunc (rw *RW) Write(p []byte) (int, error) { return 0, nil }
Btype RW struct{}\nfunc (rw *RW) Read(p []byte) (int, error) { return 0, nil }\nfunc (rw RW) Write(p []byte) (int, error) { return 0, nil }
Ctype RW struct{}\nfunc (rw RW) Read(p []byte) (int, error) { return 0, nil }
Dtype RW struct{}\nfunc (rw RW) Read(p []byte) (int, error) { return 0, nil }\nfunc (rw RW) Write(p []byte) (int, error) { return 0, nil }
Attempts:
2 left
💡 Hint
Both Read and Write methods must be implemented with matching receiver types.
🚀 Application
expert
2:00remaining
How many items are in the slice after interface satisfaction and type assertion?
What is the length of the slice 'speakers' after the code runs?
Go
package main

import "fmt"

type Speaker interface {
	Speak() string
}

type Human struct {
	name string
}

func (h Human) Speak() string {
	return "Hello, " + h.name
}

func main() {
	speakers := []Speaker{}

	h1 := Human{"Alice"}
	h2 := Human{"Bob"}

	speakers = append(speakers, h1)

	var s Speaker = h2
	if h, ok := s.(Human); ok {
		speakers = append(speakers, h)
	}

	fmt.Println(len(speakers))
}
A0
B1
C2
DCompilation error
Attempts:
2 left
💡 Hint
Check how many times append is called and what is appended.