Logo

Golang Questions Set 12:

Quiz Mode

Which of the following is not a valid control structure in Go Lang?

1
2
3
4

Solution:

What is the output of the following code snippet?

package main

import "fmt"

func main() {

   x := []int{1, 2, 3}

   y := &x

   (*y)[0] = 4

   fmt.Println(x[0])

}

Solution:

What is the correct syntax for a for loop in Go Lang?

1
2
3
4

Solution:

What is the output of the following code snippet?

package main

import "fmt"

func main() {

   x := []int{1, 2, 3}

   y := &x

   z := *y

   z[0] = 4

   fmt.Println(x[0])

}

Solution:

What is the output of the following code snippet?

package main

import "fmt"

func main() {

   x := 5

   y := &x

   z := &y

   fmt.Println(**z)

}

1
2
3
4

Solution:

What is the output of the following code?

package main

import "fmt"


func main() {

  a := []int{1, 2, 3, 4, 5}

  b := a[1:3]

  b[0] = 0

  fmt.Println(a)

}


1
2
3
4

Solution:

What is the output of the following code snippet?

package main

import "fmt"


func swap(a, b *int) {

   temp := *a

   *a = *b

   *b = temp

}


func main() {

   x := 10

   y := 20

   swap(&x, &y)

   fmt.Println(x, y)

}

1
2
3
4

Solution:

What will be the output of the following code?

package main

import "fmt"

func main() {

  numbers := []int{1, 2, 3, 4, 5}

  for i, num := range numbers {

    if i == 3 {

      continue

    }

    fmt.Println(num)

  }

}


1
2
3
4

Solution:

What is the output of the following code snippet?

package main

import "fmt"


func main() {

   var ptr *int

   if ptr != nil {

       fmt.Println("ptr is not nil")

   } else {

       fmt.Println("ptr is nil")

   }

}

1
2
3
4

Solution:

What will be the output of the following code?

package main

import "fmt"

func main() {

  var x int = 3

  var y int = 2

  if x > y {

    fmt.Println("x is greater than y")

  } else if y > x {

    fmt.Println("y is greater than x")

  }

}

1
2
3
4

Solution: