Practice 50 Swift Functions and Closures Questions, TechnoVlogs

Practice 50 Swift Functions and Closures Questions


Q1. Write a Swift program to define a function that takes two integers as parameters and returns their sum.  
  Input: 5, 10  
  Expected Output: Sum is 15  

Q2. Write a Swift program to define a function that takes a string and returns its length.  
  Input: "Hello, Swift!"  
  Expected Output: Length is 13  

Q3. Write a Swift program to define a function that takes an integer and returns true if it is even, otherwise false.  
  Input: 8  
  Expected Output: true  

Q4. Write a Swift program to define a function that takes a string and returns it reversed.  
  Input: "Swift"  
  Expected Output: "tfiwS"  

Q5. Write a Swift program to define a function that checks if a given string is a palindrome.  
  Input: "madam"  
  Expected Output: true  

Q6. Write a Swift program to define a function that takes an array of integers and returns the largest number.  
  Input: [5, 2, 9, 1, 7]  
  Expected Output: Largest number is 9  

Q7. Write a Swift program to define a closure that multiplies two numbers and returns the result.  
  Input: 3, 4  
  Expected Output: Product is 12  

Q8. Write a Swift program to define a function that calculates the factorial of a given number.  
  Input: 5  
  Expected Output: Factorial is 120  

Q9. Write a Swift program to define a closure that concatenates two strings.  
  Input: "Hello", "World"  
  Expected Output: "HelloWorld"  

Q10. Write a Swift program to define a function that takes an array of strings and returns the longest string.  
   Input: ["apple", "banana", "cherry"]  
   Expected Output: Longest string is "banana"  

Q11. Write a Swift program to define a function that swaps two integers using inout parameters.  
   Input: 5, 10  
   Expected Output: Swapped values are 10, 5  

Q12. Write a Swift program to define a function that calculates the sum of all elements in an array.  
   Input: [1, 2, 3, 4, 5]  
   Expected Output: Sum is 15  

Q13. Write a Swift program to define a closure that takes a string and returns the uppercase version of it.  
   Input: "swift"  
   Expected Output: "SWIFT"  

Q14. Write a Swift program to define a function that takes a number and returns its square.  
   Input: 6  
   Expected Output: Square is 36  

Q15. Write a Swift program to define a function that takes an array of integers and returns the average.  
   Input: [10, 20, 30, 40]  
   Expected Output: Average is 25  

Q16. Write a Swift program to define a closure that calculates the area of a rectangle.  
   Input: length = 5, width = 3  
   Expected Output: Area is 15  

Q17. Write a Swift program to define a function that checks whether a given year is a leap year.  
   Input: 2024  
   Expected Output: true  

Q18. Write a Swift program to define a function that returns the first n numbers in the Fibonacci sequence.  
   Input: 5  
   Expected Output: [0, 1, 1, 2, 3]  

Q19. Write a Swift program to define a function that takes a string and a character, and counts the occurrences of the character in the string. 
   Input: "banana", "a"  
   Expected Output: 3  

Q20. Write a Swift program to define a function that calculates the power of a number using recursion.  
   Input: base = 2, exponent = 3  
   Expected Output: 8  

Q21. Write a Swift program to define a function that returns the largest prime number in an array.  
   Input: [4, 7, 9, 13, 21]  
   Expected Output: 13  

Q22. Write a Swift program to define a function that takes a dictionary and returns the key with the highest value.  
   Input: ["A": 10, "B": 25, "C": 15]  
   Expected Output: "B"  

Q23. Write a Swift program to define a function that converts a temperature from Celsius to Fahrenheit.  
   Input: 25  
   Expected Output: Fahrenheit is 77  

Q24. Write a Swift program to define a closure that takes an integer and returns whether it is a prime number.  
   Input: 11  
   Expected Output: true  

Q25. Write a Swift program to define a function that takes a string and returns the number of vowels in it.  
   Input: "hello"  
   Expected Output: 2  

Q26. Write a Swift program to define a function that finds the smallest number in an array.  
   Input: [9, 3, 5, 7]  
   Expected Output: Smallest number is 3  

Q27. Write a Swift program to define a function that converts a decimal number to binary.  
   Input: 10  
   Expected Output: Binary is "1010"  

Q28. Write a Swift program to define a closure that calculates the circumference of a circle.  
   Input: radius = 7  
   Expected Output: Circumference is 43.98  

Q29. Write a Swift program to define a function that finds the gcd of two numbers.  
   Input: 36, 48  
   Expected Output: GCD is 12  

Q30. Write a Swift program to define a function that returns all odd numbers in an array.  
   Input: [2, 3, 5, 6, 8]  
   Expected Output: [3, 5]  

Q31. Write a Swift program to define a function that takes a string and a substring, and checks if the substring exists in the string.  
   Input: "hello", "ell"  
   Expected Output: true  

Q32. Write a Swift program to define a function that calculates the sum of digits of an integer.  
   Input: 123  
   Expected Output: Sum is 6  

Q33. Write a Swift program to define a function that removes all spaces from a string.  
   Input: "Swift is awesome"  
   Expected Output: "Swiftisawesome"  

Q34. Write a Swift program to define a closure that checks if a number is a perfect square.  
   Input: 16  
   Expected Output: true  

Q35. Write a Swift program to define a function that returns the product of all elements in an array.  
   Input: [1, 2, 3, 4]  
   Expected Output: Product is 24  

Q36. Write a Swift program to define a function that sorts an array in ascending order.  
   Input: [9, 2, 5, 1]  
   Expected Output: [1, 2, 5, 9]  

Q37. Write a Swift program to define a function that checks if two strings are anagrams.  
   Input: "listen", "silent"  
   Expected Output: true  

Q38. Write a Swift program to define a closure that calculates the cube of a number.  
   Input: 3  
   Expected Output: Cube is 27  

Q39. Write a Swift program to define a function that returns the first index of a given element in an array.  
   Input: [2, 4, 6, 8], element = 6  
   Expected Output: Index is 2  

Q40. Write a Swift program to define a function that removes duplicates from an array.  
   Input: [1, 2, 2, 3, 4, 4]  
   Expected Output: [1, 2, 3, 4]  

Q41. Write a Swift program to define a function that calculates the sum of squares of all elements in an array.  
   Input: [1, 2, 3]  
   Expected Output: 14  

Q42. Write a Swift program to define a closure that calculates the simple interest.  
   Input: principal = 1000, rate = 5, time = 2  
   Expected Output: Interest is 100  

Q43. Write a Swift program to define a function that converts hours to seconds.  
   Input: 2  
   Expected Output: 7200  

Q44. Write a Swift program to define a function that checks if all elements in an array are positive.  
   Input: [1, 2, 3, 4]  
   Expected Output: true  

Q45. Write a Swift program to define a function that calculates the sum of elements at even indices in an array.  
   Input: [1, 2, 3, 4, 5]  
   Expected Output: 9  

Q46. Write a Swift program to define a function that takes a string and returns it with all vowels replaced by ''.  
   Input: "Swift"  
   Expected Output: "Swft"  

Q47. Write a Swift program to define a function that finds the second largest number in an array.  
   Input: [5, 8, 2, 6]  
   Expected Output: 6  

Q48. Write a Swift program to define a function that calculates the difference between the maximum and minimum values in an array.  
   Input: [10, 20, 5, 8]  
   Expected Output: 15  

Q49. Write a Swift program to define a closure that calculates the perimeter of a square.  
   Input: side = 4  
   Expected Output: Perimeter is 16  

Q50. Write a Swift program to define a function that checks if a string starts with a specific prefix.  
   Input: "Swift programming", prefix = "Swift"  
   Expected Output: true  

Social Share

Bikki Singh Instructor TechnoVlogs

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!