MCQ Related “Swift”

  1. What is the default access level for a variable in Swift?
    • A) Private
    • B) Internal
    • C) Public
    • D) Fileprivate
  2. 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
  3. 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)
  4. Which keyword is used to create a function in Swift?
    • A) func
    • B) function
    • C) define
    • D) method
  5. 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
  6. Which of the following types is not a value type in Swift?
    • A) Array
    • B) Dictionary
    • C) Struct
    • D) Class
  7. 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!
  8. 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
  9. How do you concatenate two strings in Swift?
    • A) string1 + string2
    • B) string1.append(string2)
    • C) string1.concat(string2)
    • D) Both A and B
  10. 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>
  11. What is the correct way to handle exceptions in Swift?
    • A) try-catch
    • B) try-catch-finally
    • C) throw-try-catch
    • D) do-catch
  12. Which of the following correctly defines a struct in Swift?
    • A) struct MyStruct {}
    • B) class MyStruct {}
    • C) MyStruct() {}
    • D) type MyStruct {}
  13. 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
  14. Which of the following methods is used to initialize a class in Swift?
    • A) init
    • B) initialize
    • C) start
    • D) begin
  15. How do you cast a variable to a different type in Swift?
    • A) as
    • B) type
    • C) cast
    • D) convert
  16. Which protocol must be adopted to make a class conform to a delegate?
    • A) NSObjectProtocol
    • B) DelegateProtocol
    • C) AnyObject
    • D) CustomProtocol
  17. What is the output type of the map function on an array?
    • A) Array
    • B) Dictionary
    • C) Set
    • D) String
  18. 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 } }
  19. Which of the following is used to define an enumeration in Swift?
    • A) enum MyEnum {}
    • B) enumeration MyEnum {}
    • C) define MyEnum {}
    • D) MyEnum {}
  20. 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
  21. What keyword is used to inherit from a class in Swift?
    • A) extends
    • B) inherits
    • C) override
    • D) : (colon)
  22. How can you declare a variable that cannot be mutated in Swift?
    • A) var
    • B) let
    • C) const
    • D) final
  23. Which of the following functions is used to create an instance of a closure in Swift?
    • A) closure{}
    • B) {()->Type in }
    • C) () -> Type
    • D) { }
  24. What is the output of nil in Swift?
    • A) Null
    • B) Blank
    • C) Undefined
    • D) None
  25. 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]
  26. What is the keyword used to define a protocol in Swift?
    • A) interface
    • B) protocol
    • C) abstract
    • D) class
  27. 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.
  28. What is the result of the expression 2.5 <= 2.5 in Swift?
    • A) True
    • B) False
    • C) Undefined
    • D) Error
  29. What symbol is used for commenting in Swift?
    • A) //
    • B) #
    • C) /*
    • D) —
  30. 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
  31. Which of the following is a Swift collection type?
    • A) Stack
    • B) Queue
    • C) Array
    • D) Linked List
  32. 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.
  33. 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
  34. How are multi-line strings declared in Swift?
    • A) “””
    • B) ”’
    • C) []
    • D) {}
  35. 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
  36. Which of the following compares two strings for equality in Swift?
    • A) ==
    • B) =
    • C) isEqual
    • D) compare
  37. Which of the following defines a closure type in Swift?
    • A) () -> Void
    • B) func() -> Void
    • C) Void {}
    • D) closure()
  38. 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
  39. 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
  40. Which keyword is used to indicate a property observer in Swift?
    • A) watch
    • B) observe
    • C) didSet
    • D) willSet
  41. 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
  42. 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
  43. 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
  44. 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
  45. How do you define a variable that is only set once in Swift?
    • A) let
    • B) var
    • C) const
    • D) final
  46. 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()
  47. 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.
  48. 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
  49. 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)
  50. What type of values can enums in Swift contain?
    • A) Only integers
    • B) Strings only
    • C) Any type
    • D) Only unique values