Types & Basics Questions with Answers

19 practice questions on Types & Basics (Go) with answers and step-by-step solutions. Try 10 samples below, then practise all 19 free in the AptiNest app.

Sample questions

Question 1Easy

What is printed?

var n int
var s string
var b bool
fmt.Println(n, s == "", b)
  1. 0 false false
  2. nil true nil
  3. 0 true false
  4. <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

  1. n is 0.
  2. s is empty, so the comparison is true.
  3. 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)
}
  1. nil nil true
  2. 0 false false
  3. 0 false true
  4. Compile error

Answer: C. 0 false true

Explanation

Zero values: 0 for int, false for bool, nil for pointers.

Step-by-step solution

  1. Variables i, b, p are declared without initialization.
  2. In Go, an uninitialized int gets zero value 0.
  3. An uninitialized bool gets zero value false.
  4. An uninitialized pointer defaults to nil, so p == nil is true.
  5. 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?

  1. "0"
  2. "" (empty string)
  3. nil
  4. 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

  1. Go gives every variable a zero value.
  2. The zero value for strings is the empty string.
  3. 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

Strings in Go are:

  1. Mutable arrays of characters
  2. Always UTF-16 encoded
  3. Immutable byte sequences
  4. 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

  1. Strings in Go are sequences of bytes, not characters.
  2. You cannot modify a string directly; it is immutable.
  3. To change a string, convert it to a byte slice using []byte.
  4. String operations create new strings instead of modifying existing ones.
  5. 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)
}
  1. 10
  2. Runtime panic
  3. 10 5
  4. Compile error

Answer: D. Compile error

Explanation

Go refuses to compile code with unused local variables ('declared and not used').

Step-by-step solution

  1. Variable x is assigned the value 10.
  2. Variable unused is assigned the value 5 but never referenced.
  3. Go compiler checks that every local variable is used.
  4. It finds an unused variable and aborts compilation.
  5. 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)
}
  1. 5
  2. Compile error
  3. Runtime panic
  4. 15

Answer: B. Compile error

Explanation

Go never converts between numeric types implicitly; you must write int64(a) + b.

Step-by-step solution

  1. A is int32, b is int64, adding them directly causes a type mismatch.
  2. Go does not allow implicit conversion between different integer types.
  3. The code will fail to compile because of the type mismatch between int32 and int64.
  4. No runtime panic occurs because the error is caught at compile time.
  5. 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)
}
  1. 4 3.5 -3
  2. 3 3.5 -4
  3. 3.5 3.5 -3.5
  4. 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

  1. 7/2 uses integer division: 3.
  2. 7.0/2 is floating-point: 3.5.
  3. -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)))
}
  1. 5 6
  2. 6 5
  3. 6 6
  4. 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

  1. The string 'héllo' has 6 bytes because 'é' uses 2 bytes in UTF-8.
  2. len(s) counts bytes, so len(s) = 6.
  3. []rune(s) converts the string to runes, which are characters.
  4. len([]rune(s)) counts runes, so len([]rune(s)) = 5.
  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. 1
  2. 2
  3. 3
  4. 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

  1. The outer x is 1.
  2. Inside the if block, x := 2 creates a new x that shadows the outer one.
  3. X++ changes only the inner x, which is gone after the block.
  4. 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. 1:a 2:é
  2. 0:a 2:é
  3. 0:a 1:é 2:é
  4. 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

  1. A range loop over a string gives the byte index i and the rune r.
  2. The letter a is 1 byte at index 0; é takes 2 bytes, so it is at index 1.
  3. 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 →

Related topics

See all Go topics · All topics