迹忆客 计算机编程题库

Go 笔试题精选

1.
(多选题)如果 Add 函数的调用代码如下所示客,则Add 函数定义正确的是 ```go func main() { var a Integer = 1 var b Integer = 2 var i interface{} = &a sum := i.(*Integer).Add(b) fmt.Println(sum) } ```
  • ```go type Integer int func (a Integer) Add(b Integer) Integer { return a + b } ```
  • ```go type Integer int func (a Integer) Add(b *Integer) Integer { return a + *b } ```
  • ```go type Integer int func (a *Integer) Add(b Integer) Integer { return *a + b } ```
  • ```go type Integer int func (a *Integer) Add(b *Integer) Integer { return *a + *b } ```
2.
(多选题)关于 slice 或 map 操作,下面正确的是()
  • ```go var s []int s = append(s,1) ```
  • ```go var m map[string]int m["one"] = 1 ```
  • ```go var s []int s = make([]int, 0) s = append(s,1) ```
  • ```go var m map[string]int m = make(map[string]int) m["one"] = 1 ```
3.
(单选题)关于类型转化,下面语法正确的是()
  • ```go type MyInt int var i int = 1 var jMyInt = i ```
  • ```go type MyIntint var i int= 1 var jMyInt = (MyInt)i ```
  • ```go type MyIntint var i int= 1 var jMyInt = MyInt(i) ```
  • ```go type MyIntint var i int= 1 var jMyInt = i.(MyInt) ```
4.
(单选题)如果 Add 函数的调用代码如下所示,则Add函数定义正确的是 ```go func main() { var a Integer = 1 var b Integer = 2 var i interface{} = a sum := i.(Integer).Add(b) fmt.Println(sum) } ```
  • ```go type Integer int func (a Integer) Add(b Integer) Integer { return a + b } ```
  • ```go type Integer int func (a Integer) Add(b *Integer) Integer { return a + *b } ```
  • ```go type Integer int func (a *Integer) Add(b Integer) Integer { return *a + b } ```
  • ```go type Integer int func (a *Integer) Add(b *Integer) Integer { return *a + *b } ```
5.
(多选题)关于const常量定义,下面正确的使用方式是()
  • ```go const Pi float64 = 3.14159265358979323846 const zero = 0.0 ```
  • ```go const ( size int64 = 1024 eof = -1 ) ```
  • ```go const ( ERR_ELEM_EXIST error = errors.New("element already exists") ERR_ELEM_NT_EXIST error = errors.New("element not exists") ) ```
  • ```go const u, v float32 = 0, 3 const a, b, c = 3, 4, "foo" ```
6.
(单选题)从切片中删除一个元素,下面的算法实现正确的是()
  • ```go func (s *Slice)Remove(value interface{}) error { for i, v := range *s { if isEqual(value, v) { if i== len(*s) - 1 { *s = (*s)[:i] }else { *s = append((*s)[:i],(*s)[i + 2:]...) } return nil } } return ERR_ELEM_NT_EXIST } ```
  • ```go func (s *Slice)Remove(value interface{}) error { for i, v := range *s { if isEqual(value, v) { *s = append((*s)[:i],(*s)[i + 1:]) return nil } } return ERR_ELEM_NT_EXIST } ```
  • ```go func (s *Slice)Remove(value interface{}) error { for i, v := range *s { if isEqual(value, v) { delete(*s, v) return nil } } return ERR_ELEM_NT_EXIST } ```
  • ```go func (s *Slice)Remove(value interface{}) error { for i, v := range *s { if isEqual(value, v) { *s = append((*s)[:i],(*s)[i + 1:]...) return nil } } return ERR_ELEM_NT_EXIST } ```
7.
(多选题)关于变量的自增和自减操作,下面语句正确的是()
  • ```go i := 1 i++ ```
  • ```go i := 1 j = i++ ```
  • ```go i := 1 ++i ```
  • ```go i := 1 i-- ```
8.
(单选题)下面代码运行时输出的结果是什么? ```go package main import ( "fmt" "time" ) func main() { ch1 := make(chan int) go fmt.Println(<-ch1) ch1 <- 5 time.Sleep(1 * time.Second) } ```
  • 5
  • 编译不通过
  • 运行时死锁
9.
(单选题)下面代码运行时输出的结果是什么? ```go package main import ( "fmt" "time" ) func main() { ch1 := make(chan int) go func(){ fmt.Println(<-ch1) }() ch1 <- 5 time.Sleep(1 * time.Second) } ```
  • 5
  • 编译不通过
  • 运行时死锁
10.
(单选题)高并发情况下,map常见问题分析 代码如下,正确的是() ```go package main func main() { Map := make(map[int]int) for i := 0; i < 100000; i++ { go writeMap(Map, i, i) go readMap(Map, i) } } func readMap(Map map[int]int, key int) int { return Map[key] } func writeMap(Map map[int]int, key int, value int) { Map[key] = value } ```
  • 程序正常执行
  • 程序根本编译不过
  • fatal error: concurrent map read and map write
11.
(单选题)关于异常的触发,下面说法正确的是()
  • 空指针解析
  • 下标越界
  • 除数为0
  • 调用panic函数
扫码一下
查看教程更方便