programming beginner 10 sample questions

Swift MCQ Practice Test

A modern programming language by Apple for iOS and macOS app development.

Q1. What is the primary purpose of string interpolation in Swift?

Explanation: String interpolation allows you to embed variables and expressions directly within a string literal. This is achieved using backslashes and parentheses, such as `"The value is \(variable)"`.

Q2. What character is used to escape special characters, such as a double quote, within a Swift string literal?

Explanation: The backslash '' character is used to escape special characters within a Swift string literal. For example, to include a double quote character (") within a string, you would use ".

Q3. What is the purpose of the ".map" function in a Swift array?

Explanation: The ".map" function in Swift is used to create a new array by transforming each element in the original array. It applies a given transformation function to each element in the array, returning a new array with the transformed elements.

Q4. What is the purpose of the '@discardableResult' attribute in Swift?

Explanation: The '@discardableResult' attribute in Swift is used to suppress the return value of a function, which allows the caller to ignore the return value without having to explicitly use the '_ = ' syntax.

Q5. What is the purpose of the "'self' keyword in a Swift closure?

Explanation: The "self' keyword in Swift is used to capture the enclosing function's context within a closure. This allows the closure to access and modify properties and methods of the surrounding scope.

Q6. What is the purpose of the ".self" keyword in Swift?

Explanation: In Swift, the ".self" keyword is used to refer to the current type, which is useful in various contexts such as type casting, generic type inference, and more.

Q7. What is the purpose of the '!' character when used with an optional in Swift?

Explanation: The '!' character is used to force unwrap an optional. If the optional contains a value, the value is unwrapped and used. However, if the optional is nil, a runtime error (program crash) will occur.

Q8. What is the purpose of the \\@escaping keyword in a Swift closure?

Explanation: The \\@escaping keyword is used to indicate that a closure captures values that may outlive the function that called it, allowing the closure to be executed after the function has returned.

Q9. In Swift, which syntax is used for string interpolation - embedding a value inside a string literal?

Explanation: Swift interpolates with backslash-parentheses syntax: "The sum is \(x + y)". $value is not interpolation syntax in Swift (the $ appears in closure shorthand arguments like $0). The original stem asked about a nonexistent '$ operator'.

Q10. What is the purpose of the "if let" statement in Swift?

Explanation: The "if let" statement in Swift is used to safely unwrap an optional value without forcing it, which helps prevent runtime errors.

That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.

Sign Up Free
Swift MCQ Practice Test | Mind Hustle