Skip to content

Latest commit

 

History

History
268 lines (192 loc) · 6.09 KB

03-variables.md

File metadata and controls

268 lines (192 loc) · 6.09 KB

Lab: Variables

Take me to the lab!

Help for the VSCode editor.

  1. What would be the valid statement for initialising a variable:

    Note that these answers may be in a different order each time you load the lab.

    • var name = "Lia"
    • var name string = "Lia"
    • var string name = "Lia"
    • var string name = 98.00
    Reveal

    var name string = "Lia"

    All others are syntax errors.

  2. Select all the correct statements for variable initialisation
    1. var name string = "Lia"
    2. name := "Lia"
    3. var s, t string = "Lia", "Harry"
    4. var b bool = "false"
    Reveal

    A, B, C

    The final decalration is incorrect because "false" is a string, not a boolean.

  3. Select the statements that would print and then introduce a new line in the output:
    1. fmt.Print(name)
    2. fmt.Print("Hey there \n")
    3. fmt.Println("Hey there")
    4. fmt.Printf(name)
    Reveal

    B, C

    Know that fmt.Print and fmt.Printf do not automatically emit a newline; fmt.Println does.

    Despite the above statement, 2 emits a newline because it includes the special character \n which is the newline character.

  4. Consider the following code snippet and select the correct output:

    For this, make use of the code editor. Create a new file e.g. test.go. Right click in explorer window to create the file, then paste the given code block into it.

    Run this code from the integrated terminal, and observe the output to find the answer.

    go run test.go
    
    Reveal

    D

    Understand what the format specifiers are doing in the Printf statements, and what was learned in the previous question:

    • %.2f is printing radius to 2 decimal places.
    • %.1f is printing PI to 1 decimal place.
    • %f is printing area to maximum precision of float64. Since the answer is exact at 8 significant figures, that is what's printed.
    • There is no newline before Thank You because the previous fmt.Printf did not emit one.
  5. What is the correct format specifier for printing a string with quotes

    Note that these answers may be in a different order each time you load the lab.

    • %v
    • %s
    • %q
    • %vv
    Reveal

    %q

    %s also prints strings, but does not include quotes.

  6. Among the following code snippets - select the correct ways of initializing variables
    1. var name := "Priyanka"
    2. var s,t string,int = "Priyanka", 100
    3. var (
          s string = "Priyanka"
          i int = 100
      )
    4. var i,j int = 100, 200
    Reveal

    C, D

    • You can't use := in a var declaration
    • You can't declare two separate types in a single line var declaration
  7. Select the correct statement...
    1. Inner blocks can access variables of outer blocks.
    2. Outer blocks can access variables within inner blocks.
    3. Outer blocks cannot access variables within inner blocks.
    4. Inner blocks can not access variables of outer blocks.
    Reveal

    A, C

  8. From the following code snippet, identify the local and global variable
    package main
    
    var a string = "foo"
    
    func main() {
        var b string = "bar"
        {
            var c string = "loreum"
        }
    }
    1. a, b - global, c - local
    2. a, b, c - local
    3. a, b, c - global
    4. a - global, b, c - local
    Reveal

    D

    Global variables are defined with var statements outside of any func.

  9. From the following code snippet - which line will cause error
    1. package main
    2.
    3. import "fmt"
    4.
    5. var a string = "foo"
    6.
    7. func main() {
    8.    var b string = "bar"
    9.    fmt.Println(a)
    10.   fmt.Println(b)
    11.   fmt.Println(c)
    12.  {
    13.       var c string = "loreum"
    14.       fmt.Println(a)
    15.       fmt.Println(b)
    16.       fmt.Println(c)
    17.  }
    18. }
    Reveal

    Line 11

    Variable c is declared in an inner block. Line 11 is trying to use c from an outer block, which as we discovered in Q7 is illegal.

  10. What is the zero value for the following variables
    var name string
    var b bool
    var f float64
    1. ""
      true
      1.0
    2. " "
      false
      1.0
    3. ""
      false
      0.00
    4. ""
      true
      0.0
    Reveal

    C

    Don't be fooled by the number of decimal places on the zero value - it's still zero and still a float64 for which the default is zero. That rules out the first two answers.

    The default for a boolean is always false, so that rules out the first and last answer.

    The default for a string is not a string containing one space character, so that rules out the second answer.

    This leaves only the third answer.