MCQs on Meta-programming | Swift

Meta-programming in Swift allows developers to inspect and modify code at runtime. This includes using Swift’s Reflection and Mirror API, introspection techniques, and creating Domain-Specific Languages (DSLs) to enhance code flexibility.


1. Swift Reflection and Mirror API

  1. What is Swift’s Mirror API used for?
    • A) For inspecting type information at runtime
    • B) For modifying code at compile-time
    • C) For enforcing type safety
    • D) For managing memory allocation
  2. Which of the following types is returned by the Mirror API?
    • A) Mirror
    • B) Type
    • C) Reflectable
    • D) Inspector
  3. How do you create a Mirror instance for an object in Swift?
    • A) Using Mirror(reflecting:)
    • B) Using Reflection.init()
    • C) Using Mirror() constructor
    • D) Using Mirror.object()
  4. What does the children property of Mirror return?
    • A) A list of object properties and their values
    • B) The class’s methods
    • C) The class’s parents
    • D) The class’s static properties
  5. Which method of Mirror returns the type of the reflected object?
    • A) subjectType
    • B) typeOf
    • C) getType()
    • D) classType
  6. What is the output type of Mirror(reflecting: object).children?
    • A) A collection of tuples
    • B) A list of strings
    • C) An array of types
    • D) An array of objects

2. Introspection with Mirror

  1. What is introspection in Swift programming?
    • A) Examining and modifying an object’s type at runtime
    • B) Inspecting objects before runtime
    • C) Creating objects dynamically
    • D) Checking runtime errors
  2. Which of the following properties of Mirror allows you to introspect an object’s type?
    • A) subjectType
    • B) children
    • C) displayStyle
    • D) label
  3. Can the Mirror API be used to inspect properties of non-class types (like structs and enums)?
    • A) Yes
    • B) No
    • C) Only for classes
    • D) Only for enums
  4. What does the displayStyle property of a Mirror object represent?
    • A) The object’s visual style
    • B) The object’s underlying type category
    • C) The object’s class name
    • D) The object’s memory address
  5. Which of the following is a limitation of introspection using Mirror?
    • A) It cannot access private properties
    • B) It only works for objects of type AnyObject
    • C) It is limited to class types only
    • D) It requires reflection at compile time
  6. How can you retrieve an object’s property value using Mirror?
    • A) Accessing children and matching labels
    • B) Using the getValue() method
    • C) Using the accessProperty() method
    • D) Using inspect()
  7. What does the label property of a Mirror.Child represent?
    • A) The name of the child property
    • B) The value of the child property
    • C) The type of the child property
    • D) The parent class of the property
  8. How do you access the value of a specific child in a Mirror instance?
    • A) Using index positions in the children collection
    • B) Using the child’s label to match the property
    • C) Using the getChildValue() method
    • D) Accessing the value property of Mirror
  9. What is the return type of Mirror(reflecting: object).subjectType?
    • A) Any.Type
    • B) String
    • C) Any
    • D) Type

3. Custom DSLs in Swift

  1. What is a Domain-Specific Language (DSL) in Swift?
    • A) A specialized language for a specific problem domain
    • B) A generic language for all applications
    • C) A language that only works with databases
    • D) A language used only for system-level programming
  2. What is the main advantage of creating a DSL in Swift?
    • A) It simplifies code for specific tasks or domains
    • B) It enhances the performance of the application
    • C) It is easier to compile
    • D) It allows runtime reflection
  3. Which Swift feature allows the creation of a custom DSL?
    • A) Operator overloading
    • B) Protocols
    • C) Closures
    • D) Generics
  4. How does operator overloading help in creating a custom DSL?
    • A) It allows creating custom operators to express domain-specific actions
    • B) It allows creating new types for the DSL
    • C) It helps reduce memory usage in the DSL
    • D) It automatically infers types in DSL
  5. Which keyword in Swift is often used to define a custom DSL syntax?
    • A) func
    • B) let
    • C) in
    • D) typealias
  6. What is the role of closures in creating a DSL in Swift?
    • A) Closures help create block-based syntax for specific tasks
    • B) Closures provide type-safety in the DSL
    • C) Closures are used to manage memory in DSLs
    • D) Closures are required for operator overloading
  7. What is a key challenge when building a DSL in Swift?
    • A) Ensuring the DSL is both readable and maintainable
    • B) Achieving maximum performance
    • C) Avoiding reflection during runtime
    • D) Creating an automatic parser
  8. Can a custom DSL in Swift be created using a combination of closures and operator overloading?
    • A) Yes
    • B) No
    • C) Only using protocols
    • D) Only using classes
  9. Which Swift feature is commonly used to create a more readable syntax in a custom DSL?
    • A) Type aliases
    • B) Closures and operator overloading
    • C) Generics
    • D) Extensions
  10. What does the use of @escaping closures enable in a custom DSL?
    • A) It allows closures to persist beyond the scope of their original context
    • B) It limits the number of closures
    • C) It optimizes memory usage
    • D) It enables optional parameters in DSL
  11. Which of the following is an example of a custom DSL feature in Swift?
    • A) Custom mathematical operators
    • B) Default property values
    • C) Type casting
    • D) Reflection
  12. How can Swift protocols help in creating a custom DSL?
    • A) By defining behaviors that can be reused across different domains
    • B) By enforcing type safety in DSL
    • C) By providing default methods for DSL operations
    • D) By making the DSL flexible
  13. What role does Swift’s typealias keyword play in a custom DSL?
    • A) It allows creating human-readable names for complex types
    • B) It enables closures within the DSL
    • C) It defines custom operators
    • D) It reduces code duplication in the DSL
  14. In which scenario would you most likely use a custom DSL in Swift?
    • A) For mathematical calculations
    • B) For web application routing
    • C) For complex business logic like query builders or UI builders
    • D) For creating UI elements
  15. What is the primary benefit of using a custom DSL in Swift?
    • A) It improves code clarity and expressiveness for specific tasks
    • B) It increases application performance
    • C) It makes memory management easier
    • D) It helps manage runtime errors

Answers

QnoAnswer
1A) For inspecting type information at runtime
2A) Mirror
3A) Using Mirror(reflecting:)
4A) A list of object properties and their values
5A) subjectType
6A) A collection of tuples
7A) Examining and modifying an object’s type at runtime
8A) subjectType
9A) Yes
10B) The object’s underlying type category
11A) It cannot access private properties
12A) Accessing children and matching labels
13A) The name of the child property
14B) Using the child’s label to match the property
15A) Any.Type
16A) A specialized language for a specific problem domain
17A) It simplifies code for specific tasks or domains
18A) Operator overloading
19A) It allows creating custom operators to express domain-specific actions
20C) in
21A) Closures help create block-based syntax for specific tasks
22A) Ensuring the DSL is both readable and maintainable
23A) Yes
24B) Closures and operator overloading
25A) It allows closures to persist beyond the scope of their original context
26A) Custom mathematical operators
27A) By defining behaviors that can be reused across different domains
28A) It allows creating human-readable names for complex types
29C) For complex business logic like query builders or UI builders
30A) It improves code clarity and expressiveness for specific tasks

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