Bird
Raised Fist0
Goprogramming~20 mins

Basic input concepts in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Go Input 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 program reading input?

Consider this Go program that reads a single word from the user and prints it back:

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	input, _ := reader.ReadString('\n')
	fmt.Print(input)
}

If the user types hello and presses Enter, what will be printed?

Go
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	input, _ := reader.ReadString('\n')
	fmt.Print(input)
}
Ahello\r\n
Bhello
Chello\n
DError: input not read
Attempts:
2 left
๐Ÿ’ก Hint

Think about what ReadString('\n') includes in the returned string.

โ“ Predict Output
intermediate
2:00remaining
What happens if you use fmt.Scanln to read input?

Look at this Go code snippet:

package main

import "fmt"

func main() {
	var name string
	fmt.Print("Enter your name: ")
	fmt.Scanln(&name)
	fmt.Println("Hello,", name)
}

If the user types John Doe and presses Enter, what will be printed?

Go
package main

import "fmt"

func main() {
	var name string
	fmt.Print("Enter your name: ")
	fmt.Scanln(&name)
	fmt.Println("Hello,", name)
}
AHello, John Doe
BHello, John
CHello,
DRuntime error
Attempts:
2 left
๐Ÿ’ก Hint

Remember how Scanln reads input and stops at spaces.

โ“ Predict Output
advanced
2:00remaining
What is the output when reading multiple values with fmt.Scan?

Consider this Go program:

package main

import "fmt"

func main() {
	var a, b int
	fmt.Print("Enter two numbers: ")
	fmt.Scan(&a, &b)
	fmt.Println(a + b)
}

If the user inputs 3 5 and presses Enter, what will be printed?

Go
package main

import "fmt"

func main() {
	var a, b int
	fmt.Print("Enter two numbers: ")
	fmt.Scan(&a, &b)
	fmt.Println(a + b)
}
A8
B35
C0
DRuntime error
Attempts:
2 left
๐Ÿ’ก Hint

Think about how fmt.Scan parses multiple inputs separated by spaces.

โ“ Predict Output
advanced
2:00remaining
What error occurs when scanning input into wrong variable types?

Look at this Go code:

package main

import "fmt"

func main() {
	var x int
	fmt.Print("Enter a number: ")
	_, err := fmt.Scanf("%d", &x)
	if err != nil {
		fmt.Println("Error:", err)
	} else {
		fmt.Println("You entered", x)
	}
}

If the user types hello and presses Enter, what will be printed?

Go
package main

import "fmt"

func main() {
	var x int
	fmt.Print("Enter a number: ")
	_, err := fmt.Scanf("%d", &x)
	if err != nil {
		fmt.Println("Error:", err)
	} else {
		fmt.Println("You entered", x)
	}
}
AProgram crashes
BYou entered 0
CError: expected integer input
DError: input mismatch
Attempts:
2 left
๐Ÿ’ก Hint

What happens if you try to scan a string into an integer variable?

๐Ÿง  Conceptual
expert
2:00remaining
How does bufio.Reader differ from fmt.Scanln for input reading?

Which statement best describes the difference between using bufio.Reader.ReadString('\n') and fmt.Scanln for reading input in Go?

A<code>bufio.Reader.ReadString('\n')</code> reads the entire line including spaces and newline, while <code>fmt.Scanln</code> reads input only up to the first space or newline.
B<code>fmt.Scanln</code> reads the entire line including spaces and newline, while <code>bufio.Reader.ReadString('\n')</code> reads input only up to the first space.
CBoth read input until the first space and exclude the newline character.
D<code>bufio.Reader.ReadString('\n')</code> reads input word by word, while <code>fmt.Scanln</code> reads the entire line.
Attempts:
2 left
๐Ÿ’ก Hint

Think about how each method treats spaces and newlines in input.

Practice

(1/5)
1. What is the main purpose of using input functions in Go programs?
easy
A. To get information from the user during program execution
B. To print messages on the screen
C. To declare variables
D. To create new functions

Solution

  1. Step 1: Understand input function role

    Input functions allow the program to receive data from the user while it runs.
  2. Step 2: Differentiate input from output

    Printing messages is output, not input. Declaring variables or creating functions are unrelated.
  3. Final Answer:

    To get information from the user during program execution -> Option A
  4. Quick Check:

    Input = get user data [OK]
Hint: Input means getting data from user, not showing it [OK]
Common Mistakes:
  • Confusing input with output (printing)
  • Thinking input declares variables
  • Mixing input with function creation
2. Which of the following is the correct way to read a single word input from the user in Go?
easy
A. fmt.Println(variable)
B. fmt.Scanln(&variable)
C. bufio.NewReader(os.Stdin).ReadString()
D. var input string

Solution

  1. Step 1: Identify input reading functions

    fmt.Scanln reads a single word or token from user input into a variable.
  2. Step 2: Eliminate other options

    fmt.Println prints output, bufio.NewReader reads full lines, and declaring a variable alone doesn't read input.
  3. Final Answer:

    fmt.Scanln(&variable) -> Option B
  4. Quick Check:

    Single word input uses fmt.Scanln [OK]
Hint: Use fmt.Scanln for simple word input [OK]
Common Mistakes:
  • Using fmt.Println to read input
  • Confusing bufio.NewReader with Scanln for single words
  • Forgetting to pass variable address with &
3. What will be the output of this Go code snippet?
var name string
fmt.Print("Enter name: ")
fmt.Scanln(&name)
fmt.Println("Hello,", name)
If the user types: John and presses Enter.
medium
A. Hello,
B. Error: missing input
C. Enter name: John
D. Hello, John

Solution

  1. Step 1: Understand input reading

    fmt.Scanln reads the word 'John' into variable name.
  2. Step 2: Understand output printing

    fmt.Println prints "Hello," followed by the value of name, which is "John".
  3. Final Answer:

    Hello, John -> Option D
  4. Quick Check:

    Input 'John' prints 'Hello, John' [OK]
Hint: Scanln reads input; println shows it back [OK]
Common Mistakes:
  • Expecting input prompt to print again
  • Thinking Scanln reads empty string
  • Confusing output with input prompt
4. What is wrong with this Go code for reading a full line input?
var input string
reader := bufio.NewReader(os.Stdin)
input, _ = reader.ReadString('\n')
fmt.Println(input)
medium
A. It does not import the bufio and os packages
B. ReadString does not read until newline
C. input variable is declared twice causing error
D. fmt.Println cannot print strings

Solution

  1. Step 1: Check package imports

    bufio and os packages must be imported to use bufio.NewReader and os.Stdin.
  2. Step 2: Verify variable declaration

    input is declared once and assigned, which is correct. ReadString reads until newline correctly. fmt.Println can print strings.
  3. Final Answer:

    It does not import the bufio and os packages -> Option A
  4. Quick Check:

    Missing imports cause errors [OK]
Hint: Always import bufio and os for buffered input [OK]
Common Mistakes:
  • Forgetting to import required packages
  • Redeclaring variables with := after declaration
  • Assuming ReadString reads partial input
5. You want to read a user's full name including spaces in Go. Which approach correctly reads and cleans the input to remove the trailing newline?
hard
A. Use fmt.Println to read input
B. Use fmt.Scanln(&name) only
C. Use bufio.NewReader(os.Stdin).ReadString('\n') and strings.TrimSpace()
D. Use fmt.Scanf("%s", &name)

Solution

  1. Step 1: Identify method for full line input

    bufio.NewReader with ReadString('\n') reads the entire line including spaces.
  2. Step 2: Remove trailing newline

    strings.TrimSpace removes extra spaces and newline characters from input.
  3. Final Answer:

    Use bufio.NewReader(os.Stdin).ReadString('\n') and strings.TrimSpace() -> Option C
  4. Quick Check:

    Full line input + trim newline = Use bufio.NewReader(os.Stdin).ReadString('\n') and strings.TrimSpace() [OK]
Hint: Use bufio + TrimSpace for full line input [OK]
Common Mistakes:
  • Using Scanln which stops at space
  • Using Scanf with %s reads only one word
  • Trying to read input with Println
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