Interfaces Questions with Answers

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

Sample questions

Question 1Easy

In current Go, what is any?

  1. A keyword for generic classes
  2. An alias for interface{}
  3. A pointer to any struct
  4. A function type

Answer: B. An alias for interface{}

Explanation

any is a predeclared alias for the empty interface interface{}, which every type satisfies.

Step-by-step solution

  1. Every type has at least zero methods.
  2. So it satisfies the empty interface.
  3. any is the shorter name.

Why the other options are wrong

  • A. It is an identifier, not a class keyword.
  • C. It is not a pointer.
  • D. It is not a function type.

Question 2Easy

What is 'any' in Go?

  1. A function that accepts all arguments
  2. A keyword usable only as a generic type parameter, never as an ordinary variable type
  3. A special type for numbers
  4. An alias for interface{}, satisfied by every type

Answer: D. An alias for interface{}, satisfied by every type

Explanation

Every type has at least zero methods, so it satisfies the empty interface.

Step-by-step solution

  1. 'any' is an alias for interface{} in Go.
  2. Interface{} can hold any type because every type satisfies it.
  3. 'any' is used as a generic type parameter to accept any type.
  4. The correct answer is 'An alias for interface{}, satisfied by every type'.
  5. Final answer: An alias for interface{}, satisfied by every type.

Why the other options are wrong

  • A. any is not a function; it is a type name, so it takes no arguments.
  • B. any can be used as an ordinary variable type too, for example var x any.
  • C. any is not a number type; it holds values of any type, including numbers.

Tip: Remember 'any' is like a wildcard for types in Go generics.

Question 3Easy

Which is the built-in error interface?

  1. type error struct { msg string }
  2. type error interface { Error() string }
  3. type error interface { String() string }
  4. type error int

Answer: B. type error interface { Error() string }

Explanation

Any type with an Error() string method is an error.

Step-by-step solution

  1. error is a built-in interface.
  2. It has one method Error() string.
  3. Any type with that method is an error.

Why the other options are wrong

  • A. It is not a struct.
  • C. The method is Error, not String.
  • D. It is not an int.

Tip: error = Error() string.

Question 4Easy

How does a Go type implement an interface?

  1. By declaring 'implements InterfaceName' after the struct name, as in Java
  2. By registering it at init time
  3. By having all its methods; no implements keyword
  4. By listing the interface's methods inside the struct body

Answer: C. By having all its methods; no implements keyword

Explanation

Satisfaction is implicit, which decouples packages: the interface can be defined by the consumer.

Step-by-step solution

  1. Go uses implicit implementation for interfaces.
  2. A type satisfies an interface if it has all its methods.
  3. No 'implements' keyword is needed.
  4. Methods are declared separately with a receiver, as in func (t T) M(), not inside the struct body.
  5. Final answer: By having all its methods; no implements keyword.

Why the other options are wrong

  • A. Go has no implements keyword, so this declaration is invalid syntax.
  • B. Registration at init time is not needed; a type satisfies an interface by its methods alone.
  • D. Go structs hold only fields; methods are declared outside the struct with a receiver, like func (t T) M().

Tip: Remember: No 'implements' keyword needed in Go.

Question 5Medium

What is the output?

package main

import "fmt"

type Temp float64

func (t Temp) String() string { return fmt.Sprintf("%.1f°C", float64(t)) }

func main() {
	fmt.Println(Temp(21.5))
}
  1. 21.5
  2. 21.5°C
  3. Compile error
  4. Temp(21.5)

Answer: B. 21.5°C

Explanation

fmt checks for the fmt.Stringer interface and uses its String method when printing.

Step-by-step solution

  1. Temp has a String() method, and fmt uses it when printing.
  2. String formats 21.5 with one decimal place and adds °C.
  3. The output is 21.5°C, which is option B.

Why the other options are wrong

  • A. String() adds the °C text, so 21.5 alone is not printed.
  • C. The code compiles and runs.
  • D. Fmt uses the String method, so Temp(21.5) is not printed.

Tip: fmt.Println uses a String() method when the type has one.

Question 6Medium

What is the output?

package main

import "fmt"

type Shape interface{ Area() float64 }

type Sq struct{ s float64 }

func (q Sq) Area() float64 { return q.s * q.s }

func main() {
	var sh Shape = Sq{3}
	fmt.Println(sh.Area())
}
  1. 6
  2. 3
  3. Compile error
  4. 9

Answer: D. 9

Explanation

Sq has an Area() float64 method, so it satisfies Shape implicitly; 3 × 3 = 9.

Step-by-step solution

  1. Sq struct has Area method returning s squared.
  2. Sh is assigned Sq{3}, which satisfies Shape interface.
  3. sh.Area() calls Sq's Area method, 3 * 3 = 9.
  4. fmt.Println prints the result of sh.Area(), which is 9.
  5. Final answer is 9.

Why the other options are wrong

  • A. Area computes s times s, which is 9 for 3, not the sum 6.
  • B. Area returns s squared, not s, so 3 is never printed.
  • C. The code compiles, since Sq has an Area method with the same signature as Shape.

Tip: Check if a struct implements all methods of an interface to satisfy it.

Question 7Medium

What is printed?

var i interface{} = "hi"
n, ok := i.(int)
fmt.Println(n, ok)
  1. panic
  2. hi true
  3. 0 true
  4. 0 false

Answer: D. 0 false

Explanation

The comma ok form of a type assertion does not panic. On a mismatch it gives the zero value and false.

Step-by-step solution

  1. i holds a string.
  2. Assertion to int fails.
  3. n is 0 and ok is false.

Why the other options are wrong

  • A. Only the single result form panics.
  • B. The value is not an int.
  • C. ok is false when the type is wrong.

Question 8Medium

What is the output?

package main

import "fmt"

func kind(v any) string {
	switch v.(type) {
	case int:
		return "int"
	case string:
		return "string"
	default:
		return "other"
	}
}

func main() {
	fmt.Println(kind(1), kind("a"), kind(2.5))
}
  1. Compile error
  2. other other other
  3. int string other
  4. int string float64

Answer: C. int string other

Explanation

A type switch branches on the dynamic type; 2.5 is float64, which falls to default.

Step-by-step solution

  1. kind(1) returns 'int' because 1 is an int.
  2. kind('a') returns 'string' because 'a' is a string.
  3. kind(2.5) returns 'other' because 2.5 is float64, which matches default.
  4. Final output: int string other.

Why the other options are wrong

  • A. The program compiles and runs; a type switch on any is valid.
  • B. kind(1) matches the int case and returns int, so other is wrong for it.
  • D. 2.5 is float64, not a case here, so kind returns the default label other, not float64.

Tip: Remember that numeric literals without decimal points are int, and with decimals are float64.

Question 9Hard

What is the output?

package main

import "fmt"

type Shape interface{ Area() float64 }

type Square struct{ side float64 }

func (q *Square) Area() float64 { return q.side * q.side }

func main() {
	var sh Shape = Square{3}
	fmt.Println(sh.Area())
}
  1. Compile error
  2. 9
  3. 0
  4. Runtime panic

Answer: A. Compile error

Explanation

Area has a pointer receiver, so only *Square has it in its method set and satisfies Shape; use &Square{3}.

Step-by-step solution

  1. The Area method has a pointer receiver, (q *Square).
  2. Square{3} is a value, not a *Square, so it does not have Area in its method set.
  3. So Square does not implement Shape and the assignment fails to compile: option A.

Why the other options are wrong

  • B. The program does not compile, so no output is printed.
  • C. The program never runs, so 0 is not printed.
  • D. There is no runtime panic because the error happens at compile time.

Tip: If the method has a pointer receiver, store &Square{} in the interface, not a value.

Question 10Hard

What is the output?

package main

import "fmt"

func main() {
	var x any = 5
	s := x.(string)
	fmt.Println(s)
}
  1. Compile error
  2. Empty line
  3. 5
  4. Runtime panic

Answer: D. Runtime panic

Explanation

A single-value type assertion that fails panics: interface conversion: interface {} is int, not string.

Step-by-step solution

  1. x holds an int 5.
  2. x.(string) uses the single-result form.
  3. A failed assertion panics at runtime.

Why the other options are wrong

  • A. It compiles.
  • B. It does not print an empty line.
  • C. It does not print 5.

Tip: Single-result assertion panics.

Practise all 15 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