Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print a formatted string with a name.
Go
package main import "fmt" func main() { name := "Alice" fmt.Printf("Hello, [1]!\n", name) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using %d instead of %s causes wrong output or errors.
Forgetting to include quotes around the format specifier.
โ Incorrect
The %s verb formats a string in Go's fmt.Printf function.
2fill in blank
mediumComplete the code to print a floating-point number with two decimal places.
Go
package main import "fmt" func main() { pi := 3.14159 fmt.Printf("Pi is approximately [1]\n", pi) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using %d for floats causes wrong output.
Using %s prints the float as a string, not formatted.
โ Incorrect
The %.2f verb formats a float with two decimal places.
3fill in blank
hardFix the error in the code to print an integer with leading zeros to make it 5 digits.
Go
package main import "fmt" func main() { num := 42 fmt.Printf("Number: [1]\n", num) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using %5d pads with spaces, not zeros.
Using %f for integers causes errors.
โ Incorrect
The %05d verb prints an integer padded with zeros to width 5.
4fill in blank
hardFill both blanks to print a string left-aligned in a 10-character wide field and an integer right-aligned in a 5-character wide field.
Go
package main import "fmt" func main() { name := "Bob" age := 30 fmt.Printf("Name: [1] Age: [2]\n", name, age) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using %10s for string aligns right, not left.
Using %-5d left-aligns integer, but question wants right-aligned.
โ Incorrect
Use %-10s for left-aligned string in 10 spaces and %5d for right-aligned integer in 5 spaces.
5fill in blank
hardFill all three blanks to print a float with 3 decimals, a string right-aligned in 8 spaces, and an integer padded with zeros to 4 digits.
Go
package main import "fmt" func main() { price := 9.87654 product := "Pen" code := 7 fmt.Printf("Price: [1] Product: [2] Code: [3]\n", price, product, code) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using %s without width does not align string.
Using %d without zero padding for code.
โ Incorrect
Use %.3f for float with 3 decimals, %8s for right-aligned string in 8 spaces, and %04d for zero-padded 4-digit integer.