Challenge - 5 Problems
Go fmt.Print Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
What is the output of this Go code using fmt.Print?
Look at the code below. What will it print when run?
Go
package main import "fmt" func main() { fmt.Print("Hello") fmt.Print(" ") fmt.Print("World") }
Attempts:
2 left
๐ก Hint
fmt.Print prints exactly what you give it without adding new lines.
โ Incorrect
fmt.Print prints the strings one after another without adding spaces or new lines automatically. So the output is exactly "Hello World" with no new line.
โ Predict Output
intermediate2:00remaining
What does this Go code print with fmt.Print and fmt.Println?
Check the output of this program:
Go
package main import "fmt" func main() { fmt.Print("Go") fmt.Println("Lang") fmt.Print("!\n") }
Attempts:
2 left
๐ก Hint
fmt.Println adds a new line after printing, fmt.Print does not.
โ Incorrect
fmt.Print("Go") prints Go with no new line. fmt.Println("Lang") prints Lang and adds a new line. fmt.Print("!\n") prints ! and a new line. So output is GoLang\n!\n
โ Predict Output
advanced2:00remaining
What is the output of this Go code using fmt.Print with variables?
What will this program print?
Go
package main import "fmt" func main() { a := 5 b := 10 fmt.Print("Sum: ", a+b) }
Attempts:
2 left
๐ก Hint
The + operator adds numbers, not strings, when used with integers.
โ Incorrect
a and b are integers 5 and 10. a+b is 15. fmt.Print prints "Sum: " then 15, so output is "Sum: 15".
โ Predict Output
advanced2:00remaining
What error does this Go code produce?
What happens when you run this code?
Go
package main import "fmt" func main() { fmt.Print("Number: " + 5) }
Attempts:
2 left
๐ก Hint
You cannot add a string and an int directly in Go.
โ Incorrect
In Go, you cannot use + to add a string and an int directly. This causes a compile error about mismatched types.
๐ง Conceptual
expert2:00remaining
How many characters are printed by this Go program using fmt.Print?
Count the total characters printed by this program:
Go
package main import "fmt" func main() { fmt.Print("Go") fmt.Print(" ") fmt.Print(123) }
Attempts:
2 left
๐ก Hint
Remember numbers printed by fmt.Print are converted to their string form.
โ Incorrect
fmt.Print("Go") prints 2 chars, fmt.Print(" ") prints 1 char, fmt.Print(123) prints 3 chars (digits). Total 2+1+3=6 characters.