Practice 50 Swift Advanced Topics Programming Questions, TechnoVlogs

Practice 50 Swift Advanced Topics Programming Questions


Q1. Write a Swift program to demonstrate the use of closures with parameters and return values.
  Input: Closure that adds two integers 5 and 10.
  Output: The result should be 15.

Q2. Write a Swift program to demonstrate how to use map() on an array of integers.
  Input: Array [1, 2, 3, 4].
  Output: The mapped array should be [2, 4, 6, 8].

Q3. Write a Swift program to implement and use a custom operator.
  Input: Integers 5 and 10.
  Output: The result should be 5 + 10 = 15.

Q4. Write a Swift program to demonstrate how to work with associated values in enums.
  Input: Enum case .rectangle(4, 5).
  Output: The area should be 20.

Q5. Write a Swift program to demonstrate the usage of guard statement to handle early exits.
  Input: Integer -1.
  Output: The function should print "Negative input."

Q6. Write a Swift program to demonstrate how to use the defer statement.
  Input: Code that opens a file and uses defer to close it.
  Output: The file is closed after the block of code runs.

Q7. Write a Swift program to implement a basic example of protocol inheritance.
  Input: Protocols Shape and Drawable, with a class Circle.
  Output: The class Circle should confirm to both Shape and Drawable.

Q8. Write a Swift program to demonstrate method overriding in classes.
  Input: Method calculateArea() in a subclass Rectangle.
  Output: The method should return the correct area of a rectangle.

Q9. Write a Swift program to demonstrate the use of typealias.
  Input: Type alias typealias Integer = Int and using it in a function.
  Output: The function should accept and return an Integer.

Q10. Write a Swift program to demonstrate how to use Any and AnyObject.
   Input: An array of mixed types [1, "hello", 3.14].
   Output: Print the values using Any type.

Q11. Write a Swift program to demonstrate the use of @escaping in closures.
   Input: A closure passed to an asynchronous function.
   Output: The closure is executed after the function completes.

Q12. Write a Swift program to demonstrate lazy loading of a computed property.
   Input: A computed property lazy var expensiveComputation: Int.
   Output: The computation should be delayed until the property is accessed.

Q13. Write a Swift program to show how to use NSCoding for object serialization.
   Input: An object of a Person class with name and age properties.
   Output: The object should be encoded and decoded correctly.

Q14. Write a Swift program to implement a simple version of Key-Value Observing (KVO).
   Input: A Person class with a name property.
   Output: Print a message whenever name is changed.

Q15. Write a Swift program to demonstrate the use of Result type for error handling.
   Input: A function that divides two integers.
   Output: Return a Result type, handling division by zero.

Q16. Write a Swift program to demonstrate @objc and dynamic in a class.
   Input: A class with a method annotated with @objc dynamic.
   Output: The method is available for Objective-C runtime.

Q17. Write a Swift program to implement and use a custom Error enum.
   Input: Enum FileError with cases .notFound, .permissionDenied.
   Output: Handle an error when attempting to open a file.

Q18. Write a Swift program to implement a Singleton class.
   Input: Class Logger with a static shared instance.
   Output: The Logger instance should be accessed only once throughout the application.

Q19. Write a Swift program to demonstrate the use of @published property wrapper in a ObservableObject.
   Input: A class User with a name property.
   Output: The name property should trigger updates to the UI.

Q20. Write a Swift program to demonstrate the use of Combine framework to subscribe to a publisher.
   Input: A publisher emitting integer values.
   Output: The subscriber should print the emitted values.

Q21. Write a Swift program to demonstrate the use of @State property wrapper in SwiftUI.
   Input: A SwiftUI view with a toggle that modifies a state variable.
   Output: The state variable should update the view accordingly.

Q22. Write a Swift program to implement and use a custom Subscript.
   Input: Class Matrix with a subscript for accessing elements.
   Output: The subscript should return the correct element from the matrix.

Q23. Write a Swift program to demonstrate the use of @ViewBuilder in SwiftUI.
   Input: A SwiftUI view with multiple conditional child views.
   Output: The correct child view should be rendered based on the condition.

Q24. Write a Swift program to demonstrate using CocoaPods for dependency management in an iOS app.
   Input: Add a dependency Alamofire using CocoaPods.
   Output: The app should correctly use Alamofire to make a network request.

Q25. Write a Swift program to demonstrate the use of URLSession for networking.
   Input: URL to fetch data from a server.
   Output: The program should download data and handle it asynchronously.

Q26. Write a Swift program to demonstrate the use of NotificationCenter for event handling.
   Input: Post a notification with a message.
   Output: Other parts of the app should listen and respond to the notification.

Q27. Write a Swift program to implement UIViewController transitions with UIView animations.
   Input: A button that triggers a transition between two view controllers.
   Output: The view controller transition should animate smoothly.

Q28. Write a Swift program to demonstrate the use of DispatchQueue for concurrency.
   Input: A task that executes on a background queue.
   Output: The task should run in the background and update the UI on the main queue.

Q29. Write a Swift program to demonstrate how to use UICollectionView for creating a grid layout.
   Input: An array of strings representing data.
   Output: Display the data in a grid layout with multiple cells.

Q30. Write a Swift program to demonstrate the use of UITableView for dynamic list data.
   Input: An array of strings.
   Output: Display the strings as a list in a table view.

Q31. Write a Swift program to demonstrate the use of UIStackView for layout management.
   Input: A vertical stack view containing labels.
   Output: The labels should be displayed in a vertical arrangement.

Q32. Write a Swift program to demonstrate the use of Codable for JSON parsing.
   Input: JSON data representing a User object.
   Output: Parse the JSON into a User struct and print the properties.

Q33. Write a Swift program to implement and use Generic classes.
   Input: A generic Box class that can hold any type.
   Output: The class should hold and return values of any type.

Q34. Write a Swift program to demonstrate using Async/Await for asynchronous code.
   Input: A network call using async and await.
   Output: The network call should complete asynchronously.

Q35. Write a Swift program to implement a Circular Queue using arrays.
   Input: Enqueue and dequeue operations on a circular queue.
   Output: The correct elements should be returned from the queue.

Q36. Write a Swift program to implement a Binary Search Tree with insert and search operations.
   Input: Insert elements 5, 2, 8 into the tree.
   Output: Search for element 8 and return true.

Q37. Write a Swift program to demonstrate High Order Functions such as filter, reduce, and map.
   Input: An array of integers [1, 2, 3, 4].
   Output: Use map to square the numbers, resulting in [1, 4, 9, 16].

Q38. Write a Swift program to implement a Priority Queue using a heap.
   Input: Insert elements 3, 1, 4 into the queue.
   Output: The highest priority element should be returned first.

Q39. Write a Swift program to demonstrate Memory Management with ARC (Automatic Reference Counting).
   Input: Create two objects that reference each other.
   Output: The objects should not retain each other and should be deallocated correctly.

Q40. Write a Swift program to demonstrate the use of DispatchGroup to wait for multiple async tasks.
   Input: Multiple tasks that execute asynchronously.
   Output: The program should wait for all tasks to complete before continuing.

Q41. Write a Swift program to implement Circular Linked List.
   Input: Add elements 1, 2, 3 to the list.
   Output: The list should form a circle and loop through the elements.

Q42. Write a Swift program to demonstrate the use of NSCache for caching data.
   Input: Cache a large object like an image.
   Output: The cached object should be retrieved efficiently.

Q43. Write a Swift program to implement Merge Sort algorithm.
   Input: Array [5, 2, 9, 1].
   Output: The sorted array should be [1, 2, 5, 9].

Q44. Write a Swift program to implement Quick Sort algorithm.
   Input: Array [10, 5, 2, 7].
   Output: The sorted array should be [2, 5, 7, 10].

Q45. Write a Swift program to demonstrate the use of Weak references in Swift.
   Input: A parent-child relationship where the child is weakly referenced.
   Output: The child should be deallocated when the parent is deallocated.

Q46. Write a Swift program to demonstrate the use of type(of:) to check the type of an object.
   Input: An object of type String.
   Output: The type should be String.

Q47. Write a Swift program to implement Merge functionality in a custom list.
   Input: Two arrays [1, 2] and [3, 4].
   Output: Merge them into [1, 2, 3, 4].

Q48. Write a Swift program to implement a simple Observer Pattern.
   Input: An observable object and an observer.
   Output: The observer should be notified when the observable changes.

Q49. Write a Swift program to implement a custom HashMap.
   Input: Key-value pairs ("name", "John") and ("age", "30").
   Output: The map should return the correct value when searched by key.

Q50. Write a Swift program to demonstrate the use of UIRefreshControl for pull-to-refresh functionality.
   Input: A list that is refreshed by pulling down.
   Output: The list should be refreshed when the user performs the pull-to-refresh action.

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!