Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to read a number from the user.
Go
var num int
fmt.Scanln([1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Passing the variable without & causes runtime error.
Using *num is invalid syntax for input.
โ Incorrect
In Go, to read input into a variable, you must pass its address using &.
2fill in blank
mediumComplete the code to print the value of num.
Go
fmt.Println([1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using &num prints the memory address, not the value.
Using *num causes error unless num is a pointer.
โ Incorrect
To print a variable's value, just use the variable name without &.
3fill in blank
hardFix the error in reading input into variable x.
Go
var x int
fmt.Scanln([1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Passing x instead of &x causes input to fail.
Using *x is invalid because x is not a pointer.
โ Incorrect
Input functions need the address of the variable to store the input.
4fill in blank
hardFill both blanks to create a program that reads and prints a string.
Go
var input string fmt.Scanln([1]) fmt.Println([2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using input instead of &input for reading input.
Using &input when printing prints the address.
โ Incorrect
Use &input to read input and input to print the value.
5fill in blank
hardFill all three blanks to read two integers and print their sum.
Go
var a, b int fmt.Scanln([1], [2]) sum := [3] fmt.Println(sum)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Passing a and b instead of &a and &b to Scanln.
Using subtraction instead of addition for sum.
โ Incorrect
Use &a and &b to read inputs, then add a and b for the sum.