0
0
Swiftprogramming~20 mins

Assertions (XCTAssertEqual, XCTAssertTrue) in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XCTest Assertions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this XCTest assertion?
Consider the following Swift test code snippet using XCTest assertions. What will happen when this test runs?
Swift
func testSum() {
    let result = 2 + 3
    XCTAssertEqual(result, 5)
}
AThe test passes without any failure.
BThe test fails with an assertion failure because 2 + 3 is not equal to 5.
CThe test causes a runtime error due to incorrect assertion usage.
DThe test is skipped because XCTAssertEqual is not a valid assertion.
Attempts:
2 left
💡 Hint
XCTAssertEqual checks if two values are equal and passes if they are.
Predict Output
intermediate
2:00remaining
What happens when XCTAssertTrue fails?
Look at this Swift test function. What is the result when it runs?
Swift
func testIsEven() {
    let number = 3
    XCTAssertTrue(number % 2 == 0)
}
AThe test fails with an assertion failure because 3 is not even.
BThe test is ignored because XCTAssertTrue is deprecated.
CThe test causes a compile-time error due to invalid syntax.
DThe test passes because 3 is an even number.
Attempts:
2 left
💡 Hint
XCTAssertTrue expects the condition to be true to pass.
🔧 Debug
advanced
2:00remaining
Identify the error in this XCTest assertion usage
This Swift test code is intended to check if two strings are equal. What error will occur when running this test?
Swift
func testGreeting() {
    let greeting = "Hello"
    XCTAssertEqual(greeting, "hello")
}
AThe test causes a runtime crash due to type mismatch.
BThe test fails because XCTAssertEqual is case-sensitive and "Hello" != "hello".
CThe test passes because XCTAssertEqual ignores case differences.
DThe test causes a compile-time error due to missing import XCTest.
Attempts:
2 left
💡 Hint
XCTAssertEqual compares values exactly, including case for strings.
📝 Syntax
advanced
2:00remaining
Which option contains a syntax error in XCTest assertion?
Which of the following Swift test code snippets will cause a syntax error?
AXCTAssertEqual(5, 5)
BXCTAssertTrue(3 > 2)
CXCTAssertEqual(4, 4 else { return })
DXCTAssertTrue(true)
Attempts:
2 left
💡 Hint
Check for misplaced keywords or missing parentheses.
🚀 Application
expert
3:00remaining
How many assertions will fail in this test function?
Analyze the following Swift test function. How many assertions will fail when this test runs?
Swift
func testMultipleAssertions() {
    XCTAssertEqual(10, 10)
    XCTAssertTrue(5 > 10)
    XCTAssertEqual("Swift", "swift")
    XCTAssertTrue(3 == 3)
}
A1
B0
C3
D2
Attempts:
2 left
💡 Hint
Check each assertion's condition carefully.