Go Online quiz - Part 07

Go Multiple Choice Questions and answers

These inquiries are based on a subject channel, a string, and a variety of additional elements. Channels are used in Go to communicate between routines. The first is the buffered channel, while the second is the unbuffered channel. More information on the channel may be found in our blog article, which we have previously published.


The majority of the questions are based on code snippets, and you must read the code and select the appropriate alternatives. First, attempt to answer these questions without using the compiler; as you answer these questions on paper, you will gain confidence and gain a better understanding of the program's process.
These kind of questions may be found in almost every interview, and the interviewer will use them to evaluate your knowledge. When you get these questions, you must answer them on paper.

Go Online quiz - Part 07

In terms of the Go quiz, these are really simple questions that will help you understand your basic grasp of Go fundamentals. They are also structured in such a manner that you may attempt them as many times as you like.


 

1. what is the output of below code snippet

package main

import (
        "fmt"
)

func printChannel(ch chan int) {
        for {
                select {
                case num := <-ch:
                        fmt.Printf("%d ", num)

                }
        }
}

func main() {
        ch := make(chan int)
        for i := 0; i < 5; i++ {
                ch <- i
        }
        go printChannel(ch)
}




 

2. what is the output of below code snippet

package main

import (
        "fmt"
)

func printChannel(ch chan int) {
        for {
                select {
                case num := <-ch:
                        fmt.Printf("%d ", num)

                }
        }
}

func main() {
        ch := make(chan int)
        go printChannel(ch)
        for i := 0; i < 5; i++ {
                ch <- i
        }
        
}




 

3. How to test a golang program?





 

4. What is the output of below code snippet?

package main

import "fmt"

func main() {
        sum := 0
        while i < 5 {
                sum += i
                i++
        }
        fmt.Println(sum)
}




 

5. What is the output of below code snippet?

package main

import "fmt"

func main() {
        arr := [...]int{5: 0}
        for i := 0; i < 5; i++ {
                arr = append(arr, i)
        }
        fmt.Println(arr)
}




 

6. What is the output of below code snippet?

package main

import "fmt"

func main() {
        arr := [...]int{5: 0}
        for i := 1; i < 5; i++ {
                arr[i] = i + 1
        }
        fmt.Println(arr)
}




 

7. What is the output of below code snippet?

package main

import "fmt"

func reverse(arr [6]int) {
        for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
                arr[i], arr[j] = arr[j], arr[i]
        }
}

func main() {
        arr := [...]int{5: 0}
        for i := 1; i < 5; i++ {
                arr[i] = i + 1
        }
        reverse(arr)
        fmt.Println(arr)
}




 

8. What is the output of below code snippet?

package main

import "fmt"

func reverse(arr [6]int) {
        for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
                arr[i], arr[j] = arr[j], arr[i]
        }
        fmt.Println(arr)
}

func main() {
        arr := [...]int{5: 0}
        for i := 1; i < 5; i++ {
                arr[i] = i + 1
        }
        reverse(arr)
}




Post a Comment

0 Comments