0
0
iOS Swiftmobile~20 mins

URLSession basics in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
URLSession Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when you call resume() on a URLSessionDataTask?
You create a URLSessionDataTask but forget to call resume(). What will happen?
AThe task will never start and no data will be fetched.
BThe app will crash with a runtime error.
CThe task will start immediately and fetch data.
DThe task will start but complete with an error.
Attempts:
2 left
💡 Hint
Think about what resume() does for a URLSessionDataTask.
🧠 Conceptual
intermediate
2:00remaining
What is the role of the completion handler in URLSession dataTask?
Why do we provide a completion handler when creating a dataTask with URLSession?
ATo configure the request headers before sending.
BTo automatically retry the request on failure.
CTo cancel the task if it takes too long.
DTo handle the response data, response, and error after the request finishes.
Attempts:
2 left
💡 Hint
Think about what happens after the network call completes.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly creates and starts a URLSession data task?
Select the code that correctly creates a data task to fetch data from a URL and starts it.
iOS Swift
let url = URL(string: "https://example.com")!
let task = URLSession.shared.dataTask(with: url) { data, response, error in
  // handle response
}
// What is missing here?
Atask.run()
Btask.resume()
Ctask.begin()
Dtask.start()
Attempts:
2 left
💡 Hint
Look for the method that starts the task.
lifecycle
advanced
2:00remaining
What happens if you call resume() twice on the same URLSessionDataTask?
Consider this code: let task = URLSession.shared.dataTask(with: url) { data, response, error in // handle response } task.resume() task.resume() What is the effect of calling resume() twice?
AThe task runs twice and fetches data two times.
BThe app crashes with an exception.
CThe second resume() call has no effect; the task runs only once.
DThe task cancels after the second resume() call.
Attempts:
2 left
💡 Hint
Think about the task state after the first resume().
🔧 Debug
expert
2:00remaining
Why does this URLSession dataTask completion handler never run?
Look at this code: let url = URL(string: "https://example.com")! let task = URLSession.shared.dataTask(with: url) { data, response, error in print("Response received") } // Missing line here Why does "Response received" never print?
ABecause the task was never started with resume().
BBecause the URL is invalid and the task fails silently.
CBecause the completion handler is not called on the main thread.
DBecause the URLSession.shared is nil.
Attempts:
2 left
💡 Hint
Check if the task is started after creation.