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
What is the primary characteristic of Swift’s type system?
A) Type inference
B) Strong typing
C) Dynamic typing
D) Type casting
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
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
Which keyword allows type inference in Swift?
A) var
B) let
C) as
D) No keyword, inferred automatically
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
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!
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
Which keyword is used for a forced type cast?
A) as?
B) as!
C) as
D) typeCast!
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
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
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
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
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
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
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
Which type is used to represent any reference type in Swift?
A) AnyObject
B) Any
C) NSObject
D) ReferenceType
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
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
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
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
Which keyword helps achieve dynamic dispatch in Swift?
A) dynamic
B) dispatch
C) @objc
D) class
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
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
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
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
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
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
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
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
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
Qno
Answer
1
B) Strong typing
2
D) Flexibility in handling unknown data types
3
A) Compile-time error
4
D) No keyword, inferred automatically
5
A) Direct assignment between incompatible types
6
A) A way to define a new name for an existing type
7
A) Performs a forced type cast
8
B) as!
9
B) Returns an optional result of type casting
10
A) When the cast is guaranteed to succeed
11
A) It returns nil
12
A) as? returns an optional, as! forces a cast
13
A) Any instance of any type
14
A) Any type including function types
15
A) Any can hold any type, AnyObject holds only class types
16
A) AnyObject
17
A) No, it can only hold objects or instances of classes
18
A) Store any type in a variable or constant
19
A) Static dispatch is resolved at compile-time, dynamic dispatch is resolved at runtime
20
A) Calling a method on a value type like struct
21
C) @objc
22
A) When a method is called on a reference type object
23
A) Static dispatch is generally faster due to compile-time resolution