Go Struct MCQ

1. How do you define a struct in Go?

a) struct MyStruct {}
b) type MyStruct struct {}
c) define MyStruct struct
d) MyStruct := struct {}

Answer:

b) type MyStruct struct {}

Explanation:

In Go, a struct is defined using the 'type' keyword followed by the struct name and the 'struct' keyword.

2. How do you instantiate a struct in Go?

a) var myStruct MyStruct
b) myStruct := new(MyStruct)
c) myStruct := MyStruct{}
d) All of the above

Answer:

d) All of the above

Explanation:

A struct in Go can be instantiated using 'var myStruct MyStruct', 'myStruct := new(MyStruct)', or 'myStruct := MyStruct{}'.

3. How do you access a field of a struct in Go?

a) myStruct[field]
b) myStruct.field
c) myStruct->field
d) field(myStruct)

Answer:

b) myStruct.field

Explanation:

In Go, a field of a struct is accessed using the dot notation, e.g., 'myStruct.field'.

4. Can you compare two structs directly in Go?

a) Yes, if they are of the same type
b) No, structs cannot be compared
c) Yes, regardless of their type
d) Only if they are pointer types

Answer:

a) Yes, if they are of the same type

Explanation:

Two structs in Go can be compared directly using the '==' operator if they are of the same type and do not contain fields that are not comparable.

5. How do you declare a struct with embedded fields in Go?

a) Using the 'embedded' keyword
b) By specifying the type only
c) By declaring inner structs
d) Using the 'inherit' keyword

Answer:

b) By specifying the type only

Explanation:

In Go, embedded fields in a struct are declared by specifying the type only, without a field name.

6. What is an anonymous struct in Go?

a) A struct with no name
b) A struct with hidden fields
c) A struct without any fields
d) A struct with an anonymous field

Answer:

a) A struct with no name

Explanation:

An anonymous struct in Go is a struct that is defined without a name, usually declared and instantiated at the same time.

7. How do you initialize a struct with named fields in Go?

a) MyStruct{field1: value1, field2: value2}
b) MyStruct(field1, field2)
c) new(MyStruct{field1, field2})
d) MyStruct{value1, value2}

Answer:

a) MyStruct{field1: value1, field2: value2}

Explanation:

In Go, a struct can be initialized with named fields using the syntax 'MyStruct{fieldName: value}'.

8. Can structs in Go have methods associated with them?

a) Yes
b) No
c) Only if they are pointer structs
d) Only if they are anonymous structs

Answer:

a) Yes

Explanation:

In Go, methods can be defined on structs. A method is a function with a special receiver argument.

9. What is the purpose of the 'tag' in a struct field in Go?

a) To rename the field
b) To provide metadata for reflection
c) To specify default values
d) To set the visibility of the field

Answer:

b) To provide metadata for reflection

Explanation:

Tags in Go struct fields provide metadata that can be accessed using reflection, often used for things like encoding/decoding to JSON, XML, etc.

10. How can you ensure a struct field is not exported outside a package in Go?

a) Using the 'private' keyword
b) By starting the field name with a lowercase letter
c) By using the '_' prefix
d) By declaring the struct as private

Answer:

b) By starting the field name with a lowercase letter

Explanation:

In Go, a field name that starts with a lowercase letter is not exported outside the package.

11. How do you embed an interface in a struct in Go?

a) By declaring the interface inside the struct
b) By using the 'implements' keyword
c) By specifying the interface type only
d) Interfaces cannot be embedded in structs

Answer:

c) By specifying the interface type only

Explanation:

In Go, you can embed an interface in a struct by specifying the interface type only, without a field name.

12. Can a struct in Go inherit fields from another struct?

a) Yes, using inheritance
b) No, Go does not support inheritance
c) Yes, using the 'extends' keyword
d) Only through embedding

Answer:

d) Only through embedding

Explanation:

Go does not have inheritance, but you can achieve a similar effect by embedding one struct type into another.

13. How do you pass a struct to a function in Go?

a) By value
b) By reference
c) Both a and b
d) Structs cannot be passed to functions

Answer:

c) Both a and b

Explanation:

In Go, structs can be passed to functions either by value or by reference (using a pointer).

14. Is it possible to have a struct with no fields in Go?

a) Yes
b) No
c) Only in special cases
d) Only as an embedded struct

Answer:

a) Yes

Explanation:

In Go, it is perfectly valid to define a struct with no fields, often used for method grouping.

15. How do you update a field of a struct passed by reference to a function in Go?

a) Using the '.' operator on the pointer
b) By dereferencing the pointer first
c) It is not possible to update
d) Using the '->' operator

Answer:

a) Using the '.' operator on the pointer

Explanation:

In Go, when you have a pointer to a struct, you can update its fields directly using the '.' operator, without needing to dereference it explicitly.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top