A pointer in Go is a variable that stores the memory address of another variable. It allows you to directly access and modify the value stored at that address.
Click to reveal answer
beginner
How do you declare a pointer to an int variable in Go?
You declare a pointer to an int using the syntax: var p *int. Here, p is a pointer that can hold the address of an int variable.
Click to reveal answer
beginner
What does the * operator do when used with a pointer variable?
The * operator is used to dereference a pointer, which means accessing or modifying the value stored at the memory address the pointer holds.
Click to reveal answer
beginner
How do you get the address of a variable in Go?
You use the & operator before a variable name to get its memory address. For example, p = &x assigns the address of x to pointer p.
Click to reveal answer
intermediate
Why use pointers in Go?
Pointers let you share and modify data efficiently without copying it. They are useful for changing variables inside functions and managing memory directly.
Click to reveal answer
Which symbol is used to declare a pointer type in Go?
A&
B#
C*
D$
✗ Incorrect
The * symbol is used to declare a pointer type, for example, *int means a pointer to an int.
What does the & operator do in Go?
ADeclares a pointer
BDereferences a pointer
CAssigns a value
DGets the address of a variable
✗ Incorrect
The & operator returns the memory address of a variable.
How do you access the value stored at the address a pointer holds?
AUsing the * operator
BUsing the & operator
CUsing the -> operator
DUsing the . operator
✗ Incorrect
The * operator dereferences the pointer to access the value at its address.
What is the type of variable p after this declaration: var p *int?
AInteger
BPointer to int
CPointer to string
DString
✗ Incorrect
p is declared as a pointer to an int type.
Why might you use pointers in Go?
ATo modify variables inside functions without copying
BTo create new variables
CTo convert types
DTo print values
✗ Incorrect
Pointers allow functions to modify variables directly by passing their addresses.
Explain how to declare a pointer to an int and assign it the address of an int variable in Go.
Think about how you declare a pointer and how you get the address of a variable.
You got /4 concepts.
Describe what happens when you dereference a pointer in Go and why it is useful.
Consider what * does when used with a pointer variable.
You got /4 concepts.
Practice
(1/5)
1. What does a pointer in Go store?
easy
A. The actual value of a variable
B. The memory address of a variable
C. The type of a variable
D. The size of a variable
Solution
Step 1: Understand what a pointer represents
A pointer does not hold the value itself but the location where the value is stored in memory.
Step 2: Recall pointer definition in Go
In Go, a pointer stores the address of a variable, not the variable's value directly.
Final Answer:
The memory address of a variable -> Option B
Quick Check:
Pointer = memory address [OK]
Hint: Pointers always hold addresses, not values [OK]
Common Mistakes:
Thinking pointers store the actual value
Confusing pointer with variable type
Assuming pointer stores variable size
2. Which of the following is the correct way to declare a pointer to an int variable named num in Go?
easy
A. var p int = &num
B. var p *int = num
C. var p *int = &num
D. var p int = *num
Solution
Step 1: Understand pointer declaration syntax
In Go, to declare a pointer to an int, use *int as the type.
Step 2: Assign the address of num to the pointer
The address operator & gets the memory address of num, which can be assigned to the pointer.
Final Answer:
var p *int = &num -> Option C
Quick Check:
Pointer declaration uses *type and &variable [OK]
Hint: Pointer type uses * before type, assign with &variable [OK]
Common Mistakes:
Forgetting the * in pointer type
Assigning variable value instead of address
Using *num instead of &num for assignment
3. What will be the output of this Go program?
package main
import "fmt"
func main() {
x := 10
p := &x
*p = 20
fmt.Println(x)
}
medium
A. 20
B. 10
C. Memory address of x
D. Compilation error
Solution
Step 1: Understand pointer assignment and dereferencing
Variable p stores the address of x. Using *p = 20 changes the value at that address.
Step 2: Check the effect on x
Since p points to x, changing *p changes x to 20.
Final Answer:
20 -> Option A
Quick Check:
Dereferencing pointer changes original variable [OK]
Hint: Changing *p changes the original variable [OK]
Common Mistakes:
Thinking *p creates a new variable
Expecting output to be address instead of value
Confusing pointer declaration with dereferencing
4. Identify the error in this Go code snippet:
var a int = 5
var p *int = a
fmt.Println(*p)
medium
A. Cannot assign int to *int without & operator
B. Cannot print pointer value with * operator
C. Variable a must be a pointer
D. Missing import fmt
Solution
Step 1: Check pointer assignment rules
Pointer variables must be assigned the address of a variable, not the variable's value.
Step 2: Identify missing & operator
Assigning a directly to p is invalid; it should be p = &a.
Final Answer:
Cannot assign int to *int without & operator -> Option A
Quick Check:
Pointer assignment needs &variable [OK]
Hint: Use & to assign address to pointer variables [OK]