
Practice 50 Go Lang Testing and Benchmarking Coding Questions
Q1. Write a Go program to create a simple unit test for a function that adds two integers.
Input:
Add(3, 5)
Expected Output:
8
Q2. Write a Go program to create a unit test for a function that checks if a number is even.
Input:
IsEven(4)
Expected Output:
true
Q3. Write a Go program to create a unit test for a function that returns the square of a number.
Input:
Square(5)
Expected Output:
25
Q4. Write a Go program to create a unit test for a function that returns the maximum of two numbers.
Input:
Max(8, 10)
Expected Output:
10
Q5. Write a Go program to create a unit test for a function that checks if a string is empty.
Input:
IsEmpty("")
Expected Output:
true
Q6. Write a Go program to create a unit test for a function that returns the factorial of a number.
Input:
Factorial(5)
Expected Output:
120
Q7. Write a Go program to create a unit test for a function that checks if a string contains a specific substring.
Input:
Contains("Go is great", "great")
Expected Output:
true
Q8. Write a Go program to create a unit test for a function that reverses a string.
Input:
Reverse("hello")
Expected Output:
"olleh"
Q9. Write a Go program to create a unit test for a function that concatenates two strings.
Input:
Concatenate("Go", "Lang")
Expected Output:
"GoLang"
Q10. Write a Go program to create a unit test for a function that checks if a number is prime.
Input:
IsPrime(7)
Expected Output:
true
Q11. Write a Go program to create a unit test for a function that calculates the length of a string.
Input:
Length("Hello, Go!")
Expected Output:
11
Q12. Write a Go program to create a unit test for a function that capitalizes the first letter of a string.
Input:
Capitalize("hello")
Expected Output:
"Hello"
Q13. Write a Go program to create a unit test for a function that finds the index of an element in a slice.
Input:
IndexOf([]int{1, 2, 3, 4}, 3)
Expected Output:
2
Q14. Write a Go program to create a unit test for a function that removes an element from a slice.
Input:
Remove([]int{1, 2, 3}, 2)
Expected Output:
[1, 3]
Q15. Write a Go program to create a unit test for a function that returns the sum of elements in an array.
Input:
Sum([1, 2, 3, 4])
Expected Output:
10
Q16. Write a Go program to create a unit test for a function that checks if a number is a perfect square.
Input:
IsPerfectSquare(16)
Expected Output:
true
Q17. Write a Go program to create a unit test for a function that computes the GCD of two numbers.
Input:
GCD(48, 180)
Expected Output:
12
Q18. Write a Go program to create a unit test for a function that calculates the area of a circle given its radius.
Input:
AreaOfCircle(5)
Expected Output:
78.53981633974483
Q19. Write a Go program to create a unit test for a function that finds the largest number in an array.
Input:
Largest([1, 3, 7, 2])
Expected Output:
7
Q20. Write a Go program to create a unit test for a function that checks if a number is a palindrome.
Input:
IsPalindrome(121)
Expected Output:
true
Q21. Write a Go program to create a benchmark for a function that adds two integers.
Input:
Benchmark Add(10, 20)
Expected Output:
Benchmark result: 10ns per operation
Q22. Write a Go program to create a benchmark for a function that reverses a string.
Input:
Benchmark Reverse("testing")
Expected Output:
Benchmark result: 50ns per operation
Q23. Write a Go program to create a benchmark for a function that checks if a number is prime.
Input:
Benchmark IsPrime(23)
Expected Output:
Benchmark result: 15ns per operation
Q24. Write a Go program to create a benchmark for a function that computes the sum of an array.
Input:
Benchmark Sum([1, 2, 3, 4, 5])
Expected Output:
Benchmark result: 20ns per operation
Q25. Write a Go program to create a benchmark for a function that concatenates two strings.
Input:
Benchmark Concatenate("Go", "Lang")
Expected Output:
Benchmark result: 12ns per operation
Q26. Write a Go program to create a benchmark for a function that calculates the length of a string.
Input:
Benchmark Length("Hello, Go!")
Expected Output:
Benchmark result: 10ns per operation
Q27. Write a Go program to create a unit test for a function that divides two numbers.
Input:
Divide(10, 2)
Expected Output:
5
Q28. Write a Go program to create a unit test for a function that checks if a string starts with a certain prefix.
Input:
StartsWith("GoLang", "Go")
Expected Output:
true
Q29. Write a Go program to create a unit test for a function that returns the absolute value of a number.
Input:
Abs(-10)
Expected Output:
10
Q30. Write a Go program to create a unit test for a function that checks if an element exists in a slice.
Input:
Contains([]int{1, 2, 3}, 2)
Expected Output:
true
Q31. Write a Go program to create a benchmark for a function that calculates the factorial of a number.
Input:
Benchmark Factorial(5)
Expected Output:
Benchmark result: 25ns per operation
Q32. Write a Go program to create a unit test for a function that checks if a number is negative.
Input:
IsNegative(-3)
Expected Output:
true
Q33. Write a Go program to create a unit test for a function that multiplies two numbers.
Input:
Multiply(3, 4)
Expected Output:
12
Q34. Write a Go program to create a unit test for a function that checks if a number is divisible by another number.
Input:
IsDivisible(10, 2)
Expected Output:
true
Q35. Write a Go program to create a unit test for a function that removes the last element of a slice.
Input:
RemoveLast([]int{1, 2, 3})
Expected Output:
[1, 2]
Q36. Write a Go program to create a benchmark for a function that checks if a string contains another string.
Input:
Benchmark Contains("Go is great", "great")
Expected Output:
Benchmark result: 30ns per operation
Q37. Write a Go program to create a unit test for a function that calculates the power of a number.
Input:
Power(2, 3)
Expected Output:
8
Q38. Write a Go program to create a benchmark for a function that checks if a string is empty.
Input:
Benchmark IsEmpty("")
Expected Output:
Benchmark result: 5ns per operation
Q39. Write a Go program to create a benchmark for a function that calculates the Fibonacci sequence up to `n`.
Input:
Benchmark Fibonacci(10)
Expected Output:
Benchmark result: 45ns per operation
Q40. Write a Go program to create a unit test for a function that checks if a number is an Armstrong number.
Input:
IsArmstrong(153)
Expected Output:
true
Q41. Write a Go program to create a unit test for a function that checks if a string is a palindrome.
Input:
IsStringPalindrome("racecar")
Expected Output:
true
Q42. Write a Go program to create a benchmark for a function that returns the first n Fibonacci numbers.
Input:
Benchmark FibonacciSeries(5)
Expected Output:
Benchmark result: 40ns per operation
Q43. Write a Go program to create a unit test for a function that calculates the area of a rectangle.
Input:
AreaOfRectangle(5, 7)
Expected Output:
35
Q44. Write a Go program to create a unit test for a function that multiplies an array of integers.
Input:
MultiplyArray([1, 2, 3, 4])
Expected Output:
24
Q45. Write a Go program to create a benchmark for a function that converts a string to lowercase.
Input:
Benchmark ToLower("GoLang")
Expected Output:
Benchmark result: 10ns per operation
Q46. Write a Go program to create a unit test for a function that removes an element from an array.
Input:
RemoveElement([1, 2, 3], 2)
Expected Output:
[1, 3]
Q47. Write a Go program to create a benchmark for a function that calculates the power of a number.
Input:
Benchmark Power(2, 10)
Expected Output:
Benchmark result: 15ns per operation
Q48. Write a Go program to create a unit test for a function that calculates the sum of odd numbers in a slice.
Input:
SumOdd([1, 2, 3, 4])
Expected Output:
4
Q49. Write a Go program to create a unit test for a function that calculates the product of elements in a slice.
Input:
Product([1, 2, 3, 4])
Expected Output:
24
Q50. Write a Go program to create a benchmark for a function that finds the index of an element in a slice.
Input:
Benchmark IndexOf([1, 2, 3, 4], 3)
Expected Output:
Benchmark result: 20ns per operation