Practice 50 Swift Protocols and Delegates Questions, TechnoVlogs

Practice 50 Swift Protocols and Delegates Questions


Q1. Write a Swift program to define a protocol Car with a function startEngine(). Implement this protocol in a Tesla class.
  Input: None.
  Output: "Tesla engine started" printed when the startEngine() method is called.

Q2. Write a Swift program to define a protocol Shape with a function area() that returns a Double. Implement this protocol in a Circle class.
  Input: Circle with radius 5.
  Output: Area of the circle printed as "Area: 78.54".

Q3. Write a Swift program to define a protocol Downloader with a function downloadFile(url: String) and implement it in a FileManager class.
  Input: URL string "www.example.com/file.txt".
  Output: "Downloading file from www.example.com/file.txt".

Q4. Write a Swift program to define a protocol Greeting with a method greet(name: String). Create a class Person that conforms to this protocol.
  Input: Name "John".
  Output: "Hello, John" printed.

Q5. Write a Swift program to define a delegate protocol DataDelegate with a method didReceiveData(data: String) and implement it in a DataManager class.
  Input: Data "Sample Data".
  Output: "Data received: Sample Data".

Q6. Write a Swift program to define a protocol Vehicle with a property speed. Implement this protocol in a Car class and print the speed.
  Input: Car speed of 120.
  Output: "Speed: 120".

Q7. Write a Swift program to define a protocol Calculator with a method calculate(a: Int, b: Int). Implement this protocol in a SimpleCalculator class.
  Input: a = 5, b = 10.
  Output: "Result: 15".

Q8. Write a Swift program to create a delegate protocol DataProcessorDelegate with a method didProcessData(data: String) and implement it in a Processor class.
  Input: Data "Processed Data".
  Output: "Data processed: Processed Data".

Q9. Write a Swift program to define a protocol MusicPlayer with a method play() and a stop() method. Implement this protocol in a MediaPlayer class.
  Input: None.
  Output: "Playing music" when play() is called and "Stopping music" when stop() is called.

Q10. Write a Swift program to define a protocol Task with a method executeTask(). Create a class Worker that conforms to this protocol.
   Input: None.
   Output: "Task executed" when executeTask() is called.

Q11. Write a Swift program to define a delegate protocol TaskCompletionDelegate with a method taskCompleted(success: Bool) and implement it in a TaskManager class.
   Input: success = true.
   Output: "Task completed successfully" printed.

Q12. Write a Swift program to create a protocol Downloadable with a method downloadProgress(progress: Double) and implement it in a FileDownloader class.
   Input: progress = 0.5.
   Output: "Download Progress: 50%".

Q13. Write a Swift program to define a protocol Shape with a method perimeter(). Implement this protocol in a Rectangle class.
   Input: Length = 4, Width = 5.
   Output: "Perimeter: 18".

Q14. Write a Swift program to define a delegate protocol ButtonActionDelegate with a method buttonClicked(). Implement it in a ViewController class.
   Input: Button clicked.
   Output: "Button clicked" printed.

Q15. Write a Swift program to define a protocol Game with a method startGame(). Implement it in a Tennis class.
   Input: None.
   Output: "Tennis game started" printed when startGame() is called.

Q16. Write a Swift program to define a protocol Printable with a method printDetails(). Implement this protocol in a Book class.
   Input: Book title "Swift Programming".
   Output: "Book title: Swift Programming" printed.

Q17. Write a Swift program to define a delegate protocol DownloadDelegate with a method downloadDidFinish(). Implement it in a DownloadManager class.
   Input: None.
   Output: "Download finished" printed.

Q18. Write a Swift program to define a protocol Pet with a method play(). Implement it in a Dog class.
   Input: None.
   Output: "Dog is playing" printed when play() is called.

Q19. Write a Swift program to define a protocol Account with a method checkBalance(). Implement it in a BankAccount class.
   Input: None.
   Output: "Your balance is $1000" printed.

Q20. Write a Swift program to define a delegate protocol OrderDelegate with a method didPlaceOrder(orderId: String). Implement it in a Shop class.
   Input: Order ID "12345".
   Output: "Order placed: 12345".

Q21. Write a Swift program to define a protocol Sound with a method makeSound(). Implement it in a Cat class.
   Input: None.
   Output: "Meow" printed when makeSound() is called.

Q22. Write a Swift program to define a delegate protocol LoginDelegate with a method didLoginSuccessfully(). Implement it in a LoginViewController class.
   Input: None.
   Output: "Login Successful" printed when the method is called.

Q23. Write a Swift program to define a protocol Rideable with a method ride(). Implement it in a Bicycle class.
   Input: None.
   Output: "Riding the bicycle" printed when ride() is called.

Q24. Write a Swift program to define a protocol TaskDelegate with a method taskDidStart(). Implement it in a TaskExecutor class.
   Input: None.
   Output: "Task has started" printed when the method is called.

Q25. Write a Swift program to define a delegate protocol AlarmDelegate with a method didTriggerAlarm(). Implement it in a SecuritySystem class.
   Input: None.
   Output: "Alarm triggered!" printed.

Q26. Write a Swift program to define a protocol Alertable with a method sendAlert(). Implement it in a NotificationSystem class.
   Input: None.
   Output: "Alert sent" printed when sendAlert() is called.

Q27. Write a Swift program to define a protocol TaskRunnable with a method runTask(). Implement it in a ServerTask class.
   Input: None.
   Output: "Task is running" printed.

Q28. Write a Swift program to define a protocol Messenger with a method sendMessage(). Implement it in a TextMessenger class.
   Input: Message "Hello".
   Output: "Message sent: Hello".

Q29. Write a Swift program to define a protocol Storage with a method storeData(data: String). Implement it in a Database class.
   Input: Data "User Data".
   Output: "Data stored: User Data".

Q30. Write a Swift program to define a delegate protocol FileTransferDelegate with a method didTransferFile(success: Bool). Implement it in a FileTransferManager class.
   Input: success = true.
   Output: "File transfer successful" printed.

Q31. Write a Swift program to define a protocol UserInfo with a method getUserName(). Implement it in a User class.
   Input: None.
   Output: "User name: John Doe" printed.

Q32. Write a Swift program to define a protocol Device with a method turnOn(). Implement it in a TV class.
   Input: None.
   Output: "TV turned on" printed.

Q33. Write a Swift program to define a protocol Recordable with a method startRecording(). Implement it in a Camera class.
   Input: None.
   Output: "Recording started" printed when startRecording() is called.

Q34. Write a Swift program to define a delegate protocol UploadDelegate with a method didUploadFile(success: Bool). Implement it in a FileUploader class.
   Input: success = false.
   Output: "File upload failed" printed.

Q35. Write a Swift program to define a protocol WeatherData with a method fetchWeatherData(). Implement it in a WeatherService class.
   Input: None.
   Output: "Weather data fetched" printed.

Q36. Write a Swift program to define a protocol Movable with a method move(). Implement it in a Robot class.
   Input: None.
   Output: "Robot is moving" printed when move() is called.

Q37. Write a Swift program to define a delegate protocol DownloadProgressDelegate with a method downloadProgressUpdated(progress: Double). Implement it in a DownloadManager class.
   Input: progress = 0.75.
   Output: "Download Progress: 75%" displayed.

Q38. Write a Swift program to define a protocol OrderDelegate with a method orderConfirmed(). Implement it in a FoodOrder class.
   Input: None.
   Output: "Order confirmed" printed.

Q39. Write a Swift program to define a protocol Driver with a method drive(). Implement it in a BusDriver class.
   Input: None.
   Output: "Bus is driving" printed.

Q40. Write a Swift program to define a protocol Notification with a method sendNotification(message: String). Implement it in a MessageNotification class.
   Input: Message "New message received".
   Output: "Notification sent: New message received".

Q41. Write a Swift program to define a delegate protocol TaskCompletionDelegate with a method taskDidFinish(). Implement it in a JobProcessor class.
   Input: None.
   Output: "Task finished" printed when the method is called.

Q42. Write a Swift program to define a protocol Alertable with a method sendAlert(). Implement it in a SystemAlert class.
   Input: None.
   Output: "Alert sent" printed when sendAlert() is called.

Q43. Write a Swift program to define a protocol MoviePlayer with a method playMovie(). Implement it in a StreamingPlayer class.
   Input: None.
   Output: "Movie is playing" printed.

Q44. Write a Swift program to define a protocol AudioControl with a method adjustVolume(level: Int). Implement it in a Speaker class.
   Input: level = 10.
   Output: "Volume set to 10".

Q45. Write a Swift program to define a protocol Database with a method fetchData(). Implement it in a SQLDatabase class.
   Input: None.
   Output: "Data fetched from SQL Database".

Q46. Write a Swift program to define a delegate protocol DownloadDelegate with a method didCompleteDownload(). Implement it in a FileDownloadManager class.
   Input: None.
   Output: "Download complete" printed.

Q47. Write a Swift program to define a protocol Worker with a method performTask(). Implement it in a FactoryWorker class.
   Input: None.
   Output: "Task performed" printed.

Q48. Write a Swift program to define a protocol Colorable with a method setColor(). Implement it in a Car class.
   Input: None.
   Output: "Car color set to red".

Q49. Write a Swift program to define a protocol Storage with a method storeData(data: String). Implement it in a CloudStorage class.
   Input: Data "Backup Data".
   Output: "Data stored in cloud: Backup Data".

Q50. Write a Swift program to define a protocol AudioPlayer with a method playAudio(). Implement it in a MediaPlayer class.
   Input: None.
   Output: "Audio is playing" printed.

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!