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
How do you define a struct in Go? a) type Person struct {} b) struct Person {} c) struct { Person } d) type struct Person {}
In Go, what is the default value of an uninitialized struct field? a) null b) 0 c) "" d) Depends on the field’s type
How do you initialize a struct in Go? a) var p = Person{} b) p := Person() c) new(Person) d) struct = Person{}
How do you access a field of a struct in Go? a) . (dot) operator b) -> operator c) [] brackets d) :: operator
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")
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
Which keyword is used to define a named struct in Go? a) type b) struct c) define d) object
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
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 = {}
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
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()
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
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
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
How do you create a pointer to a struct in Go? a) p := &Person{} b) p := *Person{} c) p := Person{} d) p := new(Person)
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
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
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
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
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
Go is considered a minimalistic OOP language. Which feature is NOT part of its OOP system? a) Inheritance b) Polymorphism c) Interfaces d) Methods
What is the primary way Go achieves polymorphism? a) Through inheritance b) By implementing interfaces c) Using method overloading d) Using generic types
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
What concept does Go’s interface primarily support in its OOP model? a) Encapsulation b) Inheritance c) Polymorphism d) Abstraction
How does Go implement abstraction? a) Using structs and methods b) Through inheritance c) Using interfaces d) Go does not support abstraction
What feature of Go allows a type to implement multiple behaviors (polymorphism)? a) Interface implementation b) Method overloading c) Inheritance d) Function pointers
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
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
How does Go handle object composition? a) Through struct embedding b) By using inheritance c) By using interfaces d) Through function pointers
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
Qno
Answer
1
a) type Person struct {}
2
d) Depends on the field’s type
3
a) var p = Person{}
4
a) . (dot) operator
5
a) p.Name = "John"
6
a) Yes, you can define empty structs
7
a) type
8
c) Yes, it can have methods for its type
9
a) var x = struct{}
10
a) Yes, Go supports struct embedding
11
a) func (p Person) methodName()
12
a) Value receiver creates a copy, pointer receiver modifies the original object
13
a) Use a pointer receiver
14
d) Both a and c
15
a) p := &Person{}
16
a) Dereferences the struct pointer
17
a) The original struct is modified
18
a) return &p
19
a) No, methods are only allowed on structs
20
a) Pointer receiver, because it avoids copying the entire struct
21
a) Inheritance
22
b) By implementing interfaces
23
a) No, Go uses composition and interfaces instead
24
c) Polymorphism
25
c) Using interfaces
26
a) Interface implementation
27
a) Yes, Go structs automatically implement interfaces when methods match