github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/assignments/testdata/tests/lab3/task-go-questions.md (about)

     1  # Multiple Choice Questions about Go Programming
     2  
     3  Answer the following questions by editing this file by replacing the `[ ]` for the correct answer with `[x]`.
     4  Only one choice per question is correct.
     5  Selecting more than one choice will result in zero points.
     6  No other changes to the text should be made.
     7  
     8  1. What is the zero value of an integer (`int`)?
     9  
    10      - [ ] a) `1`
    11      - [ ] b) `nil`
    12      - [ ] c) `0`
    13      - [ ] d) `NaN`
    14  
    15  2. What is the zero value of a slice?
    16  
    17      - [ ] a) `[_]`
    18      - [ ] b) `[0]`
    19      - [ ] c) `nil`
    20      - [ ] d) `null`
    21  
    22  3. Which of these is a correct way of declaring a slice?
    23  
    24      - [ ] a) `slice := [4]int{4, 5, 2, 1}`
    25      - [ ] b) `slice := {4, 5, 2, 1}int`
    26      - [ ] c) `slice := [4, 5, 2, 1]int`
    27      - [ ] d) `slice := []int{4, 5, 2, 1}`
    28  
    29  4. Given the `Person` struct below, which of these is the correct way of creating an instance of `Person`?
    30  
    31      - [ ] a) `var p Person{firstName: "Johnny", shortName: "Bravo", age: 43}`
    32      - [ ] b) `p := new Person(firstName: "Johnny", shortName: "Bravo", age: 43)`
    33      - [ ] c) `p := Person{firstName: "Johnny", shortName: "Bravo", age: 43}`
    34      - [ ] d) `p Person := {firstName: "Johnny", shortName: "Bravo", age: 43}`
    35  
    36      ```go
    37      type Person struct {
    38          firstName, shortName string
    39          age int
    40      }
    41      ```
    42  
    43  5. Which is the correct way to create a map `m` with the initial values `{"a": 1, "b": 2, "c": 3}`?
    44  
    45      - [ ] a) `m := make(map[string]int{"a": 1, "b": 2, "c": 3})`
    46      - [ ] b) `m := map[string]int{"a": 1, "b": 2, "c": 3}`
    47      - [ ] c) `m := make(map[string]int, []string{"a", "b", "c"}, []int{1, 2, 3})`
    48      - [ ] d) `m := {"a": 1, "b": 2, "c": 3}`
    49  
    50  6. How would you range over a slice?
    51  
    52      - [ ] a) `for a, b in range slice {}`
    53      - [ ] b) `for each a : slice {}`
    54      - [ ] c) `for a, b := range slice {}`
    55      - [ ] d) `for a := range slice[a] {}`
    56  
    57  7. Given a slice of integers, named `sli`. How would you append a number to this slice?
    58  
    59      - [ ] a) `sli.append(2)`
    60      - [ ] b) `sli = append(sli, 2)`
    61      - [ ] c) `sli[len(sli)] = 2`
    62      - [ ] d) `append(sli, 2)`
    63      - [ ] e) `sli += 2`
    64  
    65  8. Which condition below checks if the map `m` contains the key `b`?
    66  
    67      - [ ] a) `if _, hasKey := m["b"]; hasKey {`
    68      - [ ] b) `if "b" in m {`
    69      - [ ] c) `if m.hasKey("b") {`
    70      - [ ] d) `if m["b"] {`
    71  
    72  9. Given the `ChessPiece` interface and `Bishop` struct below.
    73     How would you implement the `ChessPiece` interface on the piece `Bishop`?
    74  
    75      - [ ] a) add the phrase `implements ChessPiece` after `struct`, then add the interface methods below the struct
    76      - [ ] b) write methods with the same names as defined in the `ChessPiece` interface with `Bishop` as the receiver
    77      - [ ] c) write the required functions from the `ChessPiece` interface directly inside the `Bishop` struct
    78      - [ ] d) none of the above
    79  
    80      ```go
    81      type ChessPiece interface {
    82          Move(x, y, int)
    83          GetPos() (x, y int)
    84      }
    85  
    86      type Bishop struct {
    87          posX, posY int
    88      }
    89      ```
    90  
    91  10. What does this program print?
    92  
    93      ```go
    94      var s = "¡¡¡Hello, Gophers!!!"
    95      s = strings.TrimSuffix(s, ", Gophers!!!")
    96      s = strings.TrimPrefix(s, "¡¡¡")
    97      fmt.Print(s)
    98      ```
    99  
   100      - [ ] a) `Hello`
   101      - [ ] b) `Hello, ¡¡¡`
   102      - [ ] c) `¡¡¡Hello, Gophers!!!`
   103      - [ ] d) `, Gophers!!!Hello¡¡¡`
   104  
   105  11. What is the type of `pi` in `pi := fmt.Sprintf("%.2f", 3.1415)`?
   106  
   107      - [ ] a) `int`
   108      - [ ] b) `float`
   109      - [ ] c) `float64`
   110      - [ ] d) `string`