Golang Strings & Runes Quiz

Golang
0 Passed
0% acceptance

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.

35 Questions
~70 minutes
1

Question 1

How are strings encoded in Golang?

A
UTF-8
B
UTF-16
C
ASCII
D
Custom encoding
2

Question 2

What is a rune in Golang?

A
An alias for int32 representing a Unicode code point
B
A single byte
C
A string character
D
A special string type
3

Question 3

What is the difference between len(s) and the number of runes in a string?

go
package main

import "fmt"

func main() {
    s := "Hello, 世界"
    fmt.Println(len(s))  // bytes
    fmt.Println(len([]rune(s)))  // runes
}
A
len(s) gives bytes, rune count gives characters
B
They are always the same
C
len(s) gives characters, rune count gives bytes
D
Depends on the string
4

Question 4

Are strings mutable in Golang?

A
No, strings are immutable
B
Yes, you can modify strings directly
C
Only ASCII strings are mutable
D
Depends on how they're created
5

Question 5

What happens when you try to modify a string?

go
package main

func main() {
    s := "hello"
    s[0] = 'H'  // This fails
}
A
Compilation error - cannot assign to string index
B
String becomes "Hello"
C
Runtime panic
D
Works for ASCII characters
6

Question 6

How do you iterate over a string correctly for Unicode?

go
package main

import "fmt"

func main() {
    s := "Hello, 世界"
    for i, r := range s {
        fmt.Printf("%d: %c\n", i, r)
    }
}
A
Use range - it gives runes and byte positions
B
Use for i := 0; i < len(s); i++
C
Use strings.Split()
D
Cannot iterate Unicode strings
7

Question 7

What does this code print?

go
package main

import "fmt"

func main() {
    s := "café"
    fmt.Println(len(s))
    fmt.Println(len([]rune(s)))
}
A
5\n4
B
4\n4
C
5\n5
D
4\n5
8

Question 8

How do you convert a string to a byte slice?

A
[]byte(s)
B
byte(s)
C
string.ToBytes(s)
D
Cannot convert directly
9

Question 9

How do you convert a byte slice back to string?

go
package main

import "fmt"

func main() {
    b := []byte{'H', 'e', 'l', 'l', 'o'}
    s := string(b)
    fmt.Println(s)
}
A
string(b)
B
bytes.ToString(b)
C
strconv.Atoi(b)
D
Cannot convert back
10

Question 10

What is the strings package used for?

A
String manipulation functions
B
String encoding/decoding
C
String comparison
D
All of the above
11

Question 11

How do you concatenate strings efficiently?

go
package main

import (
    "fmt"
    "strings"
)

func main() {
    var sb strings.Builder
    sb.WriteString("Hello")
    sb.WriteString(" World")
    fmt.Println(sb.String())
}
A
Use strings.Builder for multiple concatenations
B
Use + operator
C
Use fmt.Sprintf
D
Use append() with []string
12

Question 12

In a web application processing user input, you're validating that a string contains only ASCII characters. How should you check this?

A
Iterate with range and check if each rune < 128
B
Check len(s) == len([]rune(s))
C
Use strings.ContainsAny with non-ASCII chars
D
ASCII check is automatic
13

Question 13

What does strings.TrimSpace do?

go
package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "  hello world  \n"
    fmt.Println(strings.TrimSpace(s))
}
A
Removes leading and trailing whitespace
B
Removes all spaces
C
Adds spaces
D
Splits on spaces
14

Question 14

How do you convert an integer to string?

go
package main

import (
    "fmt"
    "strconv"
)

func main() {
    n := 42
    s := strconv.Itoa(n)
    fmt.Println(s)
}
A
strconv.Itoa(n)
B
string(n)
C
fmt.Sprintf("%d", n)
D
All of the above work
15

Question 15

How do you convert a string to integer?

go
package main

import (
    "fmt"
    "strconv"
)

func main() {
    s := "42"
    n, err := strconv.Atoi(s)
    if err == nil {
        fmt.Println(n)
    }
}
A
strconv.Atoi(s) returns int and error
B
int(s)
C
atoi(s)
D
Cannot convert string to int
16

Question 16

What is the zero value of a string?

A
"" (empty string)
B
nil
C
Zero length string
D
Depends on context
17

Question 17

How do you check if a string is empty?

go
package main

import "fmt"

func main() {
    var s string
    if s == "" {
        fmt.Println("Empty")
    }
    if len(s) == 0 {
        fmt.Println("Also empty")
    }
}
A
s == "" or len(s) == 0
B
s == nil
C
s.IsEmpty()
D
Cannot check emptiness
18

Question 18

What does this slicing operation do?

go
package main

import "fmt"

func main() {
    s := "Hello, World"
    sub := s[7:12]
    fmt.Println(sub)
}
A
"World"
B
"Hello"
C
Runtime panic
D
Compilation error
19

Question 19

Why is string slicing dangerous with Unicode?

go
package main

import "fmt"

func main() {
    s := "Hello, 世界"
    sub := s[7:10]  // Middle of multi-byte character
    fmt.Println(sub)
}
A
Can split multi-byte UTF-8 sequences
B
Always safe
C
Strings don't support slicing
D
Only affects ASCII
20

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?

A
Convert to []rune, reverse the slice, convert back
B
Reverse the byte slice directly
C
Use strings.Reverse()
D
Cannot reverse Unicode strings
21

Question 21

What does strings.Contains do?

go
package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "Hello, World"
    fmt.Println(strings.Contains(s, "World"))
}
A
Returns true if substring is found
B
Returns the position
C
Splits the string
D
Replaces substrings
22

Question 22

How do you split a string into substrings?

go
package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "a,b,c"
    parts := strings.Split(s, ",")
    fmt.Println(parts)
}
A
strings.Split(s, sep)
B
s.Split(sep)
C
strings.Fields(s)
D
Cannot split strings
23

Question 23

What is the strings.Join function used for?

go
package main

import (
    "fmt"
    "strings"
)

func main() {
    parts := []string{"a", "b", "c"}
    s := strings.Join(parts, ",")
    fmt.Println(s)
}
A
Concatenates strings with separator
B
Splits strings
C
Reverses strings
D
Finds substrings
24

Question 24

How do you handle case-insensitive string comparison?

go
package main

import (
    "fmt"
    "strings"
)

func main() {
    s1 := "Hello"
    s2 := "HELLO"
    fmt.Println(strings.EqualFold(s1, s2))
}
A
strings.EqualFold(s1, s2)
B
s1 == s2
C
strings.Compare(s1, s2) == 0
D
Cannot do case-insensitive comparison
25

Question 25

What does this code demonstrate about string conversion?

go
package main

import "fmt"

func main() {
    r := '世'
    s := string(r)
    fmt.Printf("Rune: %d, String: %s\n", r, s)
}
A
Converting rune to string gives UTF-8 bytes
B
Rune value is printed
C
Compilation error
D
Runtime panic
26

Question 26

Why are strings immutable in Golang?

A
For efficiency and safety - strings can be shared
B
Historical reason
C
To prevent bugs
D
All of the above
27

Question 27

What is the output of this rune conversion?

go
package main

import "fmt"

func main() {
    s := "Hello"
    runes := []rune(s)
    fmt.Println(len(runes), cap(runes))
}
A
5 5
B
5 0
C
0 5
D
Compilation error
28

Question 28

In an internationalization library, you're implementing text truncation that preserves word boundaries. How should you handle Unicode characters?

A
Work with []rune to ensure character boundaries
B
Truncate at byte level
C
Use strings.Split
D
Unicode doesn't affect truncation
29

Question 29

What does strings.Fields do?

go
package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "  hello   world  \t\n"
    fields := strings.Fields(s)
    fmt.Println(fields)
}
A
Splits on whitespace and removes empty fields
B
Splits on spaces only
C
Joins fields
D
Counts fields
30

Question 30

How do you replace substrings in a string?

go
package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "Hello World"
    newS := strings.ReplaceAll(s, "World", "Golang")
    fmt.Println(newS)
}
A
strings.ReplaceAll(s, old, new)
B
s.Replace(old, new)
C
strings.Replace(s, old, new, -1)
D
Cannot replace in immutable strings
31

Question 31

What is the strconv package used for?

A
Converting between strings and other types
B
String manipulation
C
String encoding
D
String comparison
32

Question 32

How do you convert a float to string?

go
package main

import (
    "fmt"
    "strconv"
)

func main() {
    f := 3.14159
    s := strconv.FormatFloat(f, 'f', 2, 64)
    fmt.Println(s)
}
A
strconv.FormatFloat(f, format, prec, bitSize)
B
string(f)
C
fmt.Sprintf("%f", f)
D
All work
33

Question 33

What happens when you convert invalid UTF-8 bytes to string?

go
package main

import "fmt"

func main() {
    b := []byte{0xff, 0xfe, 0xfd}
    s := string(b)
    fmt.Println(s)  // Shows replacement chars
}
A
Invalid bytes become replacement characters (�)
B
Runtime panic
C
Compilation error
D
Bytes are ignored
34

Question 34

In a logging system, you're building log messages from multiple parts. Which approach should you use for efficiency?

A
strings.Builder for multiple concatenations
B
Multiple + operations
C
fmt.Sprintf for everything
D
[]string and strings.Join
35

Question 35

What is the most important principle for handling Unicode text in Golang?

A
Use runes for character operations, bytes for binary data
B
Always use ASCII
C
Avoid Unicode entirely
D
Use strings for everything

QUIZZES IN Golang