The address operator (&) gives the memory address of a variable. It lets you find where the variable is stored in memory.
Click to reveal answer
beginner
What does the dereference operator (*) do in Go?
The dereference operator (*) lets you access or change the value stored at a memory address (pointer). It "follows" the pointer to the actual data.
Click to reveal answer
beginner
Given: var x int = 10; p := &x; What is *p?
*p is 10, the value stored at the memory address p points to (which is x's value).
Click to reveal answer
intermediate
How do you change the value of a variable using a pointer in Go?
Use the dereference operator (*) on the pointer to assign a new value. For example, *p = 20 changes the value at the address p points to.
Click to reveal answer
intermediate
Why are address and dereference operators useful in Go?
They let you work directly with memory addresses, which helps with efficient data handling, sharing data without copying, and building complex data structures like linked lists.
Click to reveal answer
What does the & operator do in Go?
AReturns the memory address of a variable
BAccesses the value at a pointer
CDeclares a new variable
DPerforms addition
✗ Incorrect
The & operator returns the memory address of a variable.
What does *p mean if p is a pointer in Go?
AThe value stored at the address p points to
BCreates a new pointer
CDeclares a pointer variable
DThe memory address stored in p
✗ Incorrect
*p accesses the value stored at the memory address that p points to.
How do you get the address of variable x in Go?
Aaddress(x)
B*x
Cx&
D&x
✗ Incorrect
Use &x to get the memory address of variable x.
If p := &x, what does *p = 5 do?
AChanges p to 5
BChanges x to 5
CCreates a new variable 5
DCauses an error
✗ Incorrect
*p = 5 changes the value of x to 5 because p points to x.
Why use pointers in Go?
ATo declare variables
BTo copy data faster
CTo access memory addresses directly
DTo avoid using variables
✗ Incorrect
Pointers let you access and manipulate memory addresses directly, which can improve efficiency.
Explain how the address (&) and dereference (*) operators work together in Go.
Think about how you find a house address and then enter the house to see what's inside.
You got /3 concepts.
Describe a simple example in Go where you use a pointer to change a variable's value.
Start with a number, get its address, then change the number through the pointer.
You got /5 concepts.
Practice
(1/5)
1. What does the & operator do in Go?
easy
A. It declares a new variable.
B. It returns the memory address of a variable.
C. It multiplies two numbers.
D. It dereferences a pointer to get the value.
Solution
Step 1: Understand the & operator
The & operator is used to get the address of a variable in memory.
Step 2: Differentiate from * operator
The * operator is used to access the value at an address, not to get the address itself.
Final Answer:
It returns the memory address of a variable. -> Option B
Quick Check:
& operator = address retrieval [OK]
Hint: Remember & gets address, * gets value at address [OK]
Common Mistakes:
Confusing & with * operator
Thinking & multiplies values
Assuming & declares variables
2. Which of the following is the correct way to declare a pointer to an int variable num in Go?
easy
A. var p &int = num
B. var p int = *num
C. var p *int = &num
D. var p *num = &int
Solution
Step 1: Understand pointer declaration syntax
In Go, a pointer to an int is declared as *int. To assign it, use the address operator & on the variable.
Step 2: Check each option
var p *int = &num correctly declares p as a pointer to int and assigns the address of num. Others misuse types or operators.
Final Answer:
var p *int = &num -> Option C
Quick Check:
Pointer declaration = var p *int = &num [OK]
Hint: Pointer syntax: var p *Type = &variable [OK]
Common Mistakes:
Using * on variable instead of type
Using & with type instead of variable
Mixing variable and type names
3. What will be the output of this Go code?
package main
import "fmt"
func main() {
x := 10
p := &x
*p = 20
fmt.Println(x)
}
medium
A. 10
B. Compilation error
C. Memory address of x
D. 20
Solution
Step 1: Understand pointer assignment
Variable p stores the address of x. Using *p = 20 changes the value at that address.
Step 2: Check final value of x
Since *p points to x, changing *p to 20 updates x to 20.
Final Answer:
20 -> Option D
Quick Check:
Dereferencing pointer changes original variable [OK]
Hint: Changing *p changes x because p points to x [OK]
Common Mistakes:
Thinking *p = 20 creates new variable
Expecting print to show address
Confusing & and * operators
4. Identify the error in this Go code snippet:
func main() {
var a int = 5
var p *int
*p = a
fmt.Println(*p)
}
medium
A. Pointer p is not initialized before dereferencing.
B. Variable a should be a pointer.
C. Cannot assign int to *int directly.
D. fmt.Println cannot print pointer values.
Solution
Step 1: Check pointer initialization
Pointer p is declared but not assigned an address before dereferencing with *p = a.
Step 2: Understand consequences
Dereferencing an uninitialized pointer causes a runtime panic (nil pointer dereference).
Final Answer:
Pointer p is not initialized before dereferencing. -> Option A
Quick Check:
Dereference only initialized pointers [OK]
Hint: Always assign pointer before dereferencing it [OK]