Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to call the function greet.
Go
package main import "fmt" func greet() { fmt.Println("Hello!") } func main() { [1]() }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong function name like 'print' or 'hello'.
Forgetting the parentheses after the function name.
✗ Incorrect
The function
greet is called by writing its name followed by parentheses: greet().2fill in blank
mediumComplete the code to return the sum of two integers from the function add.
Go
package main import "fmt" func add(a int, b int) int { return [1] } func main() { fmt.Println(add(3, 4)) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to return the result.
✗ Incorrect
The function should return the sum of
a and b, which is a + b.3fill in blank
hardFix the error in the function call to multiply.
Go
package main import "fmt" func multiply(x int, y int) int { return x * y } func main() { result := multiply[1] fmt.Println(result) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Missing closing parenthesis.
Including only one parenthesis or none.
✗ Incorrect
Function calls require parentheses around all arguments:
multiply(5, 6).4fill in blank
hardFill both blanks to create a function isEven that returns true if a number is even.
Go
package main import "fmt" func isEven(num int) bool { return num [1] 2 [2] 0 } func main() { fmt.Println(isEven(4)) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using division (/) instead of modulus (%).
Using != instead of == for comparison.
✗ Incorrect
To check if a number is even, use the modulus operator (%) to get the remainder and compare it to zero with ==.
5fill in blank
hardFill all three blanks to create a function filterPositive that returns a slice of positive numbers from input.
Go
package main import "fmt" func filterPositive(nums []int) []int { var result []int for _, [1] := range nums { if [2] [3] 0 { result = append(result, num) } } return result } func main() { fmt.Println(filterPositive([]int{-2, 3, 0, 5, -1})) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable name in the loop or condition.
Using < instead of > in the condition.
✗ Incorrect
The loop variable is
num. The condition checks if num > 0 to keep positive numbers.