MCQs on Interfaces | Go

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

  1. How do you define an interface in Go?
    • a) type InterfaceName struct {}
    • b) type InterfaceName interface {}
    • c) interface InterfaceName {}
    • d) struct InterfaceName {}
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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()
  6. 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
  7. 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
  8. 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
  9. 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 {}
  10. 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

  1. 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
  2. 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
  3. How do you perform a type assertion in Go?
    • a) value.(Type)
    • b) Type(value)
    • c) assertType(value)
    • d) value.Type()
  4. 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
  5. 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)
  6. 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
  7. 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
  8. What is the default value of an empty interface in Go?
    • a) nil
    • b) 0
    • c) ""
    • d) false
  9. 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
  10. 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

QNoAnswer
1b) type InterfaceName interface {}
2b) Define methods with matching signatures
3c) If the methods match the interface’s signature
4a) Yes, a struct can implement any number of interfaces
5a) methodName() string
6a) The program will throw a compilation error
7b) No, Go uses implicit implementation based on method signatures
8b) Provides dynamic polymorphism
9b) No, interfaces cannot be instantiated
10a) func (s struct) String() string {}
11a) Creating new interfaces by embedding existing ones
12d) By including the inner interface as part of the outer one
13b) Reusing methods across different interfaces
14a) Yes, an interface can embed another interface
15c) Employee.Person.GetName()
16b) To simplify code by reusing existing interfaces
17a) Yes, an embedded interface can have its own methods
18b) The new interface automatically implements all methods of the embedded interface
19a) type Composite interface { Person, Employee }
20a) It throws an error if multiple methods conflict
21a) An interface that holds no methods
22a) Any type
23a) value.(Type)
24a) It causes a panic
25a) value := i.(Type)
26b) By checking the boolean value returned with the type assertion
27a) Assigns the value of x to value and checks if it is an int
28a) nil
29a) By performing a type assertion
30a) Converts data to a string

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top