MCQs on Structs and Basic OOP Concepts | Go

Go, also known as Golang, employs structs and a minimalistic approach to object-oriented programming (OOP). This quiz covers key topics like defining structs, methods, and Go’s unique OOP approach.


Defining and Using Structs

  1. How do you define a struct in Go?
    a) type Person struct {}
    b) struct Person {}
    c) struct { Person }
    d) type struct Person {}
  2. In Go, what is the default value of an uninitialized struct field?
    a) null
    b) 0
    c) ""
    d) Depends on the field’s type
  3. How do you initialize a struct in Go?
    a) var p = Person{}
    b) p := Person()
    c) new(Person)
    d) struct = Person{}
  4. How do you access a field of a struct in Go?
    a) . (dot) operator
    b) -> operator
    c) [] brackets
    d) :: operator
  5. How do you initialize a struct field with a value in Go?
    a) p.Name = "John"
    b) p.set("John")
    c) Name = p("John")
    d) p("John")
  6. Can you define a struct type without initializing any fields in Go?
    a) Yes, you can define empty structs
    b) No, struct fields are required
    c) Only in interfaces
    d) Only in methods
  7. Which keyword is used to define a named struct in Go?
    a) type
    b) struct
    c) define
    d) object
  8. Can a Go struct have methods?
    a) Yes, but only for methods defined inside the struct itself
    b) No
    c) Yes, it can have methods for its type
    d) Only for pointer receivers
  9. How do you define an anonymous struct in Go?
    a) var x = struct{}
    b) var x = new struct {}
    c) x := struct{}
    d) var x struct = {}
  10. Can you embed one struct inside another in Go?
    a) Yes, Go supports struct embedding
    b) No, Go only allows struct inheritance
    c) Only if they are pointers
    d) Only for structs of the same type

Methods and Pointer Receivers

  1. How do you define a method for a struct in Go?
    a) func (p Person) methodName()
    b) func methodName(Person)
    c) method func(Person)
    d) func (p *Person) methodName()
  2. What is the difference between a value receiver and a pointer receiver in Go methods?
    a) Value receiver creates a copy, pointer receiver modifies the original object
    b) Pointer receiver creates a copy, value receiver modifies the original object
    c) No difference
    d) Value receiver works with arrays, pointer receiver works with slices
  3. In Go, how do you pass a struct by reference?
    a) Use a pointer receiver
    b) Use a value receiver
    c) Use a copy function
    d) Use an array
  4. When should you use a pointer receiver for methods in Go?
    a) When you want to modify the struct’s fields
    b) When the struct is small
    c) When you need to avoid copying the struct
    d) Both a and c
  5. How do you create a pointer to a struct in Go?
    a) p := &Person{}
    b) p := *Person{}
    c) p := Person{}
    d) p := new(Person)
  6. What does the * symbol do when used with a struct in Go?
    a) Dereferences the struct pointer
    b) Declares the struct type
    c) Makes the struct immutable
    d) Creates a new instance of a struct
  7. What happens when you modify a struct using a pointer receiver in Go?
    a) The original struct is modified
    b) A copy of the struct is modified
    c) Nothing happens
    d) It causes a runtime error
  8. How do you return a pointer to a struct from a method in Go?
    a) return &p
    b) return p
    c) return new(p)
    d) return *p
  9. Can you define methods on non-struct types in Go?
    a) No, methods are only allowed on structs
    b) Yes, you can define methods on interfaces
    c) Yes, methods can be defined for any type
    d) Yes, but only on pointer types
  10. Which is more efficient when modifying a large struct in Go, value receiver or pointer receiver?
    a) Pointer receiver, because it avoids copying the entire struct
    b) Value receiver, because it avoids direct modification
    c) Both are equally efficient
    d) It depends on the type of the struct

Introduction to Go’s Minimalistic Object-Oriented Approach

  1. Go is considered a minimalistic OOP language. Which feature is NOT part of its OOP system?
    a) Inheritance
    b) Polymorphism
    c) Interfaces
    d) Methods
  2. What is the primary way Go achieves polymorphism?
    a) Through inheritance
    b) By implementing interfaces
    c) Using method overloading
    d) Using generic types
  3. Does Go support class-based inheritance?
    a) No, Go uses composition and interfaces instead
    b) Yes, Go supports multiple class inheritance
    c) Yes, Go supports single class inheritance
    d) Yes, but only for pointer types
  4. What concept does Go’s interface primarily support in its OOP model?
    a) Encapsulation
    b) Inheritance
    c) Polymorphism
    d) Abstraction
  5. How does Go implement abstraction?
    a) Using structs and methods
    b) Through inheritance
    c) Using interfaces
    d) Go does not support abstraction
  6. What feature of Go allows a type to implement multiple behaviors (polymorphism)?
    a) Interface implementation
    b) Method overloading
    c) Inheritance
    d) Function pointers
  7. Can a Go struct implement an interface without explicitly declaring it?
    a) Yes, Go structs automatically implement interfaces when methods match
    b) No, it must be explicitly stated
    c) Yes, but only for pointer receivers
    d) No, interfaces must be implemented in a separate file
  8. Does Go support method overloading?
    a) No, Go does not support method overloading
    b) Yes, by changing method names
    c) Yes, by changing the argument types
    d) Yes, by changing the return type
  9. How does Go handle object composition?
    a) Through struct embedding
    b) By using inheritance
    c) By using interfaces
    d) Through function pointers
  10. What is the role of the interface{} type in Go?
    a) It can hold any type of value
    b) It acts as a pointer to a struct
    c) It is used for generics
    d) It is used to define method signatures

Answer Key

QnoAnswer
1a) type Person struct {}
2d) Depends on the field’s type
3a) var p = Person{}
4a) . (dot) operator
5a) p.Name = "John"
6a) Yes, you can define empty structs
7a) type
8c) Yes, it can have methods for its type
9a) var x = struct{}
10a) Yes, Go supports struct embedding
11a) func (p Person) methodName()
12a) Value receiver creates a copy, pointer receiver modifies the original object
13a) Use a pointer receiver
14d) Both a and c
15a) p := &Person{}
16a) Dereferences the struct pointer
17a) The original struct is modified
18a) return &p
19a) No, methods are only allowed on structs
20a) Pointer receiver, because it avoids copying the entire struct
21a) Inheritance
22b) By implementing interfaces
23a) No, Go uses composition and interfaces instead
24c) Polymorphism
25c) Using interfaces
26a) Interface implementation
27a) Yes, Go structs automatically implement interfaces when methods match
28a) No, Go does not support method overloading
29a) Through struct embedding
30a) It can hold any type of value

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