Skip to content

Instantly share code, notes, and snippets.

@sangam14
Created September 4, 2021 15:22
Show Gist options
  • Save sangam14/4b45ae7cca343119ed9d861025b38539 to your computer and use it in GitHub Desktop.
Save sangam14/4b45ae7cca343119ed9d861025b38539 to your computer and use it in GitHub Desktop.
Golang-MCQ-ans
Q1: What's the output of the following code?
```go
package main
import "fmt"
const (
a = iota
b = iota
c = iota
)
const (
d, e, f = iota, iota, iota
)
func main() {
fmt.Println(a, b, c, d, e, f)
}
```
a. 0 1 2 3 4 5
b. 0 1 2 0 1 2
c. 0 1 2 0 0 0
d. 0 0 0 0 1 2
A: c
Q2: What's the output of the following code?
```go
package main
import "fmt"
func main() {
var a int8 = 3
var b int16 = 4
sum := a + b
fmt.Println(sum)
}
```
a. 7
b. 4
c. 3
d. invalid operation: a + b (mismatched types int8 and int16)
A: d
Q3: Can short declaration ":=" be used for defining global variables?
a. Yes
b. No
A: b
Q4: Which one of the following is correct?
a. const Pi = math.Pi
b. const Pi = 3.14
c. both a and b are correct
d. None of the above
A: c
Q5: Arrays are value types. In case of arrays perform as arguments, functions get their copies instead of a reference. Don’t they?
a. Yes
b. No
A: a
Q6: Which of the following variables are exportable from another external package?
```go
package main
var (
aName string
BigBro string
爱 string
)
func main() {
}
```
a. aName, BigBro
b. BigBro
c. aName, BigBro, 爱
d. BigBro, 爱
A: b
Q7: What's the sequence for the output of the following code?
```go
package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
wg.Add(1)
go func() {
fmt.Println("1")
wg.Done()
}()
wg.Add(1)
go func() {
fmt.Println("2")
wg.Done()
}()
wg.Wait()
fmt.Println("3")
}
```
a. Always 1 2 3
b. Always 2 1 3
c. 3
d. "1 2 3" or "2 1 3"
A: d
Q8: How to compile the following code to the binary file with name "eight"?
```go
package main
import "fmt"
func main() {
fmt.Println("Hi")
}
```
a. go build 8.go eight
b. go build -o eight 8.go
c. go build 8.go -o eight
A: b
Q9: Can we set DEBUG=true with *go build*?
```go
package main
import (
"fmt"
)
var DEBUG bool
func main() {
fmt.Printf("DEBUG is %t\n", DEBUG)
}
```
a. Yes. go build -ldflags '-X main.DEBUG=true' 9/9.go
b. No
A: b
Q10: What verb should we use with fmt.Printf to print boolean?
a. %s
b. %t
c. %b
A: b
Q11:- Q: Does Go support type inheritance?
a. Yes
b. No
A: b
Q12:- Is it possible to declare multiple types of variables in single declaration in Go?
var a, b, c = 3, 4, "foo"
a. Yes
b. No
A: a
Q13:- Q: Is it possible to make package content directly accessible without need to be preceeded by "fmt."?
a. Yes. import "fmt"
b. Yes. import _ "fmt"
c. Yes. import . "fmt"
d. No
A: c
Q14:- Q: Is it possible to have multiple tag strings in struct field?
```go
package main
import (
"fmt"
"reflect"
)
func main() {
type User struct {
Name string `key:"name"`
}
u := User{}
st := reflect.TypeOf(u)
field := st.Field(0)
fmt.Println(field.Tag.Get("key"))
}
```
a. Yes. They should be comma-separated: `key:"name",maxlength:"128"`
b. Yes. They should be space-separated: `key:"name" maxlength:"128"`
c. No
A: b
Q15:- Q: What is the output of the following code?
```go
package main
import "fmt"
func main() {
for i := 0; i < 4; i++ {
defer fmt.Print(i)
}
}
```
a. 0123
b. 3210
c. 0000
d. 3333
A: b
Q16:- Q: What characters does *go fmt* command use for indent?
a. 4 spaces
b. 2 spaces
c. tab character
d. it's configurable
A: c
Q17:- Q: Choose the correct statement regarding the output of the following code.
```go
package main
import "fmt"
func main() {
tagsViews := map[string]int{
"unix": 0,
"python": 1,
"go": 2,
"golang": 3,
"devops": 4,
"gc": 5,
}
for key, views := range tagsViews {
fmt.Println("There are", views, "views for", key)
}
}
```
a. Output will be ordered by values
b. Output will be ordered by keys
c. Output will be ordered randomly
d. Output will be ordered by position in the code
A: c
Q18:- How can we change the value of GOMAXPROCS in Go?
a. Via environment variable GOMAXPROCS
b. In the code
c. a and b
d. Impossible as it's equal to the number of available CPUs
A: c
Q19:- Q: Which is the slowest concatenation method from the list?
```go
package main
import (
"bytes"
"fmt"
"strings"
)
func main() {
// 1
s := ""
for i := 0; i < 10; i++ {
s += "a"
}
fmt.Println(s)
// 2
var buffer bytes.Buffer
for i := 0; i < 10; i++ {
buffer.WriteString("a")
}
fmt.Println(buffer.String())
// 3
sl := []string{"a", "a", "a", "a", "a", "a", "a", "a", "a", "a"}
fmt.Printf(strings.Join(sl, ""))
}
```
a. 1
b. 2
c. 3
A: a
Q20:- Where should we use *defer* in the following code?
```go
package main
import "os"
func main() {
src, err := os.Open("filename")
// 1
//defer src.Close()
if err != nil {
return
}
// 2
//defer src.Close()
src.WriteString("Hello")
// 3
//defer src.Close()
}
```
a. 1
b. 2
c. 3
d. None of them
A: b
Q21:Q: What data types can you use "for - range" statement?
a. array, slice, map
b. array, slice, map, string, channel
c. slice, map, string
d. slice, map
A: b
Q22:- Q: Does Go support optional parameters in functions?
a. Yes
b. No
A: b
Q23: Q: When will init() function be called?
a. Before main() function in main package
b. After importing a package with defined init() function
c. Only when you call it
d. a and b
A: d
Q24:- Q: Does Go have a ternary operator?
a. Yes
b. No
A: b
Q25:- Q: Is it possible to check if the slices are equal with "==" operator or not?
a. Yes
b. No
A: b
Q26:-
Q: What is the size of the following struct?
```go
package main
import "fmt"
import "unsafe"
func main() {
s := struct {
A float32
B string
}{0, "go"}
fmt.Printf("%T, %d\n", s, unsafe.Sizeof(s))
}
```
a. 12
b. 8
c. 12 bytes on architectures with 4 bytes pointers and 24 on architectures with 8 bytes pointers
d. 24
A: c
Q27:- Q: What's the default buffer size of the channel in Go?
a. 0
b. 1
c. No default size
A: a
Q28 :- Q: What are the default values of these types: string, `*string` ?
a. "", nil
b. "", ""
c. nil, nil
d. nil, ""
A: a
Q29:- Q: Is it possible to define constant of an array type float32?
```go
package main
import "fmt"
const array = []float32{0.1, 0.2, 0.3}
func main() {
fmt.Printf("%v", array)
}
```
a. Yes
b. No
A: b
Q30:- Q: In Go, does a *break* statement exit from a *select* block?
a. Yes
b. No
A: a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment