Bird
Raised Fist0
Goprogramming~20 mins

Output using fmt.Println in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Go fmt.Println Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of fmt.Println with multiple arguments
What is the output of this Go code snippet?
Go
package main
import "fmt"
func main() {
    fmt.Println("Hello", "World", 2024)
}
AHelloWorld2024
BHello World 2024
C"Hello World 2024"
DHello, World, 2024
Attempts:
2 left
๐Ÿ’ก Hint
fmt.Println separates arguments with spaces by default.
โ“ Predict Output
intermediate
2:00remaining
Output of fmt.Println with newline characters
What will this Go program print?
Go
package main
import "fmt"
func main() {
    fmt.Println("Line1\nLine2")
}
A
Line1
Line2
BLine1\nLine2
CLine1 Line2
DLine1\n Line2
Attempts:
2 left
๐Ÿ’ก Hint
The string contains an escape sequence for newline.
โ“ Predict Output
advanced
2:00remaining
Output of fmt.Println with slice argument
What is the output of this Go code?
Go
package main
import "fmt"
func main() {
    nums := []int{1, 2, 3}
    fmt.Println(nums)
}
A1,2,3
B1 2 3
C[1 2 3]
D[]int{1, 2, 3}
Attempts:
2 left
๐Ÿ’ก Hint
fmt.Println prints slices with brackets and spaces.
โ“ Predict Output
advanced
2:00remaining
Output of fmt.Println with pointer variable
What will this Go program print?
Go
package main
import "fmt"
func main() {
    x := 10
    p := &x
    fmt.Println(p)
}
A&10
B10
Cp
D0xc0000140b0
Attempts:
2 left
๐Ÿ’ก Hint
Printing a pointer shows its memory address.
โ“ Predict Output
expert
2:00remaining
Output of fmt.Println with interface and type assertion
What is the output of this Go code?
Go
package main
import "fmt"
func main() {
    var i interface{} = 42
    fmt.Println(i)
    if v, ok := i.(int); ok {
        fmt.Println(v * 2)
    }
}
A
42
84
B
42
42
C
interface value
84
D
42
<nil>
Attempts:
2 left
๐Ÿ’ก Hint
Type assertion extracts the int value from interface{}.

Practice

(1/5)
1. What does the fmt.Println function do in Go?
easy
A. Creates a new package
B. Reads input from the user
C. Declares a new variable
D. Prints text or values to the screen with a new line

Solution

  1. Step 1: Understand the purpose of fmt.Println

    The function fmt.Println is used to display output on the screen, automatically adding a new line after the output.
  2. Step 2: Differentiate from other functions

    It does not read input, declare variables, or create packages; it only prints output.
  3. Final Answer:

    Prints text or values to the screen with a new line -> Option D
  4. Quick Check:

    fmt.Println prints output with newline [OK]
Hint: Remember: println means print with a new line [OK]
Common Mistakes:
  • Confusing fmt.Println with input functions
  • Thinking it declares variables
  • Assuming it creates packages
2. Which of the following is the correct way to print "Hello, Go!" using fmt.Println?
easy
A. fmt.Println("Hello, Go!")
B. fmt.Println('Hello, Go!')
C. fmt.Println(Hello, Go!)
D. fmt.Println(Hello Go)

Solution

  1. Step 1: Check string syntax in Go

    Strings must be enclosed in double quotes " ". Single quotes ' ' are for single characters.
  2. Step 2: Validate the options

    fmt.Println("Hello, Go!") uses double quotes correctly. Options A, B, and C either use single quotes or no quotes, which causes errors.
  3. Final Answer:

    fmt.Println("Hello, Go!") -> Option A
  4. Quick Check:

    Strings need double quotes in fmt.Println [OK]
Hint: Use double quotes for strings in fmt.Println [OK]
Common Mistakes:
  • Using single quotes for strings
  • Omitting quotes around text
  • Forgetting commas inside print
3. What will be the output of this Go code?
package main
import "fmt"
func main() {
    fmt.Println("Go", "is", "fun")
}
medium
A. Go is fun
B. Goisfun
C. "Go is fun"
D. Go, is, fun

Solution

  1. Step 1: Understand fmt.Println with multiple arguments

    When multiple values are separated by commas, fmt.Println prints them separated by spaces automatically.
  2. Step 2: Analyze the output

    The output will be the words separated by spaces: Go is fun, followed by a new line.
  3. Final Answer:

    Go is fun -> Option A
  4. Quick Check:

    fmt.Println adds spaces between arguments [OK]
Hint: Multiple arguments print with spaces automatically [OK]
Common Mistakes:
  • Expecting no spaces between words
  • Thinking commas print literally
  • Confusing output with quotes
4. Find the error in this Go code snippet:
package main
import "fmt"
func main() {
    fmt.Println("Hello" + "World")
}
medium
A. Syntax error due to missing comma
B. Runtime error because + can't join strings
C. No error, it prints HelloWorld
D. fmt.Println requires parentheses

Solution

  1. Step 1: Check string concatenation in Go

    Using + between strings concatenates them without spaces.
  2. Step 2: Verify fmt.Println usage

    fmt.Println can print the concatenated string without error.
  3. Final Answer:

    No error, it prints HelloWorld -> Option C
  4. Quick Check:

    String + concatenates; fmt.Println prints result [OK]
Hint: String + joins text; no comma needed inside fmt.Println [OK]
Common Mistakes:
  • Thinking + causes syntax error in print
  • Expecting space between Hello and World
  • Confusing + with comma separator
5. You want to print the numbers 1, 2, and 3 on the same line separated by spaces using fmt.Println. Which code snippet achieves this correctly?
hard
A. fmt.Println(1 2 3)
B. fmt.Println("1", "2", "3")
C. fmt.Println("1" "2" "3")
D. fmt.Println(1 + 2 + 3)

Solution

  1. Step 1: Understand printing multiple values

    Using commas in fmt.Println prints each argument separated by spaces automatically.
  2. Step 2: Analyze each option

    fmt.Println(1 2 3) causes syntax error (missing commas or operators).
    fmt.Println("1" "2" "3") concatenates adjacent strings to "123" without spaces.
    fmt.Println("1", "2", "3") prints 1 2 3 with spaces.
    fmt.Println(1 + 2 + 3) prints 6.
  3. Step 3: Choose the best for printing numbers as strings separated by spaces

    fmt.Println("1", "2", "3") prints the numbers as strings separated by spaces, exactly as requested.
  4. Final Answer:

    fmt.Println("1", "2", "3") -> Option B
  5. Quick Check:

    Separate strings with commas to print spaced values [OK]
Hint: Use commas to separate values for spaced output [OK]
Common Mistakes:
  • Using + to join numbers (adds them instead)
  • Printing a single string instead of separate values
  • Confusing string and integer printing
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