Practice 50 Go Lang Interfaces and Reflection Coding Questions, TechnoVlogs

Practice 50 Go Lang Interfaces and Reflection Coding Questions


Q1. Write a Go program to define an interface with a method Speak(), and implement the interface in a Person struct.  
  Input:    
  Person: John
  Expected Output:  
  John says Hello!

Q2. Write a Go program to define an interface Shape with methods Area() and Perimeter(). Implement this interface for Rectangle and Circle structs.  
  Input:    
  Rectangle: Length = 4, Width = 5
  Circle: Radius = 3
  Expected Output:  
  Rectangle Area: 20, Perimeter: 18
  Circle Area: 28.274333882308138, Perimeter: 18.84955592153876

Q3. Write a Go program to create an interface Drawable with a method Draw(), and implement it for Circle and Rectangle structs.  
  Input:    
  Circle: Radius = 5
  Rectangle: Length = 6, Width = 4
  Expected Output:  
  Drawing a Circle
  Drawing a Rectangle

Q4. Write a Go program to create an interface Vehicle with a method Drive(). Implement it for Car and Bike structs.  
  Input:    
  Car: Model = "Sedan"
  Bike: Model = "Mountain"
  Expected Output:  
  Driving a Sedan
  Riding a Mountain Bike

Q5. Write a Go program to demonstrate type assertion with an interface and check the concrete type.  
  Input:    
  Interface: Shape, Concrete type: Circle
  Expected Output:  
  Concrete type: Circle

Q6. Write a Go program to define an interface Animal with a method Sound(). Implement it for Dog and Cat structs.  
  Input:    
  Dog: Breed = "Bulldog"
  Cat: Breed = "Siamese"
  Expected Output:  
  Bulldog makes Woof!
  Siamese makes Meow!

Q7. Write a Go program to create an interface Operation with methods Add() and Subtract(). Implement this interface for Calculator struct.  
  Input:    
  Add: 5 + 3
  Subtract: 5 - 3
  Expected Output:  
  Addition: 8
  Subtraction: 2

Q8. Write a Go program to create an interface Payable with a method Pay(). Implement it for Employee and Freelancer structs.  
  Input:    
  Employee Salary: 5000
  Freelancer Pay: 2000
  Expected Output:  
  Paying Employee 5000
  Paying Freelancer 2000

Q9. Write a Go program to implement an empty interface and store different data types in it.  
  Input:    
  Data types: 42, "Hello", true
  Expected Output:  
  42
  Hello
  true

Q10. Write a Go program to define an interface Transport with a method Move(), and implement it for Bus and Train structs.  
   Input:     
   Bus: Model = "Volvo"
   Train: Model = "Express"
   Expected Output:  
   Moving a Bus (Volvo)
   Moving a Train (Express)

Q11. Write a Go program to use reflection to inspect the type and value of a variable.  
   Input:     
   Variable: "GoLang"
   Expected Output:  
   Type: string, Value: GoLang

Q12. Write a Go program to demonstrate the use of reflect.TypeOf to check the type of a variable.  
   Input:     
   Variable: 10
   Expected Output:  
   Type: int

Q13. Write a Go program to demonstrate the use of reflect.ValueOf to check the value of a variable.  
   Input:     
   Variable: true
   Expected Output:  
   Value: true

Q14. Write a Go program to use reflection to change the value of a variable.  
   Input:     
   Original Value: 5
   New Value: 10
   Expected Output:  
   Updated Value: 10

Q15. Write a Go program to create an interface Payment with a method Process(), and implement it for CreditCard and DebitCard structs.  
   Input:     
   CreditCard Number: 1234-5678
   DebitCard Number: 9876-5432
   Expected Output:  
   Processing CreditCard 1234-5678
   Processing DebitCard 9876-5432

Q16. Write a Go program to create an interface Shape with a method Area(). Implement it for Rectangle and Triangle structs.  
   Input:     
   Rectangle: Length = 4, Width = 6
   Triangle: Base = 4, Height = 3
   Expected Output:  
   Rectangle Area: 24
   Triangle Area: 6

Q17. Write a Go program to use reflection to dynamically call a function.  
   Input:     
   Function: Add(x, y int) -> x + y
   Parameters: 3, 4
   Expected Output:  
   Result: 7

Q18. Write a Go program to use reflection to iterate over the fields of a struct.  
   Input:     
   Struct: Person{Name: "Alice", Age: 30}
   Expected Output:  
   Name: Alice
   Age: 30

Q19. Write a Go program to create an interface User with methods Login() and Logout(). Implement it for Admin and Guest structs.  
   Input:     
   Admin Username: admin1
   Guest Username: guest1
   Expected Output:  
   Admin logged in
   Guest logged in

Q20. Write a Go program to create an interface Processor with a method ProcessData(). Implement it for CSVProcessor and JSONProcessor structs.  
   Input:     
   Data: {"name": "John", "age": 30}
   Expected Output:  
   Processing CSV Data
   Processing JSON Data

Q21. Write a Go program to use reflection to inspect a slice's length and type.  
   Input:     
   Slice: [1, 2, 3]
   Expected Output:  
   Type: slice, Length: 3

Q22. Write a Go program to demonstrate type assertion on an interface that holds a string value.  
   Input:     
   Interface: "GoLang"
   Expected Output:  
   Concrete type: string
   Value: GoLang

Q23. Write a Go program to create an interface Logger with a method LogMessage(). Implement it for ConsoleLogger and FileLogger structs.  
   Input:     
   Console Logger: "Console Log"
   File Logger: "File Log"
   Expected Output:  
   Console Log: Console Log
   File Log: File Log

Q24. Write a Go program to use reflection to call a method of a struct dynamically.  
   Input:     
   Method: Speak(), Struct: Person{Name: "John"}
   Expected Output:  
   John says Hello!

Q25. Write a Go program to create an interface Account with methods Deposit() and Withdraw(). Implement it for SavingAccount and CheckingAccount structs.  
   Input:     
   Deposit: 100, Withdraw: 50
   Expected Output:  
   Deposited: 100
   Withdrawn: 50

Q26. Write a Go program to define an interface Runnable with a method Run(). Implement it for Car and Motorcycle structs.  
   Input:     
   Car: Model = "Honda"
   Motorcycle: Model = "Yamaha"
   Expected Output:  
   Running a Car (Honda)
   Running a Motorcycle (Yamaha)

Q27. Write a Go program to create a type FloatValue that implements Stringer interface for printing float values.  
   Input:     
   Float: 10.75
   Expected Output:  
   10.75

Q28. Write a Go program to use reflection to check if a value is a pointer.  
   Input:     
   Variable: Pointer to int
   Expected Output:  
   Is a pointer: true

Q29. Write a Go program to create an interface Person with a method Introduce(). Implement it for Employee and Student structs.  
   Input:     
   Employee: "John", Student: "Alice"
   Expected Output:  
   Employee John introduces himself
   Student Alice introduces herself

Q30. Write a Go program to create an interface Converter with a method Convert(). Implement it for Temperature and Currency structs.  
   Input:     
   Temperature: 100°C to Fahrenheit
   Currency: 50 USD to EUR
   Expected Output:  
   Temperature: 212°F
   Currency: 42 EUR

Q31. Write a Go program to use reflection to set a value of a field dynamically.  
   Input:     
   Struct: Person{Name: "John"}, Set Name = "Alice"
   Expected Output:      
   Updated Name: Alice

Q32. Write a Go program to create a type Double that implements fmt.Stringer interface to print doubled values.  
   Input:     
   Value: 5
   Expected Output:  
   Doubled value: 10

Q33. Write a Go program to use reflection to check if a value implements an interface.  
   Input:     
   Variable: Implements the Stringer interface
   Expected Output:  
   Implements interface: true

Q34. Write a Go program to define an interface Manager with method Supervise(). Implement it for TeamLead and ProjectManager structs.  
   Input:     
   TeamLead Name: Alice
   ProjectManager Name: Bob
   Expected Output:  
   Alice supervises the team
   Bob supervises the project

Q35. Write a Go program to use reflection to invoke a method with arguments.  
   Input:     
   Method: Add(x, y int) -> x + y
   Arguments: 4, 3
   Expected Output:  
   Result: 7

Q36. Write a Go program to create an interface Operation with methods Execute() and Undo(). Implement it for Copy and Delete operations. 
   Input:     
   Copy Action: Copying file
   Delete Action: Deleting file
   Expected Output:  
   Executing Copy
   Executing Delete

Q37. Write a Go program to use reflection to check if a value is of type struct.  
   Input:     
   Variable: Person{Name: "John"}
   Expected Output:  
   Is a struct: true

Q38. Write a Go program to create an interface Employee with method Work(). Implement it for Developer and Designer structs.  
   Input:     
   Developer: "John"
   Designer: "Alice"
   Expected Output:  
   Developer John works on code
   Designer Alice works on design

Q39. Write a Go program to use reflection to inspect a map and display its keys and values.  
   Input:     
   Map: {"name": "John", "age": 30}
   Expected Output:  
   Key: name, Value: John
   Key: age, Value: 30

Q40. Write a Go program to create an interface Shape with a method Perimeter(). Implement it for Square and Triangle structs.  
   Input:     
   Square: Side = 4
   Triangle: Side1 = 3, Side2 = 4, Side3 = 5
   Expected Output:  
   Square Perimeter: 16
   Triangle Perimeter: 12

Q41. Write a Go program to create an interface Account with method GetBalance(). Implement it for CheckingAccount and SavingsAccount.  
   Input:     
   Checking Account Balance: 1000
   Savings Account Balance: 5000
   Expected Output:  
   Checking Account Balance: 1000
   Savings Account Balance: 5000

Q42. Write a Go program to use reflection to modify a value in a map.  
   Input:     
   Map: {"name": "John"}
   Key to modify: name, New value: Alice
   Expected Output:  
   Updated Map: {"name": "Alice"}

Q43. Write a Go program to create an interface Worker with method PerformTask(). Implement it for Plumber and Electrician structs.  
   Input:     
   Plumber: "John"
   Electrician: "Alice"
   Expected Output:  
   Plumber John performs plumbing
   Electrician Alice performs electrical work

Q44. Write a Go program to use reflection to create a new instance of a struct dynamically.  
   Input:     
   Struct: Person{Name: "John"}
   Expected Output:  
   New Instance Created: John

Q45. Write a Go program to define an interface Logger with method Log(). Implement it for FileLogger and ConsoleLogger structs.  
   Input:     
   File Logger: "Logging to file"
   Console Logger: "Logging to console"
   Expected Output:  
   Logging to file
   Logging to console

Q46. Write a Go program to use reflection to check the number of fields in a struct.  
   Input:     
   Struct: Person{Name: "John", Age: 30}
   Expected Output:  
   Number of Fields: 2

Q47. Write a Go program to use reflection to check the kind of a value.  
   Input:     
   Value: 100
   Expected Output:  
   Kind: int

Q48. Write a Go program to use reflection to check if a value implements a specific interface.  
   Input:     
   Variable: Implements the fmt.Stringer interface
   Expected Output:  
   Implements fmt.Stringer: true

Q49. Write a Go program to create an interface Message with a method Send(). Implement it for Email and SMS structs.  
   Input:     
   Email: "john@example.com"
   SMS: "555-1234"
   Expected Output:  
   Sending Email to john@example.com
   Sending SMS to 555-1234

Q50. Write a Go program to use reflection to call a method that accepts parameters.  
   Input:     
   Method: Multiply(x, y int) -> x * y
   Arguments: 3, 4
   Expected Output:  
   Result: 12

Share on Social Media