Practice 50 Swift Networking Programming Questions, TechnoVlogs

Practice 50 Swift Networking Programming Questions


Q1. Write a Swift program to send a GET request to a URL and print the response.
  Input: URL "https://jsonplaceholder.typicode.com/posts".
  Output: JSON response printed.

Q2. Write a Swift program to send a POST request with a JSON payload to a URL.
  Input: URL "https://jsonplaceholder.typicode.com/posts", payload { "title": "foo", "body": "bar", "userId": 1 }.
  Output: JSON response confirming the POST request.

Q3. Write a Swift program to fetch JSON data from a URL and parse it into a model object.
  Input: URL "https://jsonplaceholder.typicode.com/posts".
  Output: Parsed model object printed with attributes like title, body.

Q4. Write a Swift program to handle errors in a network request using do-catch.
  Input: URL "https://jsonplaceholder.typicode.com/invalidendpoint".
  Output: Error message printed.

Q5. Write a Swift program to make a GET request with URL parameters and display the response.
  Input: URL "https://jsonplaceholder.typicode.com/posts?id=1".
  Output: JSON response printed with specific post data.

Q6. Write a Swift program to send a PUT request to update data on a server and print the response.
  Input: URL "https://jsonplaceholder.typicode.com/posts/1", payload { "id": 1, "title": "updated", "body": "updated body" }.
  Output: JSON response confirming the update.

Q7. Write a Swift program to download an image from a URL and display it in an UIImageView.
  Input: URL "https://via.placeholder.com/150".
  Output: Image displayed in an UIImageView.

Q8. Write a Swift program to send a DELETE request to remove data from a server.
  Input: URL "https://jsonplaceholder.typicode.com/posts/1".
  Output: JSON response confirming the deletion.

Q9. Write a Swift program to handle network timeouts using URLSession.
  Input: URL "https://jsonplaceholder.typicode.com/posts", simulate a timeout.
  Output: Timeout error message printed.

Q10. Write a Swift program to check if the device has an active internet connection before making a network request.
   Input: None.
   Output: "No internet connection" if the device is offline.

Q11. Write a Swift program to download data asynchronously using URLSession and print the result once downloaded.
   Input: URL "https://jsonplaceholder.typicode.com/posts".
   Output: "Data downloaded" and JSON data printed.

Q12. Write a Swift program to fetch data from an API, decode it into an array of objects, and print the first object.
   Input: URL "https://jsonplaceholder.typicode.com/posts".
   Output: First post object printed with userId, id, title, and body.

Q13. Write a Swift program to make a network request to a REST API with authentication headers.
   Input: URL "https://api.example.com/data", headers { "Authorization": "Bearer <token>" }.
   Output: JSON response printed after authentication.

Q14. Write a Swift program to upload an image to a server using a POST request.
   Input: URL "https://example.com/upload", image file.
   Output: Response message confirming image upload.

Q15. Write a Swift program to parse a JSON response with nested data structures.
   Input: URL "https://jsonplaceholder.typicode.com/users".
   Output: Nested data structure printed (user name, address, etc.).

Q16. Write a Swift program to handle JSON response errors gracefully when decoding data.
   Input: Invalid JSON URL "https://jsonplaceholder.typicode.com/posts/invalid".
   Output: Error message printed.

Q17. Write a Swift program to implement basic error handling in network requests using Result.
   Input: URL "https://jsonplaceholder.typicode.com/posts".
   Output: Success or failure result printed based on the response.

Q18. Write a Swift program to send a request to an API with multiple query parameters.
   Input: URL "https://jsonplaceholder.typicode.com/posts?userId=1&title=foo".
   Output: Filtered JSON response printed.

Q19. Write a Swift program to create a custom URLSession to handle network requests.
   Input: URL "https://jsonplaceholder.typicode.com/posts".
   Output: Data fetched via custom URLSession.

Q20. Write a Swift program to implement basic caching of network responses.
   Input: URL "https://jsonplaceholder.typicode.com/posts".
   Output: Response is cached and reused in subsequent requests.

Q21. Write a Swift program to fetch data from a server and parse it using Codable.
   Input: URL "https://jsonplaceholder.typicode.com/posts".
   Output: JSON data decoded into a list of model objects.

Q22. Write a Swift program to handle 404 errors when making a network request.
   Input: URL "https://jsonplaceholder.typicode.com/invalidendpoint".
   Output: "Error 404: Not Found".

Q23. Write a Swift program to handle a 500 internal server error gracefully.
   Input: URL "https://jsonplaceholder.typicode.com/500".
   Output: "Server error: 500".

Q24. Write a Swift program to download and display data from a URL in a table view.
   Input: URL "https://jsonplaceholder.typicode.com/posts".
   Output: Data displayed in a table view.

Q25. Write a Swift program to send a POST request with form data.
   Input: URL "https://example.com/form", form data { "name": "John", "email": "john@example.com" }.
   Output: Form submission confirmation printed.

Q26. Write a Swift program to handle network response using closures.
   Input: URL "https://jsonplaceholder.typicode.com/posts".
   Output: Response data printed using a closure.

Q27. Write a Swift program to upload a file to a server using multipart/form-data encoding.
   Input: URL "https://example.com/upload", file data.
   Output: Response confirming the file upload.

Q28. Write a Swift program to use URLSessionDownloadTask for downloading large files.
   Input: URL "https://example.com/largefile.zip".
   Output: File downloaded to the local device.

Q29. Write a Swift program to handle network requests using async and await keywords.
   Input: URL "https://jsonplaceholder.typicode.com/posts".
   Output: Data fetched asynchronously.

Q30. Write a Swift program to make a network request with basic authentication.
   Input: URL "https://example.com", headers { "Authorization": "Basic <base64_encoded_credentials>" }.
   Output: JSON response after basic authentication.

Q31. Write a Swift program to handle a slow network connection and show a loading spinner while fetching data.
   Input: URL "https://jsonplaceholder.typicode.com/posts".
   Output: Loading spinner displayed while data is being fetched.

Q32. Write a Swift program to parse a JSON response into a Codable struct and display the results.
   Input: URL "https://jsonplaceholder.typicode.com/posts".
   Output: List of posts printed with details.

Q33. Write a Swift program to send a POST request with a JSON body and print the response.
   Input: URL "https://jsonplaceholder.typicode.com/posts", JSON { "title": "new title", "body": "new body" }.
   Output: JSON response confirming the POST request.

Q34. Write a Swift program to download a file asynchronously and show progress updates.
   Input: URL "https://example.com/largefile.zip".
   Output: Progress bar updates and file download completion.

Q35. Write a Swift program to send a network request to fetch user details and display them in a label.
   Input: URL "https://jsonplaceholder.typicode.com/users/1".
   Output: User details displayed (name, username, etc.).

Q36. Write a Swift program to make a network request with a custom header.
   Input: URL "https://jsonplaceholder.typicode.com/posts", custom header { "X-Custom-Header": "value" }.
   Output: Response printed with custom header included.

Q37. Write a Swift program to show a success or failure message based on network request completion.
   Input: URL "https://jsonplaceholder.typicode.com/posts".
   Output: "Request successful" or "Request failed" printed.

Q38. Write a Swift program to implement pagination by making multiple network requests.
   Input: URL "https://jsonplaceholder.typicode.com/posts?page=1".
   Output: Data for the current page displayed.

Q39. Write a Swift program to create a network manager class to handle multiple requests.
   Input: URL "https://jsonplaceholder.typicode.com/posts".
   Output: Data fetched using network manager.

Q40. Write a Swift program to handle an invalid URL error in a network request.
   Input: Invalid URL "https://invalidurl".
   Output: "Invalid URL error" message printed.

Q41. Write a Swift program to upload a file with metadata using URLSession.
   Input: URL "https://example.com/upload", file data and metadata.
   Output: Response confirming the file upload with metadata.

Q42. Write a Swift program to use URLSession with a custom HTTPMethod.
   Input: URL "https://jsonplaceholder.typicode.com/posts", HTTPMethod "DELETE".
   Output: Response confirming the DELETE request.

Q43. Write a Swift program to use URLSession to download a large JSON file in chunks.
   Input: URL "https://example.com/largefile.json".
   Output: File downloaded in chunks.

Q44. Write a Swift program to send a network request to fetch the latest weather data and display it.
   Input: URL "https://api.openweathermap.org/data/2.5/weather?q=London".
   Output: Weather data printed.

Q45. Write a Swift program to send a network request with multiple query parameters and print the response.
   Input: URL "https://jsonplaceholder.typicode.com/posts?userId=1&title=foo".
   Output: Filtered posts printed.

Q46. Write a Swift program to parse a complex JSON response into a model with multiple arrays and objects.
   Input: URL "https://jsonplaceholder.typicode.com/users".
   Output: Parsed JSON with user details.

Q47. Write a Swift program to implement background network downloads with URLSession.
   Input: URL "https://example.com/largefile.zip".
   Output: File downloaded in the background.

Q48. Write a Swift program to send a GET request with URL-encoded parameters and handle the response.
   Input: URL "https://jsonplaceholder.typicode.com/posts?userId=1".
   Output: Filtered posts for the specific user.

Q49. Write a Swift program to check network status and display appropriate messages.
   Input: None.
   Output: "No network connection" or "Connected to the internet".

Q50. Write a Swift program to download and parse JSON data from a URL using async and await.
   Input: URL "https://jsonplaceholder.typicode.com/posts".
   Output: JSON data parsed and printed asynchronously.

Share on Social Media