Golang Strings & Runes Quiz
35 comprehensive questions on Golang strings and runes, covering UTF-8 basics, rune vs byte differences, string immutability, manipulation techniques, and conversion operations — with 18 code examples demonstrating string handling patterns.
Question 1
How are strings encoded in Golang?
Question 2
What is a rune in Golang?
Question 3
What is the difference between len(s) and the number of runes in a string?
package main
import "fmt"
func main() {
s := "Hello, 世界"
fmt.Println(len(s)) // bytes
fmt.Println(len([]rune(s))) // runes
}Question 4
Are strings mutable in Golang?
Question 5
What happens when you try to modify a string?
package main
func main() {
s := "hello"
s[0] = 'H' // This fails
}Question 6
How do you iterate over a string correctly for Unicode?
package main
import "fmt"
func main() {
s := "Hello, 世界"
for i, r := range s {
fmt.Printf("%d: %c\n", i, r)
}
}Question 7
What does this code print?
package main
import "fmt"
func main() {
s := "café"
fmt.Println(len(s))
fmt.Println(len([]rune(s)))
}Question 8
How do you convert a string to a byte slice?
Question 9
How do you convert a byte slice back to string?
package main
import "fmt"
func main() {
b := []byte{'H', 'e', 'l', 'l', 'o'}
s := string(b)
fmt.Println(s)
}Question 10
What is the strings package used for?
Question 11
How do you concatenate strings efficiently?
package main
import (
"fmt"
"strings"
)
func main() {
var sb strings.Builder
sb.WriteString("Hello")
sb.WriteString(" World")
fmt.Println(sb.String())
}Question 12
In a web application processing user input, you're validating that a string contains only ASCII characters. How should you check this?
Question 13
What does strings.TrimSpace do?
package main
import (
"fmt"
"strings"
)
func main() {
s := " hello world \n"
fmt.Println(strings.TrimSpace(s))
}Question 14
How do you convert an integer to string?
package main
import (
"fmt"
"strconv"
)
func main() {
n := 42
s := strconv.Itoa(n)
fmt.Println(s)
}Question 15
How do you convert a string to integer?
package main
import (
"fmt"
"strconv"
)
func main() {
s := "42"
n, err := strconv.Atoi(s)
if err == nil {
fmt.Println(n)
}
}Question 16
What is the zero value of a string?
Question 17
How do you check if a string is empty?
package main
import "fmt"
func main() {
var s string
if s == "" {
fmt.Println("Empty")
}
if len(s) == 0 {
fmt.Println("Also empty")
}
}Question 18
What does this slicing operation do?
package main
import "fmt"
func main() {
s := "Hello, World"
sub := s[7:12]
fmt.Println(sub)
}Question 19
Why is string slicing dangerous with Unicode?
package main
import "fmt"
func main() {
s := "Hello, 世界"
sub := s[7:10] // Middle of multi-byte character
fmt.Println(sub)
}Question 20
In a text processing application, you're implementing a function to reverse a string while preserving Unicode characters. How should you approach this?
Question 21
What does strings.Contains do?
package main
import (
"fmt"
"strings"
)
func main() {
s := "Hello, World"
fmt.Println(strings.Contains(s, "World"))
}Question 22
How do you split a string into substrings?
package main
import (
"fmt"
"strings"
)
func main() {
s := "a,b,c"
parts := strings.Split(s, ",")
fmt.Println(parts)
}Question 23
What is the strings.Join function used for?
package main
import (
"fmt"
"strings"
)
func main() {
parts := []string{"a", "b", "c"}
s := strings.Join(parts, ",")
fmt.Println(s)
}Question 24
How do you handle case-insensitive string comparison?
package main
import (
"fmt"
"strings"
)
func main() {
s1 := "Hello"
s2 := "HELLO"
fmt.Println(strings.EqualFold(s1, s2))
}Question 25
What does this code demonstrate about string conversion?
package main
import "fmt"
func main() {
r := '世'
s := string(r)
fmt.Printf("Rune: %d, String: %s\n", r, s)
}Question 26
Why are strings immutable in Golang?
Question 27
What is the output of this rune conversion?
package main
import "fmt"
func main() {
s := "Hello"
runes := []rune(s)
fmt.Println(len(runes), cap(runes))
}Question 28
In an internationalization library, you're implementing text truncation that preserves word boundaries. How should you handle Unicode characters?
Question 29
What does strings.Fields do?
package main
import (
"fmt"
"strings"
)
func main() {
s := " hello world \t\n"
fields := strings.Fields(s)
fmt.Println(fields)
}Question 30
How do you replace substrings in a string?
package main
import (
"fmt"
"strings"
)
func main() {
s := "Hello World"
newS := strings.ReplaceAll(s, "World", "Golang")
fmt.Println(newS)
}Question 31
What is the strconv package used for?
Question 32
How do you convert a float to string?
package main
import (
"fmt"
"strconv"
)
func main() {
f := 3.14159
s := strconv.FormatFloat(f, 'f', 2, 64)
fmt.Println(s)
}Question 33
What happens when you convert invalid UTF-8 bytes to string?
package main
import "fmt"
func main() {
b := []byte{0xff, 0xfe, 0xfd}
s := string(b)
fmt.Println(s) // Shows replacement chars
}Question 34
In a logging system, you're building log messages from multiple parts. Which approach should you use for efficiency?
Question 35
What is the most important principle for handling Unicode text in Golang?
