Bird
Raised Fist0
Goprogramming~20 mins

Output formatting basics in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Go Output Formatting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this Go code using fmt.Printf?
Consider the following Go code snippet. What will it print to the console?
Go
package main

import "fmt"

func main() {
    x := 42
    y := 3.14159
    fmt.Printf("x = %d, y = %.2f\n", x, y)
}
Ax = 42, y = 3.1416
Bx = 42, y = 3.14159
Cx = 42, y = 3.14
Dx = 42, y = 3.1
Attempts:
2 left
๐Ÿ’ก Hint
Look at the %.2f format specifier. It controls how many decimal places are shown.
โ“ Predict Output
intermediate
2:00remaining
What does this Go code print with width formatting?
What is the output of this Go program that uses width specifiers in fmt.Printf?
Go
package main

import "fmt"

func main() {
    name := "Go"
    fmt.Printf("|%10s|%-10s|\n", name, name)
}
A| Go|Go |
B|Go | Go|
C| Go |Go |
D| Go| Go|
Attempts:
2 left
๐Ÿ’ก Hint
The %10s means right-align in 10 spaces, %-10s means left-align in 10 spaces.
โ“ Predict Output
advanced
2:00remaining
What is the output of this Go code using fmt.Sprintf with multiple verbs?
What string does this Go code produce?
Go
package main

import "fmt"

func main() {
    s := fmt.Sprintf("%d + %d = %d", 7, 5, 7+5)
    fmt.Println(s)
}
A7 + 5 = 0
B7 + 5 = 7+5
C7 + 5 = 75
D7 + 5 = 12
Attempts:
2 left
๐Ÿ’ก Hint
fmt.Sprintf formats the string with the given values replacing %d verbs.
โ“ Predict Output
advanced
2:00remaining
What error does this Go code produce?
What happens when you run this Go code?
Go
package main

import "fmt"

func main() {
    fmt.Printf("%d %s", 10)
}
A10 %s
Bruntime error: missing argument for %s
C10
DCompilation error: wrong number of arguments
Attempts:
2 left
๐Ÿ’ก Hint
Check if the number of verbs matches the number of arguments.
๐Ÿง  Conceptual
expert
3:00remaining
How many items are printed by this Go code with fmt.Printf and a slice?
What is the output count of items printed by this Go code snippet?
Go
package main

import "fmt"

func main() {
    nums := []int{1, 2, 3}
    fmt.Printf("%v %v %v %v", nums[0], nums[1], nums[2], nums)
}
APrints 4 items: 1 2 3 [1 2 3]
BPrints 3 items: 1 2 3
CPrints 1 item: [1 2 3]
DCompilation error: cannot print slice with %v
Attempts:
2 left
๐Ÿ’ก Hint
Each %v corresponds to one argument. The last argument is the whole slice.

Practice

(1/5)
1. What does the %d format specifier do in fmt.Printf in Go?
easy
A. Prints an integer value
B. Prints a floating-point number
C. Prints a string
D. Prints a boolean value

Solution

  1. Step 1: Understand format specifiers in Go

    The %d specifier is used to print decimal integers in Go's fmt.Printf.
  2. Step 2: Match specifier to data type

    Since %d prints integers, it matches integer values, not floats, strings, or booleans.
  3. Final Answer:

    Prints an integer value -> Option A
  4. Quick Check:

    %d = integer [OK]
Hint: Remember %d is for decimal integers in printf [OK]
Common Mistakes:
  • Confusing %d with %f for floats
  • Using %d for strings
  • Thinking %d prints booleans
2. Which of the following is the correct syntax to print a string variable name using fmt.Printf in Go?
easy
A. fmt.Printf("%d", name)
B. fmt.Printf("%f", name)
C. fmt.Printf("%s", name)
D. fmt.Printf(name)

Solution

  1. Step 1: Identify the correct format specifier for strings

    The format specifier for strings in Go's fmt.Printf is %s.
  2. Step 2: Check the syntax correctness

    fmt.Printf("%s", name) uses fmt.Printf("%s", name), which correctly prints the string variable name.
  3. Final Answer:

    fmt.Printf("%s", name) -> Option C
  4. Quick Check:

    %s prints strings [OK]
Hint: Use %s for strings in fmt.Printf [OK]
Common Mistakes:
  • Using %d or %f for strings
  • Omitting quotes around format string
  • Passing variable without format specifier
3. What is the output of the following Go code?
package main
import "fmt"
func main() {
    x := 7
    y := 3.14
    fmt.Printf("x = %d, y = %.1f\n", x, y)
}
medium
A. x = 7, y = 3.1
B. x = 7, y = 3.14
C. x = 7, y = 3.0
D. x = 7, y = 3

Solution

  1. Step 1: Understand the format specifiers used

    %d prints integer x as 7. %.1f prints floating-point y rounded to 1 decimal place.
  2. Step 2: Apply rounding to y

    3.14 rounded to 1 decimal place is 3.1, so output is "x = 7, y = 3.1".
  3. Final Answer:

    x = 7, y = 3.1 -> Option A
  4. Quick Check:

    %.1f rounds float to 1 decimal [OK]
Hint: %.1f rounds float to 1 decimal place [OK]
Common Mistakes:
  • Expecting full float without rounding
  • Confusing %d with %f
  • Ignoring newline character
4. Identify the error in this Go code snippet:
package main
import "fmt"
func main() {
    name := "Alice"
    age := 30
    fmt.Printf("Name: %s, Age: %f\n", name, age)
}
medium
A. Missing newline character in format string
B. Missing import statement
C. Incorrect variable declaration
D. Using %f to print an integer variable

Solution

  1. Step 1: Check format specifiers against variable types

    name is a string, printed with %s correctly. age is an integer but printed with %f, which expects a float.
  2. Step 2: Identify the mismatch causing error

    Using %f for an integer causes a runtime error or incorrect output.
  3. Final Answer:

    Using %f to print an integer variable -> Option D
  4. Quick Check:

    Match %f with float, not int [OK]
Hint: Match %f only with float variables [OK]
Common Mistakes:
  • Using %f for integers
  • Ignoring type mismatch warnings
  • Assuming %f works for all numbers
5. You want to print a table of product names and prices aligned in columns using fmt.Printf. Which format string correctly aligns the name left in 10 spaces and price right in 6 spaces with 2 decimals?
fmt.Printf("...", name, price)
hard
A. "%10s %-6.2f\n"
B. "%-10s %6.2f\n"
C. "%10s %6.2f\n"
D. "%-10s %-6.2f\n"

Solution

  1. Step 1: Understand alignment flags in format strings

    In Go, %-10s means left-align string in 10 spaces; %6.2f means right-align float in 6 spaces with 2 decimals.
  2. Step 2: Match alignment to requirements

    We want product name left-aligned in 10 spaces and price right-aligned in 6 spaces with 2 decimals, so "%-10s %6.2f\n" fits.
  3. Final Answer:

    "%-10s %6.2f\n" -> Option B
  4. Quick Check:

    Left-align string with %-10s, right-align float with %6.2f [OK]
Hint: Use - for left-align strings, no - for right-align numbers [OK]
Common Mistakes:
  • Swapping alignment flags
  • Using wrong width or precision
  • Forgetting to specify decimal places for floats
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard