0
0
Goprogramming~20 mins

Basic input concepts in Go - Practice Problems & Coding Challenges

Choose your learning style9 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.