MCQs on Swift’s Type System | Swift

Swift’s type system ensures type safety and performance by enforcing strict typing rules. Understand how type casting, Any and AnyObject, and dispatch methods play crucial roles in Swift’s type management.


1. Understanding Swift’s Strongly Typed System

  1. What is the primary characteristic of Swift’s type system?
    • A) Type inference
    • B) Strong typing
    • C) Dynamic typing
    • D) Type casting
  2. Which of the following is NOT a benefit of Swift’s strongly typed system?
    • A) Type errors are caught at compile time
    • B) Less code is needed for type declaration
    • C) Code is more maintainable and readable
    • D) Flexibility in handling unknown data types
  3. What is the result of assigning a mismatched type in Swift?
    • A) Compile-time error
    • B) Runtime error
    • C) Implicit casting
    • D) Automatic type conversion
  4. Which keyword allows type inference in Swift?
    • A) var
    • B) let
    • C) as
    • D) No keyword, inferred automatically
  5. Swift’s type system ensures type safety by preventing:
    • A) Direct assignment between incompatible types
    • B) Overloading of methods
    • C) Optimizations during runtime
    • D) Function overloading
  6. What is a type alias in Swift?
    • A) A way to define a new name for an existing type
    • B) A type that stores a reference to another type
    • C) A method to perform type conversion
    • D) A way to dynamically assign types at runtime

2. Type Casting with as, as?, and as!

  1. What does the as keyword do in Swift?
    • A) Performs a forced type cast
    • B) Performs an optional cast
    • C) Performs a safe type cast
    • D) Converts a type to a superclass
  2. Which keyword is used for a forced type cast?
    • A) as?
    • B) as!
    • C) as
    • D) typeCast!
  3. What does as? do in Swift?
    • A) Converts any type to a superclass
    • B) Returns an optional result of type casting
    • C) Forces the cast without checking
    • D) Allows casting between different types
  4. When is it safe to use as! for type casting?
    • A) When the cast is guaranteed to succeed
    • B) When the cast is uncertain
    • C) When casting between unrelated types
    • D) When there is no cast required
  5. What happens when an as? cast fails?
    • A) It returns nil
    • B) It throws a runtime error
    • C) It triggers an assertion failure
    • D) It performs a fallback cast
  6. What is the main difference between as? and as!?
    • A) as? returns an optional, as! forces a cast
    • B) as! is used for type inference
    • C) as? performs an unsafe cast
    • D) as? can only be used with class types

3. Any and AnyObject

  1. What does the type Any represent in Swift?
    • A) Any instance of any type
    • B) A class instance
    • C) A reference to an optional
    • D) Any object that inherits from NSObject
  2. Which types can Any hold in Swift?
    • A) Any type including function types
    • B) Only class instances
    • C) Only reference types
    • D) Only structs and enums
  3. What is the difference between Any and AnyObject?
    • A) Any can hold any type, AnyObject holds only class types
    • B) AnyObject can hold any type, Any only holds classes
    • C) AnyObject is used only for arrays
    • D) Any and AnyObject are the same
  4. Which type is used to represent any reference type in Swift?
    • A) AnyObject
    • B) Any
    • C) NSObject
    • D) ReferenceType
  5. Can AnyObject hold primitive types like Int or String?
    • A) No, it can only hold objects or instances of classes
    • B) Yes, it can hold any type
    • C) Yes, but only string types
    • D) No, it only holds arrays
  6. What does Any allow you to do in Swift?
    • A) Store any type in a variable or constant
    • B) Store only objects
    • C) Perform type-safe casting
    • D) Return any value from a function

4. Static vs. Dynamic Dispatch

  1. What is the main difference between static and dynamic dispatch?
    • A) Static dispatch is resolved at compile-time, dynamic dispatch is resolved at runtime
    • B) Static dispatch is slower than dynamic dispatch
    • C) Static dispatch can only be used with structs
    • D) Dynamic dispatch is more type-safe
  2. Which of the following is an example of static dispatch?
    • A) Calling a method on a value type like struct
    • B) Calling a method on a reference type like class
    • C) Using dynamic method resolution
    • D) Using polymorphism with interfaces
  3. Which keyword helps achieve dynamic dispatch in Swift?
    • A) dynamic
    • B) dispatch
    • C) @objc
    • D) class
  4. When does dynamic dispatch occur?
    • A) When a method is called on a reference type object
    • B) When the method is resolved at compile-time
    • C) When using constant types
    • D) During early initialization phases
  5. What is the performance difference between static and dynamic dispatch?
    • A) Static dispatch is generally faster due to compile-time resolution
    • B) Dynamic dispatch is always faster
    • C) Static dispatch requires more runtime overhead
    • D) Dynamic dispatch has no impact on performance
  6. Which of the following will require dynamic dispatch in Swift?
    • A) Calling a method from a subclass
    • B) Calling a method on a struct
    • C) Calling a method from a constant
    • D) Calling a method without polymorphism
  7. In which situation is dynamic dispatch used in Swift?
    • A) When method overriding occurs in a subclass
    • B) When working with constants
    • C) When working with value types
    • D) When using enums
  8. What is an example of a method that uses static dispatch?
    • A) A method in a struct
    • B) A method in a class
    • C) A method that is dynamically resolved
    • D) A method that uses the @objc attribute
  9. What is the consequence of using dynamic dispatch for methods in Swift?
    • A) It allows method overriding and polymorphism
    • B) It increases compile-time errors
    • C) It decreases runtime performance significantly
    • D) It can only be used with AnyObject
  10. How is method dispatch handled in Swift classes by default?
    • A) Dynamically, allowing method overriding
    • B) Statically, with fixed method calls
    • C) Via the @objc keyword
    • D) By manually specifying dispatch type
  11. What does the @objc keyword enable in Swift?
    • A) Dynamic dispatch for methods
    • B) Static dispatch for functions
    • C) Type-safe casting
    • D) The creation of custom classes
  12. When does static dispatch occur in Swift?
    • A) When calling methods on value types
    • B) When using inheritance
    • C) When methods are overridden
    • D) When working with closures

Answers

QnoAnswer
1B) Strong typing
2D) Flexibility in handling unknown data types
3A) Compile-time error
4D) No keyword, inferred automatically
5A) Direct assignment between incompatible types
6A) A way to define a new name for an existing type
7A) Performs a forced type cast
8B) as!
9B) Returns an optional result of type casting
10A) When the cast is guaranteed to succeed
11A) It returns nil
12A) as? returns an optional, as! forces a cast
13A) Any instance of any type
14A) Any type including function types
15A) Any can hold any type, AnyObject holds only class types
16A) AnyObject
17A) No, it can only hold objects or instances of classes
18A) Store any type in a variable or constant
19A) Static dispatch is resolved at compile-time, dynamic dispatch is resolved at runtime
20A) Calling a method on a value type like struct
21C) @objc
22A) When a method is called on a reference type object
23A) Static dispatch is generally faster due to compile-time resolution
24A) Calling a method from a subclass
25A) When method overriding occurs in a subclass
26A) A method in a struct
27A) It allows method overriding and polymorphism
28A) Dynamically, allowing method overriding
29A) Dynamic dispatch for methods
30A) When calling methods on value types

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top