Help for the VSCode editor.
-
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.
-
Select all the correct statements for variable initialisation
var name string = "Lia"
name := "Lia"
var s, t string = "Lia", "Harry"
var b bool = "false"
Reveal
A, B, C
The final decalration is incorrect because
"false"
is a string, not a boolean. -
Select the statements that would print and then introduce a new line in the output:
fmt.Print(name)
fmt.Print("Hey there \n")
fmt.Println("Hey there")
fmt.Printf(name)
Reveal
B, C
Know that
fmt.Print
andfmt.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. -
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 offloat64
. Since the answer is exact at 8 significant figures, that is what's printed.- There is no newline before
Thank You
because the previousfmt.Printf
did not emit one.
-
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. -
Among the following code snippets - select the correct ways of initializing variables
-
var name := "Priyanka"
-
var s,t string,int = "Priyanka", 100
-
var ( s string = "Priyanka" i int = 100 )
-
var i,j int = 100, 200
Reveal
C, D
- You can't use
:=
in avar
declaration - You can't declare two separate types in a single line
var
declaration
-
-
Select the correct statement...
- Inner blocks can access variables of outer blocks.
- Outer blocks can access variables within inner blocks.
- Outer blocks cannot access variables within inner blocks.
- Inner blocks can not access variables of outer blocks.
Reveal
A, C
-
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" } }
a
,b
- global,c
- locala
,b
,c
- locala
,b
,c
- globala
- global,b
,c
- local
Reveal
D
Global variables are defined with
var
statements outside of anyfunc
. -
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 usec
from an outer block, which as we discovered in Q7 is illegal. -
What is the zero value for the following variables
var name string var b bool var f float64
-
"" true 1.0
-
" " false 1.0
-
"" false 0.00
-
"" 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.
-