Sample questions
Question 1Easy
What does p := new(int) give you?
- An int with value 0
- A nil *int
- An int slice of length 0
- A *int pointing to a new zero valued int
Answer: D. A *int pointing to a new zero valued int
Explanation
new(T) allocates zeroed storage for T and returns a pointer to it. *p is 0 at first.
Step-by-step solution
new(int) allocates an int set to 0.- It returns its address.
*p reads 0.
Why the other options are wrong
- A. The result is a pointer, not the int.
- B. The pointer is not nil.
- C. new(int) has nothing to do with slices.
Question 2Easy
What is printed?
func inc(n int) { n++ }
func main() {
x := 5
inc(x)
fmt.Println(x)
}
- 6
- 0
- 5
- Compile error
Answer: C. 5
Explanation
Go passes arguments by value, so inc changes only its own copy. Pass *int to modify the caller.
Step-by-step solution
inc receives a copy of x.n++ changes the copy.x remains 5.
Why the other options are wrong
- A. Only the copy was incremented.
- B. x is not reset to zero.
- D. The code compiles.
Question 3Easy
When should a method usually have a pointer receiver?
- Never; Go has no pointers
- Only when the struct is empty
- When it modifies the receiver or the struct is large
- Always, for every method, because value receivers cannot read the struct's fields
Answer: C. When it modifies the receiver or the struct is large
Explanation
Pointer receivers avoid copies and allow mutation; keep receivers consistent across a type's methods.
Step-by-step solution
- Pointer receiver lets method change struct data directly.
- Pointer receiver avoids copying large structs repeatedly.
- Value receiver can't modify struct or handle big data efficiently.
- Use pointer receiver when method needs to alter struct state.
- Use pointer receiver for performance with large structs.
- Correct answer: When it modifies the receiver or the struct is large.
Why the other options are wrong
- A. Go does have pointers, and they are used in receivers.
- B. Empty structs are not the rule; pointer receivers are chosen for mutation or size.
- D. Value receivers can read fields fine, so that alone is no reason for a pointer.
Tip: Use pointer receivers for methods that change data or work with big structs.
Question 4Easy
How do you make a struct field accessible from other packages?
- Start its name with an uppercase letter
- Add an export tag
- Mark it with the public keyword and add it to the package's export list
- Put it first in the struct
Answer: A. Start its name with an uppercase letter
Explanation
Go uses capitalisation for visibility: Name is exported, name is not.
Step-by-step solution
- Start its name with an uppercase letter.
- This makes the field accessible from other packages.
- Go uses this convention for exporting identifiers.
- No tags or keywords are needed for visibility.
- Final answer: Start its name with an uppercase letter.
Why the other options are wrong
- B. Tags do not control export; struct tags are only read by reflection at runtime.
- C. Go has no public keyword; export depends on the first letter of the name.
- D. Position in the struct does not control export; the name's first letter alone decides it.
Tip: Use uppercase for struct fields to make them accessible from other packages.
Question 5Medium
What does a struct tag like json:"name" do?
- Gives metadata read via reflection, e.g. the JSON key
- Sets a default value
- Makes the field private
- Validates the field's value at compile time, rejecting structs whose data does not match
Answer: A. Gives metadata read via reflection, e.g. the JSON key
Explanation
encoding/json reads tags to choose key names and options like omitempty.
Step-by-step solution
- The
json:"name" tag tells the JSON encoder which field to use as the key. - It provides metadata for reflection, used by packages like encoding/json.
- This metadata helps in mapping struct fields to JSON keys correctly.
- Tags are read at runtime, not compile time, so validation isn't done here.
- The correct answer is A (Gives metadata read via reflection, e.g. the JSON key).
Why the other options are wrong
- B. Tags do not set default values; defaults are done in code, not in a struct tag.
- C. Tags do not affect visibility; the name's first letter decides.
- D. Tags are not validated at compile time; the encoder just reads them at runtime.
Tip: JSON tags give metadata for encoding/decoding, not for validation or privacy.
Question 6Medium
What is printed?
type P struct{ X, Y int }
fmt.Println(P{1, 2} == P{1, 2})
- false
- true
- Compile error
- 1
Answer: B. true
Explanation
Structs whose fields are all comparable can be compared with ==, field by field.
Step-by-step solution
- Both values have X=1 and Y=2.
- All fields are ints, so comparable.
- Equal gives true.
Why the other options are wrong
- A. The field values match.
- C. Comparison is allowed here.
- D. == gives a bool, not 1.
Question 7Medium
What is the output?
package main
import "fmt"
type Animal struct{ Name string }
func (a Animal) Hello() string { return "I am " + a.Name }
type Dog struct {
Animal
Breed string
}
func main() {
d := Dog{Animal{"Rex"}, "Lab"}
fmt.Println(d.Hello(), d.Name)
}
- I am Rex
- I am Lab Lab
- Compile error
- I am Rex Rex
Answer: D. I am Rex Rex
Explanation
Embedding promotes Animal's fields and methods to Dog, so d.Hello() and d.Name work.
Step-by-step solution
- d.Hello() calls the Animal's Hello method, returning 'I am Rex'.
- d.Name accesses the embedded Animal's Name field, which is 'Rex'.
- fmt.Println prints both values as 'I am Rex Rex'.
- The code compiles because Dog embeds Animal and provides all necessary fields.
- Final output is 'I am Rex Rex'.
Why the other options are wrong
- A. Hello builds 'I am ' plus the name, which has one space, not two.
- B. Lab is the Breed field, not Name, so d.Name cannot print Lab.
- C. The embedded Animal's Name and Hello are promoted, so the code compiles without error.
Tip: Embedded types in Go inherit methods and fields, making them accessible as if they were part of the outer type.
Question 8Medium
What is the output?
package main
import "fmt"
type P struct{ X, Y int }
func main() {
a := P{1, 2}
b := P{1, 2}
fmt.Println(a == b)
}
- false
- true
- Compile error
- Runtime panic
Answer: B. true
Explanation
Structs whose fields are all comparable can be compared with ==, field by field.
Step-by-step solution
- A is a struct with X=1 and Y=2.
- B is a struct with X=1 and Y=2.
- Structs with int fields are comparable.
- Comparing a == b checks each field.
- All fields match, so output is true.
Why the other options are wrong
- A. Structs with the same field values are equal, so a == b is true here, not false.
- C. Structs whose fields are all comparable can be compared with ==, so it compiles.
- D. Comparing two structs with == does not panic; it compares each field and returns a bool.
Tip: Remember: structs with all comparable fields can be compared with ==.
Question 9Hard
What is the output?
package main
import "fmt"
type P struct{ X int }
func main() {
p := &P{1}
q := p
q.X = 9
fmt.Println(p.X)
}
- 0
- 1
- 9
- Compile error
Answer: C. 9
Explanation
p and q point to the same struct; q.X is shorthand for (*q).X.
Step-by-step solution
- P is a pointer to a P struct with X = 1.
- Q is assigned p, so q points to the same struct as p.
- q.X = 9 changes the X field of the struct to 9.
- fmt.Println(p.X) prints the updated value of X from the shared struct.
- p.X is 9 because both p and q refer to the same struct.
Why the other options are wrong
- A. Pointers share one struct, so q.X = 9 also changes p.X; it does not stay at 0.
- B. The original 1 is overwritten through the shared pointer, so p.X shows 9, not 1.
- D. The code compiles; p and q are both pointers, and setting a field through a pointer is allowed.
Tip: When two pointers point to the same struct, changes to one are visible through the other.
Question 10Hard
What is printed?
type Counter struct{ n int }
func (c Counter) Inc() { c.n++ }
func main() {
c := Counter{}
c.Inc()
c.Inc()
fmt.Println(c.n)
}
- 0
- 2
- 1
- Compile error
Answer: A. 0
Explanation
A value receiver gets a copy of the struct, so c.n++ changes the copy only. Use func (c *Counter) Inc() to update the original.
Step-by-step solution
Inc has a value receiver.- Each call works on a copy.
c.n stays 0.
Why the other options are wrong
- B. The increments are lost.
- C. Even the first call has no effect on c.
- D. Value receiver methods compile and run.
Tip: Use pointer receivers for methods that modify state.
Practise all 14 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →