Golang Interfaces Quiz

Golang
0 Passed
0% acceptance

35 comprehensive questions on Golang interfaces, covering interface basics, implicit implementation, empty interface, type assertions, and type switches — with 18 code examples demonstrating interface usage and polymorphism.

35 Questions
~70 minutes
1

Question 1

What is an interface in Golang?

A
A type that specifies a set of method signatures that a concrete type must implement
B
A concrete data type like struct
C
A function that returns multiple values
D
A built-in data type
2

Question 2

How do you declare an interface in Golang?

A
type InterfaceName interface { method signatures }
B
interface InterfaceName { method signatures }
C
class InterfaceName { method signatures }
D
var InterfaceName interface { method signatures }
3

Question 3

What will this basic interface example print?

go
package main

import "fmt"

type Speaker interface {
    speak()
}

type Dog struct{}

func (d Dog) speak() {
    fmt.Println("Woof!")
}

func main() {
    var s Speaker = Dog{}
    s.speak()
}
A
Woof!
B
Compilation error
C
No output
D
Runtime panic
4

Question 4

What is implicit interface implementation in Golang?

A
Types automatically satisfy interfaces by implementing their methods, no explicit declaration needed
B
Interfaces must be explicitly implemented with implements keyword
C
All types implement all interfaces
D
Interfaces are inherited like in other languages
5

Question 5

What is the empty interface interface{}?

A
An interface with no methods that can hold any type
B
An interface that cannot be implemented
C
A synonym for void
D
An interface for nil values
6

Question 6

What will this empty interface example print?

go
package main

import "fmt"

func printValue(v interface{}) {
    fmt.Printf("Type: %T, Value: %v\n", v, v)
}

func main() {
    printValue(42)
    printValue("hello")
    printValue([]int{1, 2, 3})
}
A
Type: int, Value: 42\nType: string, Value: hello\nType: []int, Value: [1 2 3]
B
Compilation error
C
Type: interface{}, Value: 42
D
No output
7

Question 7

What is a type assertion in Golang?

A
A way to extract the concrete value from an interface variable at runtime
B
A compile-time type check
C
A way to convert between types
D
A method call on interfaces
8

Question 8

What will this type assertion example print?

go
package main

import "fmt"

func main() {
    var i interface{} = "hello world"
    s := i.(string)
    fmt.Println(s)
}
A
hello world
B
Compilation error
C
Runtime panic
D
No output
9

Question 9

What happens if a type assertion fails?

A
Runtime panic occurs
B
Returns zero value
C
Compilation error
D
Ignores the assertion
10

Question 10

What is the comma ok idiom for type assertions?

A
value, ok := x.(T) - checks if assertion succeeds without panicking
B
A way to ignore errors
C
A loop construct
D
A function call
11

Question 11

What will this safe type assertion example print?

go
package main

import "fmt"

func main() {
    var i interface{} = 42
    if s, ok := i.(string); ok {
        fmt.Println("String:", s)
    } else {
        fmt.Println("Not a string")
    }
}
A
Not a string
B
String: 42
C
Compilation error
D
Runtime panic
12

Question 12

What is a type switch in Golang?

A
A switch statement that compares types instead of values
B
A switch for primitive types only
C
A way to change variable types
D
A switch that uses interfaces
13

Question 13

What will this type switch example print?

go
package main

import "fmt"

func printType(i interface{}) {
    switch v := i.(type) {
    case int:
        fmt.Printf("Integer: %d\n", v)
    case string:
        fmt.Printf("String: %s\n", v)
    default:
        fmt.Printf("Other: %T\n", v)
    }
}

func main() {
    printType(42)
    printType("hello")
    printType(3.14)
}
A
Integer: 42\nString: hello\nOther: float64
B
Compilation error
C
Integer: 42\nString: hello\nOther: interface{}
D
No output
14

Question 14

Can interfaces be embedded in structs?

A
Yes, embedding interfaces promotes their methods to the struct
B
No, only structs can be embedded
C
Only in certain cases
D
Compilation error
15

Question 15

What will this embedded interface example do?

go
package main

import "fmt"

type Reader interface {
    read()
}

type Writer interface {
    write()
}

type ReadWriter interface {
    Reader
    Writer
}

type File struct{}

func (f File) read()  { fmt.Println("reading") }
func (f File) write() { fmt.Println("writing") }

func main() {
    var rw ReadWriter = File{}
    rw.read()
    rw.write()
}
A
reading\nwriting
B
Compilation error
C
No output
D
Runtime panic
16

Question 16

What is the zero value of an interface variable?

A
nil
B
Zero value of the concrete type
C
Empty interface{}
D
Compilation error
17

Question 17

What happens when you call a method on a nil interface?

A
Runtime panic occurs
B
Method call is ignored
C
Returns zero value
D
Compilation error
18

Question 18

What will this nil interface example do?

go
package main

type Speaker interface {
    speak()
}

func main() {
    var s Speaker
    s.speak()  // This will panic
}
A
Runtime panic
B
No output
C
Compilation error
D
Prints something
19

Question 19

What is interface satisfaction?

A
When a concrete type implements all methods required by an interface
B
When an interface implements a concrete type
C
When two interfaces are compatible
D
A compile-time check
20

Question 20

Do pointer and value receivers satisfy different interfaces?

A
Yes, T and *T have different method sets, so they satisfy different interfaces
B
No, they satisfy the same interfaces
C
Only value receivers matter
D
Only pointer receivers matter
21

Question 21

What will this method set example show?

go
package main

import "fmt"

type Mover interface {
    move()
}

type Car struct{}

func (c Car) move() {
    fmt.Println("Car moving")
}

func main() {
    var m Mover = Car{}
    m.move()
    
    var m2 Mover = &Car{}
    m2.move()
}
A
Car moving\nCar moving
B
Compilation error
C
Car moving
D
Runtime panic
22

Question 22

What is the difference between interface{} and any?

A
They are identical - any is a type alias for interface{} introduced in Go 1.18
B
interface{} is for empty interfaces, any is for non-empty
C
any is deprecated
D
They are completely different
23

Question 23

What is interface composition?

A
Embedding one interface into another to combine their method requirements
B
Implementing multiple interfaces
C
Creating interface hierarchies
D
Merging interface methods
24

Question 24

What will this interface composition example do?

go
package main

import "fmt"

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

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

type ReadWriter interface {
    Reader
    Writer
}

func main() {
    fmt.Println("ReadWriter requires both Read and Write methods")
}
A
ReadWriter requires both Read and Write methods
B
Compilation error
C
No output
D
Runtime panic
25

Question 25

Can you type assert to an interface?

A
Yes, you can assert that an interface value implements another interface
B
No, type assertions only work for concrete types
C
Only for empty interfaces
D
Compilation error
26

Question 26

What will this interface assertion example do?

go
package main

import "fmt"

type Stringer interface {
    String() string
}

type Printer interface {
    Print()
}

type Document struct{}

func (d Document) String() string { return "document" }
func (d Document) Print()       { fmt.Println("printing") }

func main() {
    var i interface{} = Document{}
    if s, ok := i.(Stringer); ok {
        fmt.Println(s.String())
    }
    if p, ok := i.(Printer); ok {
        p.Print()
    }
}
A
document\nprintting
B
Compilation error
C
document
D
Runtime panic
27

Question 27

What is the reflect package's relationship to interfaces?

A
reflect uses interface{} to inspect types and values at runtime
B
reflect replaces interfaces
C
reflect is unrelated to interfaces
D
reflect implements all interfaces
28

Question 28

What is a common pitfall with interface{}?

A
Losing type safety and having to use type assertions everywhere
B
Interfaces are too restrictive
C
Cannot store any values
D
Performance issues
29

Question 29

What is the main benefit of interfaces in Golang?

A
Enabling polymorphism and decoupling code from concrete implementations
B
Faster execution
C
Smaller binaries
D
Easier syntax
30

Question 30

What is the difference between error interface and other interfaces?

A
error is a built-in interface, but works the same as user-defined interfaces
B
error cannot be implemented by user types
C
error is a concrete type
D
error has special syntax
31

Question 31

What will this error interface example do?

go
package main

import "fmt"

type MyError struct {
    msg string
}

func (e MyError) Error() string {
    return e.msg
}

func doSomething() error {
    return MyError{"something went wrong"}
}

func main() {
    if err := doSomething(); err != nil {
        fmt.Println(err)
    }
}
A
something went wrong
B
Compilation error
C
No output
D
Runtime panic
32

Question 32

Can interfaces have methods with the same name?

A
No, interface methods must have unique names within the interface
B
Yes, but only if they have different signatures
C
Yes, always
D
Only in embedded interfaces
33

Question 33

What is the method set of an interface?

A
The set of methods that must be implemented by satisfying types
B
The methods available on interface variables
C
All methods in the program
D
Built-in methods
34

Question 34

What is duck typing?

A
If it looks like a duck and quacks like a duck, it's a duck - types satisfy interfaces by behavior
B
A way to define types
C
A compile-time check
D
A runtime feature
35

Question 35

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

A
Design interfaces around behavior, keep them small and focused, use interface{} sparingly
B
Make interfaces as large as possible
C
Avoid interfaces altogether
D
Use inheritance hierarchies

QUIZZES IN Golang