Interfaces in Go are a powerful feature that allows you to define behaviors and structures abstractly. This chapter will cover defining, implementing interfaces, interface composition, and empty interfaces with type assertions.
Chapter 2: Interfaces in Go – MCQs
1. Defining and Implementing Interfaces
How do you define an interface in Go?
a) type InterfaceName struct {}
b) type InterfaceName interface {}
c) interface InterfaceName {}
d) struct InterfaceName {}
Which of the following is required to implement an interface in Go?
a) Explicitly declare implementation in the struct
b) Define methods with matching signatures
c) Use the interface keyword in the struct
d) There is no need for any explicit declaration
How do you check if a type implements an interface in Go?
a) By using the implements keyword
b) By declaring the type explicitly in the interface
c) If the methods match the interface’s signature
d) You cannot check for implementation in Go
Can a struct implement multiple interfaces in Go?
a) Yes, a struct can implement any number of interfaces
b) No, a struct can implement only one interface
c) Yes, but only if they are part of the same package
d) No, Go does not support multiple interfaces
Which of the following is the correct way to declare a method in a Go interface?
a) methodName() string
b) func methodName() string
c) methodName() (string)
d) methodName() -> string
What happens if a struct does not implement all methods defined in an interface?
a) The program will throw a compilation error
b) The struct will automatically inherit default methods
c) The struct is considered to implement the interface
d) The struct cannot be used where the interface is expected
In Go, is it necessary for a struct to declare that it implements an interface?
a) Yes, the struct must explicitly declare implementation
b) No, Go uses implicit implementation based on method signatures
c) No, as Go does not have interfaces
d) Yes, Go requires an implements keyword
What is the main advantage of interfaces in Go?
a) Enables struct inheritance
b) Provides dynamic polymorphism
c) Improves code readability
d) Allows direct function overloading
Can you create an instance of an interface in Go?
a) Yes, you can directly instantiate an interface
b) No, interfaces cannot be instantiated
c) Yes, but only if the interface has methods
d) No, you can only declare an interface type
How do you implement the String method for a struct in Go?
a) func (s struct) String() string {}
b) func String(s struct) string {}
c) method String() string {}
d) func String() (string) {}
2. Interface Composition
What is interface composition in Go?
a) Creating new interfaces by embedding existing ones
b) Overloading functions in interfaces
c) Implementing multiple methods within a single interface
d) Using interfaces to compose complex structures
How do you embed an interface inside another interface in Go?
a) By using the embed keyword
b) By declaring an interface inside another interface
c) By listing the inner interface as a field
d) By including the inner interface as part of the outer one
What does interface composition allow in Go?
a) Multiple inheritance of structs
b) Reusing methods across different interfaces
c) Creating circular dependencies
d) Declaring default implementations for methods
Can an interface in Go embed another interface?
a) Yes, an interface can embed another interface
b) No, interfaces cannot embed other interfaces
c) Yes, but only within the same package
d) No, Go interfaces are not composable
How would you call the method GetName() from an embedded interface Person inside Employee?
a) Employee.GetName()
b) Person.GetName()
c) Employee.Person.GetName()
d) GetName()
What is the main purpose of interface composition in Go?
a) To create multi-method interfaces
b) To simplify code by reusing existing interfaces
c) To enable inheritance of methods
d) To generate default implementations of methods
Can an embedded interface in Go have its own methods?
a) Yes, an embedded interface can have its own methods
b) No, embedded interfaces only inherit methods from other interfaces
c) Yes, but only if the parent interface does not have any methods
d) No, they must inherit methods without defining new ones
In Go, what is the outcome of embedding an interface in another interface?
a) The new interface must implement all methods of the embedded interface
b) The new interface automatically implements all methods of the embedded interface
c) The embedded interface is ignored
d) The new interface loses the ability to implement methods
How do you declare a composite interface in Go?
a) type Composite interface { Person, Employee }
b) type Composite interface { GetName(), GetSalary() }
c) type Composite interface { Person, Employee }
d) type Composite interface {}
How does Go handle method resolution when multiple interfaces are composed together?
a) It throws an error if multiple methods conflict
b) It prefers the most recently declared method
c) It uses the method from the last interface declared
d) It resolves methods based on method signature matches
3. Empty Interfaces and Type Assertions
What is an empty interface in Go?
a) An interface that holds no methods
b) An interface that has one method
c) An interface with default method implementations
d) An interface used to store any type of data
Which type can be assigned to an empty interface in Go?
a) Any type
b) Only structs
c) Only integer types
d) Only boolean types
How do you perform a type assertion in Go?
a) value.(Type)
b) Type(value)
c) assertType(value)
d) value.Type()
What happens if a type assertion fails in Go?
a) It causes a panic
b) It returns an error
c) It returns nil
d) It returns false
Which of the following is the correct way to assert a value from an empty interface to its original type in Go?
a) value := i.(Type)
b) i.Type(value)
c) Type(i).value
d) value := Type(i)
How do you check if a type assertion was successful in Go?
a) By using a try block
b) By checking the boolean value returned with the type assertion
c) By using the assert keyword
d) By inspecting the result of panic
What does the following Go code do?goCopy codevar x interface{} = 10 value, ok := x.(int)
a) Assigns the value of x to value and checks if it is an int
b) Raises an error
c) Converts x to int and assigns it to value
d) Checks if x is a string
What is the default value of an empty interface in Go?
a) nil
b) 0
c) ""
d) false
How can you extract a value from an empty interface in Go?
a) By performing a type assertion
b) By using the extract function
c) By calling empty.Value()
d) By using interface keyword
What does the following code do in Go?goCopy codevar data interface{} = "Hello" result := data.(string)
a) Converts data to a string
b) Returns the length of data
c) Throws an error because data is an empty interface
d) Prints Hello
Answers
QNo
Answer
1
b) type InterfaceName interface {}
2
b) Define methods with matching signatures
3
c) If the methods match the interface’s signature
4
a) Yes, a struct can implement any number of interfaces
5
a) methodName() string
6
a) The program will throw a compilation error
7
b) No, Go uses implicit implementation based on method signatures
8
b) Provides dynamic polymorphism
9
b) No, interfaces cannot be instantiated
10
a) func (s struct) String() string {}
11
a) Creating new interfaces by embedding existing ones
12
d) By including the inner interface as part of the outer one
13
b) Reusing methods across different interfaces
14
a) Yes, an interface can embed another interface
15
c) Employee.Person.GetName()
16
b) To simplify code by reusing existing interfaces
17
a) Yes, an embedded interface can have its own methods
18
b) The new interface automatically implements all methods of the embedded interface
19
a) type Composite interface { Person, Employee }
20
a) It throws an error if multiple methods conflict
21
a) An interface that holds no methods
22
a) Any type
23
a) value.(Type)
24
a) It causes a panic
25
a) value := i.(Type)
26
b) By checking the boolean value returned with the type assertion
27
a) Assigns the value of x to value and checks if it is an int