Swift Aptitude Test

This post presents a Swift programming aptitude test with 25 multiple-choice questions for beginners. These beginner-level MCQ questions cover essential aspects of Swift programming and are designed to test fundamental understanding and basic syntax.

1. What is the correct syntax to declare a constant in Swift?

a) let constant = 10
b) const constant = 10
c) var constant = 10
d) constant let = 10

2. Which data type is used to store a non-optional floating-point number?

a) Double?
b) Float
c) Double
d) Int

3. How do you create a dictionary with string keys and integer values in Swift?

a) [String, Int]()
b) [String: Int]()
c) Dictionary<String, Int>()
d) Dictionary[String, Int]()

4. What is the output of print(“Swift”.count)?

a) 4
b) 5
c) Error
d) None of the above

5. How do you check for equality between two strings in Swift?

a) ==
b) ===
c) isEqual
d) equals()

6. Which keyword is used to handle exceptions in Swift?

a) catch
b) try
c) throw
d) error

7. What is the default access level of classes in Swift?

a) public
b) internal
c) private
d) open

8. What does Optional mean in Swift?

a) The variable can only store integer values
b) The variable may or may not have a value
c) The variable must have a value
d) The variable cannot have a value

9. Which of these is the correct way to declare an array of integers in Swift?

a) int[] numbers = [1, 2, 3]
b) var numbers: [Int] = [1, 2, 3]
c) [Int] numbers = [1, 2, 3]
d) array<Int> numbers = [1, 2, 3]

10. What is a closure in Swift?

a) A special keyword to close the app
b) A constant value
c) A self-contained block of functionality that can be passed around
d) An error-handling mechanism

11. Which operator can unwrap an optional if it contains a value or else provide a default value?

a) ??
b) ::
c) !!
d) &&

12. How do you define a computed property in Swift?

a) Using the var keyword and providing a getter and setter
b) Using the let keyword and providing a getter and setter
c) Using the compute keyword
d) It is not possible in Swift

13. What is the purpose of the weak keyword in Swift?

a) It declares a variable that cannot be changed
b) It prevents strong reference cycles in closures
c) It creates a weak reference to an object
d) It makes the variable accessible only within the class

14. What are protocols in Swift?

a) A method to send data between servers
b) A blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality
c) Error handling mechanisms
d) A type of closure

15. How is type safety enforced in Swift?

a) By using strong typing, where the type of every variable must be declared at compile time
b) Through dynamic typing, where the type of a variable can change at runtime
c) Swift is not a type-safe language
d) By using typecasting exclusively

16. What is the implicit return feature in Swift functions?

a) Functions can automatically return the last value in their body
b) All functions return nil by default
c) Functions need to specify return type always
d) There is no such feature

17. Which control structure allows you to execute a block of code multiple times based on a condition at the beginning?

a) repeat-while
b) for-in
c) while
d) switch

18. What is an enum in Swift?

a) A function that returns an enumerator
b) A type that represents a group of related values and enables you to work with those values in a type-safe way
c) A protocol that defines enumerations
d) A method to loop through elements

19. How do you handle errors in Swift?

a) Using error codes
b) Using try, catch blocks
c) Using on-error
d) Using fault blocks

20. What does the dynamic keyword do in Swift?

a) Makes the property change dynamically
b) Allows Swift to interoperate with Objective-C runtime
c) Creates dynamic data structures
d) None of the above

21. How do you declare a method in Swift that can be overridden by a subclass?

a) Prefix the method with the final keyword
b) Prefix the method with the static keyword
c) Prefix the method with the open keyword
d) All methods are overridable by default

22. Which statement about Swift’s type inference is correct?

a) Swift cannot infer types, they must always be explicitly declared
b) Swift can infer types based on assigned values
c) Swift uses type inference only in functions
d) Swift’s type inference is optional and rarely used

23. What is the result of the expression 10.0 / 4 in Swift?

a) 2
b) 2.5
c) 2.0
d) Error

24. Which pattern is commonly used in Swift to pass data between view controllers?

a) Delegate pattern
b) Observer pattern
c) Singleton pattern
d) Factory pattern

25. What does the @escaping attribute do in Swift?

a) It indicates that a closure escapes a function to be executed later
b) It prevents a closure from escaping the function
c) It marks variables that escape their scope
d) It optimizes memory usage for closures

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top