0
0
Goprogramming~10 mins

Output formatting basics in Go - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A"%v"
B"%d"
C"%f"
D"%s"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using %d instead of %s causes wrong output or errors.
Forgetting to include quotes around the format specifier.
2fill in blank
medium

Complete 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'
A"%.2f"
B"%d"
C"%s"
D"%v"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using %d for floats causes wrong output.
Using %s prints the float as a string, not formatted.
3fill in blank
hard

Fix 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'
A"%05d"
B"%5d"
C"%d"
D"%f"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using %5d pads with spaces, not zeros.
Using %f for integers causes errors.
4fill in blank
hard

Fill 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'
A"%-10s"
B"%10s"
C"%5d"
D"%-5d"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using %10s for string aligns right, not left.
Using %-5d left-aligns integer, but question wants right-aligned.
5fill in blank
hard

Fill 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'
A"%.3f"
B"%8s"
C"%04d"
D"%-8s"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using %s without width does not align string.
Using %d without zero padding for code.