0
0
iOS Swiftmobile~20 mins

Test-driven development in Swift in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift TDD Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is the main purpose of writing tests before code in TDD?
In Test-driven development (TDD), why do developers write tests before writing the actual code?
ATo avoid writing any production code
BTo make the app run faster
CTo delay coding until all tests are ready
DTo define the expected behavior and guide the implementation
Attempts:
2 left
💡 Hint
Think about how tests help shape the code you write.
ui_behavior
intermediate
1:30remaining
What happens when a failing test is run in Xcode during TDD?
You write a test for a new feature in Swift and run it in Xcode, but the feature code is not implemented yet. What will you see?
AThe test fails, showing a red indicator in the test navigator
BThe test passes automatically
CThe app UI updates to show the feature
DXcode crashes
Attempts:
2 left
💡 Hint
Think about what happens when code does not meet test expectations.
lifecycle
advanced
2:00remaining
What is the correct TDD cycle order?
Arrange the steps of the Test-driven development cycle in the correct order.
A2,1,3,4
B1,2,3,4
C1,3,2,4
D3,1,2,4
Attempts:
2 left
💡 Hint
Remember the mantra: Red, Green, Refactor.
📝 Syntax
advanced
1:30remaining
What is the output of this Swift test code snippet?
Consider this Swift test function: func testSum() { let result = sum(2, 3) XCTAssertEqual(result, 6) } Assuming sum adds two numbers, what will happen when this test runs?
iOS Swift
func sum(_ a: Int, _ b: Int) -> Int {
  return a + b
}
AThe test is skipped automatically
BThe test passes successfully
CThe test fails because 2 + 3 is 5, not 6
DThe test causes a runtime error
Attempts:
2 left
💡 Hint
Check the expected value in XCTAssertEqual.
🔧 Debug
expert
2:00remaining
Why does this Swift test crash with 'fatal error: unexpectedly found nil while unwrapping an Optional'?
Given this test code: func testUserName() { let user: User? = nil XCTAssertEqual(user!.name, "Alice") } Why does this test crash instead of failing?
ABecause user is nil and force-unwrapping it causes a runtime crash
BBecause XCTAssertEqual cannot compare Optional values
CBecause the test function is missing @testable import
DBecause the name property is not defined in User
Attempts:
2 left
💡 Hint
Focus on the exclamation mark after user.