Sample questions
Question 1Easy
What is printed?
var n int
var s string
var b bool
fmt.Println(n, s == "", b)
- 0 false false
- nil true nil
- 0 true false
- <nil> true false
Answer: C. 0 true false
Explanation
Go gives every variable a zero value: 0 for numbers, empty string for strings and false for booleans.
Step-by-step solution
n is 0.s is empty, so the comparison is true.b is false.
Why the other options are wrong
- A. The empty string equals "", so the middle value is true.
- B. Basic types never hold nil.
- D. Basic types never hold nil, so no <nil> appears.
Question 2Easy
What is the output?
package main
import "fmt"
func main() {
var i int
var b bool
var p *int
fmt.Println(i, b, p == nil)
}
- nil nil true
- 0 false false
- 0 false true
- Compile error
Answer: C. 0 false true
Explanation
Zero values: 0 for int, false for bool, nil for pointers.
Step-by-step solution
- Variables i, b, p are declared without initialization.
- In Go, an uninitialized int gets zero value 0.
- An uninitialized bool gets zero value false.
- An uninitialized pointer defaults to nil, so p == nil is true.
- 0 false true.
Why the other options are wrong
- A. An uninitialized int is 0, not nil; only pointers, maps, slices and channels default to nil.
- B. The comparison p == nil is true for a nil pointer, so the last value prints true, not false.
- D. Compile error is wrong: i, b and p are all used in Println, so the program compiles and runs.
Tip: Remember Go’s default zero values: 0, false, nil for int, bool, pointer.
Question 3Easy
What is the zero value of a string variable in Go?
- "0"
- "" (empty string)
- nil
- Undefined; it must be initialised before use
Answer: B. "" (empty string)
Explanation
Every Go type has a zero value; for strings it is the empty string, not nil.
Step-by-step solution
- Go gives every variable a zero value.
- The zero value for strings is the empty string.
- There is no undefined.
Why the other options are wrong
- A. 0 is for numbers.
- C. Strings cannot be nil.
- D. Go initialises automatically.
Tip: var zero string gives the empty string "".
Question 4Easy
- Mutable arrays of characters
- Always UTF-16 encoded
- Immutable byte sequences
- Pointers to null-terminated char arrays
Answer: C. Immutable byte sequences
Explanation
You cannot change s[0]; build new strings or use []byte or strings.Builder.
Step-by-step solution
- Strings in Go are sequences of bytes, not characters.
- You cannot modify a string directly; it is immutable.
- To change a string, convert it to a byte slice using []byte.
- String operations create new strings instead of modifying existing ones.
- Thus, strings in Go are immutable byte sequences.
Why the other options are wrong
- A. Go strings are immutable, so they are not mutable arrays of characters as this option says.
- B. Go strings are byte sequences, not UTF-16; UTF-8 is the usual encoding.
- D. Go strings are not null-terminated char pointers; they store a length alongside the bytes.
Tip: Remember: strings in Go cannot be changed directly; they are immutable.
Question 5Medium
What is the output?
package main
import "fmt"
func main() {
x := 10
unused := 5
fmt.Println(x)
}
- 10
- Runtime panic
- 10 5
- Compile error
Answer: D. Compile error
Explanation
Go refuses to compile code with unused local variables ('declared and not used').
Step-by-step solution
- Variable x is assigned the value 10.
- Variable unused is assigned the value 5 but never referenced.
- Go compiler checks that every local variable is used.
- It finds an unused variable and aborts compilation.
- Compile error.
Why the other options are wrong
- A. Go rejects an unused local variable at compile time, so the program never runs and prints nothing.
- B. There is no runtime panic; the unused variable is a compile-time error, not a runtime one.
- C. Println is passed only x, so the value 5 is never printed, even if it compiled.
Tip: Use the variable or remove its declaration to avoid a compile error.
Question 6Medium
What is the output?
package main
import "fmt"
func main() {
var a int32 = 5
var b int64 = 10
fmt.Println(a + b)
}
- 5
- Compile error
- Runtime panic
- 15
Answer: B. Compile error
Explanation
Go never converts between numeric types implicitly; you must write int64(a) + b.
Step-by-step solution
- A is int32, b is int64, adding them directly causes a type mismatch.
- Go does not allow implicit conversion between different integer types.
- The code will fail to compile because of the type mismatch between int32 and int64.
- No runtime panic occurs because the error is caught at compile time.
- The correct output is a compile error.
Why the other options are wrong
- A. Mixing int32 and int64 does not print 5; Go refuses the addition before the program runs.
- C. No runtime panic happens, because Go rejects this mismatch at compile time, not during execution.
- D. 15 would need an implicit conversion, which Go never does between int32 and int64.
Tip: Always convert types explicitly when performing operations on different numeric types in Go.
Question 7Medium
What is the output?
package main
import "fmt"
func main() {
fmt.Println(7/2, 7.0/2, -7/2)
}
- 4 3.5 -3
- 3 3.5 -4
- 3.5 3.5 -3.5
- 3 3.5 -3
Answer: D. 3 3.5 -3
Explanation
Integer division truncates toward zero, so -7/2 is -3; 7.0/2 is floating-point.
Step-by-step solution
- 7/2 uses integer division: 3.
- 7.0/2 is floating-point: 3.5.
- -7/2 truncates toward zero: -3.
Why the other options are wrong
- A. Integer division truncates, not rounds.
- B. -7/2 is -3, not -4.
- C. Integer ops do not give fractions.
Tip: Go truncates toward zero.
Question 8Medium
What is the output?
package main
import "fmt"
func main() {
s := "héllo"
fmt.Println(len(s), len([]rune(s)))
}
- 5 6
- 6 5
- 6 6
- 5 5
Answer: B. 6 5
Explanation
len on a string counts bytes; é is 2 bytes in UTF-8, so 6 bytes but 5 runes.
Step-by-step solution
- The string 'héllo' has 6 bytes because 'é' uses 2 bytes in UTF-8.
- len(s) counts bytes, so len(s) = 6.
- []rune(s) converts the string to runes, which are characters.
- len([]rune(s)) counts runes, so len([]rune(s)) = 5.
- Final output is 6 5.
Why the other options are wrong
- A. len(s) counts bytes, not characters, so the first value is 6, not 5.
- C. Counting runes gives 5, not 6, because é is one rune, not two.
- D. The byte length is 6, not 5, because é takes two bytes in UTF-8.
Tip: Remember: len(string) = byte count, len(rune slice) = character count.
Question 9Hard
What is the output?
package main
import "fmt"
func main() {
x := 1
if true {
x := 2
x++
}
fmt.Println(x)
}
- 1
- 2
- 3
- Compile error
Answer: A. 1
Explanation
The inner := declares a new x that shadows the outer one; the outer x stays 1.
Step-by-step solution
- The outer x is 1.
- Inside the if block, x := 2 creates a new x that shadows the outer one.
- X++ changes only the inner x, which is gone after the block.
- fmt.Println(x) prints the outer x, which is 1 (option A).
Why the other options are wrong
- B. 2 is the inner x, which is out of scope after the block.
- C. The outer x is never changed, so 3 is wrong.
- D. The code compiles and runs, so there is no compile error.
Tip: Using := inside a block creates a new variable that hides the outer one.
Question 10Hard
What is the output?
package main
import "fmt"
func main() {
for i, r := range "aé" {
fmt.Print(i, ":", string(r), " ")
}
fmt.Println()
}
- 1:a 2:é
- 0:a 2:é
- 0:a 1:é 2:é
- 0:a 1:é
Answer: D. 0:a 1:é
Explanation
Ranging over a string yields byte offsets and runes; 'a' is at 0 and 'é' starts at byte 1.
Step-by-step solution
- A range loop over a string gives the byte index i and the rune r.
- The letter a is 1 byte at index 0; é takes 2 bytes, so it is at index 1.
- Print adds the text as written, so the output is 0:a 1:é, which is option D.
Why the other options are wrong
- A. The index of a is 0, not 1.
- B. The index of é is 1, not 2, because a takes only one byte.
- C. A range loop gives two runes here, so there is no third value.
Tip: Range over a string gives byte offsets, not character counts.
Practise all 19 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 →