Strings & Runes Questions with Answers

8 practice questions on Strings & Runes (Go) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Easy

What is printed?

s := "Go"
fmt.Println(s[0] == 'G')
  1. false
  2. 71
  3. Compile error
  4. true

Answer: D. true

Explanation

s[0] is a single byte, and the character literal 'G' is an untyped constant that converts to byte. The comparison is valid and is true.

Step-by-step solution

  1. s[0] reads the first byte of "Go", which is the byte value of 'G'.
  2. 'G' is an untyped rune constant, so Go converts it to byte for the comparison.
  3. Both sides hold the same byte, so the result is true.

Why the other options are wrong

  • A. The first byte is 'G', so the comparison is true.
  • B. 71 is the byte's numeric value; the comparison returns a bool, not a number.
  • C. Comparing a byte with a character literal compiles fine.

Tip: Indexing a Go string gives bytes, and character literals compare with them directly.

Question 2Easy

What is printed?

fmt.Println("abc"[1])
  1. b
  2. 98
  3. 1
  4. Compile error

Answer: B. 98

Explanation

Indexing a string gives a byte, not a string. The byte for b is 98 and Println prints it as a number.

Step-by-step solution

  1. "abc"[1] is the second byte.
  2. Its type is byte (uint8).
  3. Println shows 98.

Why the other options are wrong

  • A. Printing a byte shows its number, not the letter.
  • C. 1 is the index, not the value.
  • D. String indexing is legal.

Tip: Use string(s[1]) or %c to print the letter.

Question 3Medium

What is printed?

s := "héllo"
fmt.Println(len(s))
  1. 6
  2. 5
  3. 7
  4. 4

Answer: A. 6

Explanation

len of a string counts bytes. In UTF-8 the letter é takes two bytes, so the string has 6 bytes but 5 runes.

Step-by-step solution

  1. h = 1 byte, é = 2 bytes.
  2. l, l, o = 3 bytes.
  3. Total 6 bytes.

Why the other options are wrong

  • B. 5 is len([]rune(s)), the rune count.
  • C. No character here takes 3 bytes.
  • D. That undercounts the bytes.

Tip: Use utf8.RuneCountInString for characters.

Question 4Medium

What is printed?

t := "cat"
bb := []byte(t)
bb[0] = 'b'
fmt.Println(t, string(bb))
  1. bat bat
  2. cat cat
  3. Compile error
  4. cat bat

Answer: D. cat bat

Explanation

[]byte(t) makes a new slice holding a copy of the bytes. Changing the copy does not change the string t, because strings are immutable.

Step-by-step solution

  1. []byte(t) copies the bytes of "cat" into a new slice.
  2. bb[0] = 'b' changes only that copy.
  3. t stays "cat", while string(bb) is "bat".

Why the other options are wrong

  • A. t is not changed; only the byte slice was edited.
  • B. string(bb) is "bat", so the second value is not "cat".
  • C. Assigning a byte to a slice element is valid, so this compiles.

Tip: Converting between string and []byte copies the data.

Question 5Medium

What is printed?

r := []rune("año")
fmt.Println(len(r), len("año"))
  1. 3 3
  2. 4 3
  3. 3 4
  4. 4 4

Answer: C. 3 4

Explanation

[]rune converts the text to one element per character: a, ñ and o, which is 3. len on a string counts bytes, and ñ takes 2 bytes in UTF-8, so the string has 4 bytes.

Step-by-step solution

  1. []rune("año") splits the text into characters: a, ñ, o.
  2. That gives a slice of 3 runes.
  3. len("año") counts UTF-8 bytes: a = 1, ñ = 2, o = 1, total 4.

Why the other options are wrong

  • A. The byte count of the string is 4, not 3.
  • B. The two values are swapped: the rune slice has 3 elements and the string has 4 bytes.
  • D. The rune slice has 3 elements, not 4.

Tip: len on a string counts bytes; len on a []rune counts characters.

Question 6Hard

What is printed?

s2 := "héllo"
fmt.Println(s2[1:3] == "é", len(s2[1:2]))
  1. true 2
  2. false 1
  3. true 1
  4. false 2

Answer: C. true 1

Explanation

é takes two bytes, at positions 1 and 2, so s2[1:3] is exactly "é" and the comparison is true. s2[1:2] is only the first byte of é, so its length is 1.

Step-by-step solution

  1. In "héllo", h is byte 0 and é is bytes 1 and 2.
  2. s2[1:3] takes bytes 1 and 2, which is exactly "é", so the comparison is true.
  3. s2[1:2] takes only byte 1, half of é, so len is 1.

Why the other options are wrong

  • A. A slice of length 1 is one byte, not two.
  • B. s2[1:3] is exactly é, so the first value is true.
  • D. Both values are wrong: the comparison is true and the length is 1.

Tip: Go slices strings by byte, so index maths on accented letters can split a character.

Question 7Hard

What is printed?

for _, r := range "\xff" {
    fmt.Println(r)
}
  1. 65533
  2. 255
  3. -1
  4. 0

Answer: A. 65533

Explanation

The byte 0xFF cannot start a valid UTF-8 sequence, so range decodes it as the replacement character U+FFFD. As a rune value, U+FFFD is 65533.

Step-by-step solution

  1. "\xff" is one byte, 0xFF, which is not valid UTF-8 on its own.
  2. range over a string decodes UTF-8, and an invalid byte becomes U+FFFD.
  3. U+FFFD is the rune value 65533.

Why the other options are wrong

  • B. range does not return the raw byte; it decodes the string as UTF-8.
  • C. Runes are never negative, so -1 cannot appear here.
  • D. 0 is the NUL character, not the replacement character.

Tip: Invalid UTF-8 shows up as U+FFFD (65533) when you range over a string.

Question 8Hard

What is printed?

for i := range "aé!" {
    fmt.Print(i, " ")
}
  1. 0 1 2
  2. 0 2 3
  3. 1 2 3
  4. 0 1 3

Answer: D. 0 1 3

Explanation

Ranging over a string yields the byte index of each rune. a starts at 0, é at 1 (two bytes), and ! at 3.

Step-by-step solution

  1. a is one byte at index 0.
  2. é begins at 1 and uses bytes 1 and 2.
  3. ! begins at index 3.

Why the other options are wrong

  • A. Indexes advance by the byte length of each rune.
  • B. Indexes start at 0, and é starts at 1.
  • C. The first index is 0.

Practise all 8 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Go topics · All topics