Learn Pointers in Go

Question 1

package main

import "fmt"

func main() {
    x := 10
    p := &x
    *p = 20
    fmt.Println(x)
}

The Correct answer is: 20

Explanation: x is initialized to 10. p is a pointer to x.*p = 20 changes the value of x to 20 through the pointer p.

fmt.Println(x) prints the updated value of x, which is 20

Question 2

package main

import "fmt"

func main() {
    var p *int
    x := 5
    p = &x
    fmt.Println(*p)
}

The Correct Answer is: 5

Explanation: p is a pointer to x. x is initialized to 5. p points to x, so p dereferences the pointer and accesses the value of x.

fmt.Println(p) prints the value at the address p points to, which is 5.

Question 3

package main

import "fmt"

func main() {
    p := new(int)
    *p = 42
    fmt.Println(*p)
}

The Correct answer is: 42

Explanation: p := new(int) creates a new int with a default value of 0 and returns a pointer to it.
p = 42 assigns the value 42 to the integer that p points to.

fmt.Println(p) prints the value pointed to by p, which is 42

Question 4

package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{Name: "Alice", Age: 30}
    pPtr := &p
    pPtr.Age = 35
    fmt.Println(p.Age)
}

The Correct answer is: 35

Explanation: p is a Person struct with Name set to “Alice” and Age set to 30.
pPtr is a pointer to p.
pPtr.Age = 35 updates the Age field of the Person instance p through the pointer pPtr.
fmt.Println(p.Age) prints the updated value of Age, which is 35.

Question 5

package main

import "fmt"

func main() {
    var p *int
    if p == nil {
        fmt.Println("p is nil")
    } else {
        fmt.Println("p is not nil")
    }
}

The correct answer is: p is nil

Explanation: var p *int declares a pointer p and initializes it to nil (the zero value for pointers).
The if p == nil condition checks if p is nil, which it is.
Therefore, fmt.Println(“p is nil”) prints that p is indeed nil.

Question 6

package main

import "fmt"

func increment(p *int) {
    *p++
}

func main() {
    x := 5
    increment(&x)
    fmt.Println(x)
}

The correct answer is: 6

Explanation: x is initialized to 5.
The increment function takes a pointer to x and increments the value at that pointer.
After calling increment(&x), x is increased by 1, resulting in 6.
fmt.Println(x) prints the updated value of x, which is 6.

Question 7

package main

import "fmt"

func swap(a, b *int) {
    *a, *b = *b, *a
}

func main() {
    x, y := 10, 20
    swap(&x, &y)
    fmt.Println(x, y)
}

The correct answer is: 20, 10

Explanation: x is initialized to 10 and y is initialized to 20.
The swap function takes pointers to x and y and swaps their values.
*a, *b = *b, *a swaps the values pointed to by a and b, so x becomes 20 and y becomes 10.
fmt.Println(x, y) prints the swapped values: 20,10.

Question 8

package main

import "fmt"

func main() {
    x := 1
    p1 := &x
    p2 := p1
    *p2 = 2
    fmt.Println(x)
}

The correct answer is: 2

Explanation: x is initialized to 1.
p1 is a pointer to x.
p2 is assigned the value of p1, so p2 is also a pointer to x.
*p2 = 2 updates the value at the address pointed to by p2, which is the same address as p1, so x is set to 2.
fmt.Println(x) prints the updated value of x, which is now 2.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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