Skip to content
- What is the default access level for a variable in Swift?
- A) Private
- B) Internal
- C) Public
- D) Fileprivate
- Which of the following is a correct way to declare a constant in Swift?
- A) var myConstant = 10
- B) constant myConstant = 10
- C) let myConstant = 10
- D) const myConstant = 10
- How do you create an array in Swift?
- A) var myArray = [1, 2, 3]
- B) var myArray : Array = 1, 2, 3
- C) var myArray = new Array(1, 2, 3)
- D) var myArray = (1, 2, 3)
- Which keyword is used to create a function in Swift?
- A) func
- B) function
- C) define
- D) method
- How do you define a tuple in Swift?
- A) var myTuple = (1, “hello”)
- B) var myTuple : (Int, String) = (1, “hello”)
- C) var myTuple = [1, “hello”]
- D) Both A and B
- Which of the following types is not a value type in Swift?
- A) Array
- B) Dictionary
- C) Struct
- D) Class
- How do you declare an optional variable in Swift?
- A) var name: ?
- B) var name: Optional<String>
- C) var name: String?
- D) var name: String!
- What does the
guard statement do in Swift?
- A) It creates a loop.
- B) It provides early exit for functions.
- C) It checks conditions.
- D) Both B and C
- How do you concatenate two strings in Swift?
- A) string1 + string2
- B) string1.append(string2)
- C) string1.concat(string2)
- D) Both A and B
- How do you create a dictionary in Swift?
- A) var myDict = [“key”: “value”]
- B) var myDict = {“key”: “value”}
- C) var myDict = new Dictionary([“key”: “value”])
- D) var myDict : Dictionary<String, String>
- What is the correct way to handle exceptions in Swift?
- A) try-catch
- B) try-catch-finally
- C) throw-try-catch
- D) do-catch
- Which of the following correctly defines a struct in Swift?
- A) struct MyStruct {}
- B) class MyStruct {}
- C) MyStruct() {}
- D) type MyStruct {}
- What is a typealias used for in Swift?
- A) To define new data structures
- B) To create an alias for an existing type
- C) To create protocol definitions
- D) To indicate variable type
- Which of the following methods is used to initialize a class in Swift?
- A) init
- B) initialize
- C) start
- D) begin
- How do you cast a variable to a different type in Swift?
- A) as
- B) type
- C) cast
- D) convert
- Which protocol must be adopted to make a class conform to a delegate?
- A) NSObjectProtocol
- B) DelegateProtocol
- C) AnyObject
- D) CustomProtocol
- What is the output type of the
map function on an array?
- A) Array
- B) Dictionary
- C) Set
- D) String
- How do you declare a computed property in Swift?
- A) var property: Int { return 0 }
- B) var property: Int = { return 0 }
- C) var property: Int { get { return 0 } }
- D) var property: Int { set { return 0 } }
- Which of the following is used to define an enumeration in Swift?
- A) enum MyEnum {}
- B) enumeration MyEnum {}
- C) define MyEnum {}
- D) MyEnum {}
- What is the purpose of the
@objc attribute in Swift?
- A) To define properties
- B) To expose Swift code to Objective-C
- C) To declare classes
- D) To specify method visibility
- What keyword is used to inherit from a class in Swift?
- A) extends
- B) inherits
- C) override
- D) : (colon)
- How can you declare a variable that cannot be mutated in Swift?
- A) var
- B) let
- C) const
- D) final
- Which of the following functions is used to create an instance of a closure in Swift?
- A) closure{}
- B) {()->Type in }
- C) () -> Type
- D) { }
- What is the output of
nil in Swift?
- A) Null
- B) Blank
- C) Undefined
- D) None
- Which of the following is the correct way to create a set in Swift?
- A) var mySet: Set<Int> = [1, 2, 3]
- B) var mySet = {1, 2, 3}
- C) var mySet: Set = [1, 2, 3]
- D) var mySet: Array = [1, 2, 3]
- What is the keyword used to define a protocol in Swift?
- A) interface
- B) protocol
- C) abstract
- D) class
- Which of the following statements is true about
lazy properties in Swift?
- A) They are initialized when accessed.
- B) They are initialized at compile time.
- C) They are always mutable.
- D) They cannot be modified.
- What is the result of the expression
2.5 <= 2.5 in Swift?
- A) True
- B) False
- C) Undefined
- D) Error
- What symbol is used for commenting in Swift?
- How do you create a singleton class in Swift?
- A) Using static properties
- B) Using final classes
- C) Using single instances
- D) Both A and B
- Which of the following is a Swift collection type?
- A) Stack
- B) Queue
- C) Array
- D) Linked List
- What does the keyword
final mean when used with a class in Swift?
- A) The class cannot be subclassed.
- B) The class is immutable.
- C) The class is private.
- D) The class cannot have properties.
- How can you define a generic function in Swift?
- A) func myFunction<Type>(param: Type) {}
- B) func myFunction<T>(param: T) {}
- C) function myFunction<T>(param: T) {}
- D) Both A and B
- How are multi-line strings declared in Swift?
- What is the main purpose of the
deinit method in Swift?
- A) To initialize a class
- B) To handle cleanup before an instance is deallocated
- C) To create a new instance
- D) To override a method
- Which of the following compares two strings for equality in Swift?
- A) ==
- B) =
- C) isEqual
- D) compare
- Which of the following defines a closure type in Swift?
- A) () -> Void
- B) func() -> Void
- C) Void {}
- D) closure()
- What is the purpose of using
typealias in Swift?
- A) To create a variable type
- B) To create shorthand for complex type definitions
- C) To define function types
- D) All of the above
- How do you access the last item of an array in Swift?
- A) array.last
- B) array[-1]
- C) array[array.count – 1]
- D) Both A and C
- Which keyword is used to indicate a property observer in Swift?
- A) watch
- B) observe
- C) didSet
- D) willSet
- How can you create an instance of an enumeration in Swift?
- A) let value = MyEnum.case1
- B) let value = MyEnum(case1)
- C) var value = MyEnum::case1
- D) enum value = MyEnum.case1
- What will happen if you try to access an index out of bounds in an array in Swift?
- A) Fatal error
- B) Returns nil
- C) No error, returns default value
- D) The program exits gracefully
- Which of the following is a way to use Swift’s optional binding?
- A) if let
- B) while let
- C) switch let
- D) both A and B
- What does the
self keyword refer to in a class?
- A) The parent class
- B) The current instance of the class
- C) The superclass
- D) The delegate of the class
- How do you define a variable that is only set once in Swift?
- A) let
- B) var
- C) const
- D) final
- Which of the following is the correct way to handle asynchronous tasks in Swift?
- A) DispatchQueue
- B) async/await
- C) Both A and B
- D) Thread()
- What does
@escaping mean in a closure?
- A) The closure will not be retained.
- B) The closure can be called after the function returns.
- C) The closure is synchronous.
- D) The closure is optional.
- How do you define a static property in Swift?
- A) static var myVar: Int
- B) var myVar: static Int
- C) let myVar: Int = 0
- D) final var myVar: Int
- Which of the following is NOT a valid way to initialize an array in Swift?
- A) var myArray = Int
- B) var myArray: [Int] = []
- C) var myArray = Array(1, 2, 3)
- D) var myArray = Array(repeating: 0, count: 5)
- What type of values can enums in Swift contain?
- A) Only integers
- B) Strings only
- C) Any type
- D) Only unique values