Sample questions
Question 1Easy
What is printed?
s := "Go"
fmt.Println(s[0] == 'G')
- false
- 71
- Compile error
- 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
- s[0] reads the first byte of "Go", which is the byte value of 'G'.
- 'G' is an untyped rune constant, so Go converts it to byte for the comparison.
- 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])
- b
- 98
- 1
- 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
"abc"[1] is the second byte.- Its type is
byte (uint8). - 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))
- 6
- 5
- 7
- 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
- h = 1 byte, é = 2 bytes.
- l, l, o = 3 bytes.
- 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))
- bat bat
- cat cat
- Compile error
- 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
- []byte(t) copies the bytes of "cat" into a new slice.
- bb[0] = 'b' changes only that copy.
- 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"))
- 3 3
- 4 3
- 3 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
- []rune("año") splits the text into characters: a, ñ, o.
- That gives a slice of 3 runes.
- 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]))
- true 2
- false 1
- true 1
- 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
- In "héllo", h is byte 0 and é is bytes 1 and 2.
- s2[1:3] takes bytes 1 and 2, which is exactly "é", so the comparison is true.
- 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)
}
- 65533
- 255
- -1
- 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
- "\xff" is one byte, 0xFF, which is not valid UTF-8 on its own.
- range over a string decodes UTF-8, and an invalid byte becomes U+FFFD.
- 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, " ")
}
- 0 1 2
- 0 2 3
- 1 2 3
- 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
a is one byte at index 0.é begins at 1 and uses bytes 1 and 2.! 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 →