Practice 50 Swift Unit Testing Programming Questions
Q1. Write a Swift program to create a unit test to check if a function adds two numbers correctly.
Input: Two integers 5 and 10.
Output: The function should return 15.
Q2. Write a Swift program to create a unit test that verifies if a string is empty.
Input: An empty string "".
Output: The test should return true.
Q3. Write a Swift program to create a unit test to check if a function correctly multiplies two numbers.
Input: Two integers 3 and 4.
Output: The function should return 12.
Q4. Write a Swift program to create a unit test to verify if an array contains a specific element.
Input: Array [1, 2, 3] and element 2.
Output: The test should return true.
Q5. Write a Swift program to create a unit test to check if a number is even.
Input: Number 4.
Output: The test should return true.
Q6. Write a Swift program to create a unit test to check if a number is odd.
Input: Number 5.
Output: The test should return true.
Q7. Write a Swift program to create a unit test to verify if a function correctly returns the sum of an array.
Input: Array [1, 2, 3, 4].
Output: The sum should be 10.
Q8. Write a Swift program to create a unit test to check if a function returns the correct factorial of a number.
Input: Number 5.
Output: The factorial should be 120.
Q9. Write a Swift program to create a unit test to check if a string contains a specific substring.
Input: String "hello world" and substring "world".
Output: The test should return true.
Q10. Write a Swift program to create a unit test that verifies if a number is prime.
Input: Number 7.
Output: The test should return true.
Q11. Write a Swift program to create a unit test to check if a function correctly reverses a string.
Input: String "hello".
Output: The function should return "olleh".
Q12. Write a Swift program to create a unit test to check if a dictionary contains a specific key.
Input: Dictionary [1: "one", 2: "two"] and key 2.
Output: The test should return true.
Q13. Write a Swift program to create a unit test to check if a function returns the maximum value in an array.
Input: Array [1, 5, 3, 7].
Output: The maximum value should be 7.
Q14. Write a Swift program to create a unit test to verify if a string is a valid email address.
Input: String "test@example.com".
Output: The test should return true.
Q15. Write a Swift program to create a unit test to check if a function returns the correct average of an array.
Input: Array [1, 2, 3, 4].
Output: The average should be 2.5.
Q16. Write a Swift program to create a unit test to check if a number is within a given range.
Input: Number 5 and range 1...10.
Output: The test should return true.
Q17. Write a Swift program to create a unit test to check if a function returns the correct length of a string.
Input: String "hello".
Output: The length should be 5.
Q18. Write a Swift program to create a unit test to verify if a function correctly concatenates two strings.
Input: Strings "hello" and "world".
Output: The concatenated result should be "helloworld".
Q19. Write a Swift program to create a unit test to check if a string is a valid palindrome.
Input: String "madam".
Output: The test should return true.
Q20. Write a Swift program to create a unit test to verify if an array is sorted in ascending order.
Input: Array [1, 2, 3, 4].
Output: The test should return true.
Q21. Write a Swift program to create a unit test to check if a function correctly calculates the square of a number.
Input: Number 5.
Output: The square should be 25.
Q22. Write a Swift program to create a unit test to check if a function correctly converts a string to uppercase.
Input: String "hello".
Output: The function should return "HELLO".
Q23. Write a Swift program to create a unit test to check if a string has a specific length.
Input: String "hello" and length 5.
Output: The test should return true.
Q24. Write a Swift program to create a unit test to verify if a function correctly calculates the area of a circle.
Input: Radius 5.
Output: The area should be 78.54.
Q25. Write a Swift program to create a unit test to verify if a function checks if a string starts with a specific prefix.
Input: String "hello" and prefix "he".
Output: The test should return true.
Q26. Write a Swift program to create a unit test to check if a number is divisible by another number.
Input: Number 10 and divisor 2.
Output: The test should return true.
Q27. Write a Swift program to create a unit test to verify if a function calculates the area of a rectangle correctly.
Input: Length 4 and width 6.
Output: The area should be 24.
Q28. Write a Swift program to create a unit test to check if a function returns the correct sum of even numbers in an array.
Input: Array [1, 2, 3, 4].
Output: The sum of even numbers should be 6.
Q29. Write a Swift program to create a unit test to check if a function correctly finds the smallest number in an array.
Input: Array [3, 5, 1, 7].
Output: The smallest number should be 1.
Q30. Write a Swift program to create a unit test to check if a string contains only digits.
Input: String "12345".
Output: The test should return true.
Q31. Write a Swift program to create a unit test to verify if a function correctly calculates the difference between two numbers.
Input: Numbers 10 and 3.
Output: The difference should be 7.
Q32. Write a Swift program to create a unit test to check if a string contains only uppercase letters.
Input: String "HELLO".
Output: The test should return true.
Q33. Write a Swift program to create a unit test to check if a string contains only lowercase letters.
Input: String "hello".
Output: The test should return true.
Q34. Write a Swift program to create a unit test to check if a function correctly finds the index of an element in an array.
Input: Array [1, 2, 3] and element 2.
Output: The index should be 1.
Q35. Write a Swift program to create a unit test to check if a function correctly returns the last element of an array.
Input: Array [1, 2, 3].
Output: The last element should be 3.
Q36. Write a Swift program to create a unit test to verify if a string is a valid URL.
Input: String "https://www.example.com".
Output: The test should return true.
Q37. Write a Swift program to create a unit test to check if a function correctly finds the common elements between two arrays.
Input: Arrays [1, 2, 3] and [2, 3, 4].
Output: The common elements should be [2, 3].
Q38. Write a Swift program to create a unit test to check if a number is negative.
Input: Number -5.
Output: The test should return true.
Q39. Write a Swift program to create a unit test to check if a function correctly finds the length of the longest word in a string.
Input: String "I love programming".
Output: The length should be 11.
Q40. Write a Swift program to create a unit test to check if a function returns a string without spaces.
Input: String "hello world".
Output: The function should return "helloworld".
Q41. Write a Swift program to create a unit test to check if a function correctly counts the occurrences of a character in a string.
Input: String "hello world" and character "o".
Output: The count should be 2.
Q42. Write a Swift program to create a unit test to check if a function correctly removes whitespace from a string.
Input: String " hello ".
Output: The function should return "hello".
Q43. Write a Swift program to create a unit test to check if a number is a multiple of another number.
Input: Number 15 and divisor 5.
Output: The test should return true.
Q44. Write a Swift program to create a unit test to check if a function correctly checks for palindrome.
Input: String "racecar".
Output: The test should return true.
Q45. Write a Swift program to create a unit test to check if a function correctly rounds a number to two decimal places.
Input: Number 3.14159.
Output: The rounded number should be 3.14.
Q46. Write a Swift program to create a unit test to verify if a function correctly finds the longest common prefix of two strings.
Input: Strings "flower" and "flow".
Output: The common prefix should be "fl".
Q47. Write a Swift program to create a unit test to check if a function correctly merges two arrays.
Input: Arrays [1, 2] and [3, 4].
Output: The merged array should be [1, 2, 3, 4].
Q48. Write a Swift program to create a unit test to verify if a function correctly computes the power of a number.
Input: Base 2 and exponent 3.
Output: The result should be 8.
Q49. Write a Swift program to create a unit test to check if a number is positive.
Input: Number 5.
Output: The test should return true.
Q50. Write a Swift program to create a unit test to check if a function correctly returns the current time in seconds.
Input: Current time (not required as input).
Output: The function should return the current time in seconds.
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!