MCQs on Macros and Metaprogramming | Scala

Dive deep into macros, metaprogramming, runtime reflection, and Scala Meta utilities with these 30 essential MCQs. Enhance your understanding of Scala’s advanced capabilities for code generation and type manipulation.


Macros and Metaprogramming in Scala

1. Macros in Scala

  1. What is the primary purpose of macros in Scala?
    • A) To optimize runtime performance
    • B) To generate code at compile-time
    • C) To improve logging features
    • D) To enhance debugging
  2. What is the key advantage of compile-time macros in Scala?
    • A) Faster execution at runtime
    • B) Automatic serialization of data
    • C) Type-safe code generation
    • D) Better thread safety
  3. Which keyword is used to define a macro in Scala?
    • A) macro
    • B) def
    • C) compile
    • D) reflect
  4. What does a macro return during compilation?
    • A) A runtime value
    • B) Abstract syntax trees (ASTs)
    • C) Serialized data
    • D) A runtime error
  5. In which phase of compilation do macros execute?
    • A) Parsing phase
    • B) Semantic analysis phase
    • C) Code generation phase
    • D) Type-checking phase
  6. Which of the following is a limitation of macros in Scala?
    • A) They cannot access runtime data
    • B) They are not type-safe
    • C) They reduce runtime performance
    • D) They can only be used with primitive types
  7. Which API is commonly used for macro implementation in Scala 2?
    • A) scala.reflect.macros.blackbox.Context
    • B) scala.meta.macros
    • C) java.lang.reflect
    • D) scalaz.macros
  8. What is one of the primary goals of macros in functional programming?
    • A) To improve runtime type checking
    • B) To eliminate boilerplate code
    • C) To enforce immutability
    • D) To handle concurrency
  9. What feature replaced macros in Scala 3?
    • A) Inline methods and quotes
    • B) Reflection API
    • C) Abstract type members
    • D) Higher-order functions
  10. Why are macros considered experimental in Scala 2?
    • A) They are not officially supported by the Scala compiler
    • B) They have unpredictable runtime behavior
    • C) They lack a stable API
    • D) They require external libraries

2. Reflection and Runtime Type Information

  1. What is runtime reflection in Scala used for?
    • A) To manipulate the AST
    • B) To inspect and modify program structure at runtime
    • C) To enhance type inference
    • D) To serialize objects
  2. Which Scala package provides reflection capabilities?
    • A) scala.runtime
    • B) scala.reflect
    • C) scala.meta.reflect
    • D) scala.util.reflect
  3. What is a TypeTag in Scala?
    • A) A runtime representation of a type
    • B) A macro for code generation
    • C) A trait for higher-kinded types
    • D) An annotation for type aliases
  4. What is a Manifest in Scala?
    • A) A deprecated way to access type information
    • B) A compile-time annotation
    • C) A runtime container for ASTs
    • D) A functional programming construct
  5. Which of the following can be used to inspect method details at runtime?
    • A) Class objects in Java Reflection
    • B) scala.reflect.runtime.universe
    • C) TypeTag and Manifest
    • D) All of the above
  6. Why is runtime reflection less favored in functional programming?
    • A) It breaks immutability principles
    • B) It introduces runtime overhead
    • C) It lacks type safety
    • D) All of the above
  7. What is the ToolBox in Scala Reflection API?
    • A) A utility to execute Scala code at runtime
    • B) A container for runtime types
    • C) A debugger for macros
    • D) A logger for errors
  8. How does TypeTag differ from ClassTag?
    • A) TypeTag provides full type information, while ClassTag focuses on erased types
    • B) ClassTag is used for runtime reflection only
    • C) TypeTag cannot access generic types
    • D) ClassTag is faster but less precise
  9. What is one drawback of using runtime reflection in Scala?
    • A) Reduced runtime performance
    • B) Loss of type safety
    • C) Increased code complexity
    • D) All of the above
  10. Which method allows dynamic code evaluation using Scala Reflection?
    • A) ToolBox.eval
    • B) Manifest.invoke
    • C) TypeTag.execute
    • D) Reflect.run

3. Scala Meta and its Utilities

  1. What is the primary use of Scala Meta?
    • A) Runtime code manipulation
    • B) Compile-time code analysis and transformation
    • C) Thread management
    • D) Parallel computation
  2. Which library supports the creation of custom linting tools in Scala?
    • A) Scala Meta
    • B) Cats Effect
    • C) ZIO
    • D) Play Framework
  3. What are quasiquotes in Scala Meta?
    • A) A way to construct and deconstruct syntax trees
    • B) A debugging utility
    • C) A runtime optimization tool
    • D) A type aliasing mechanism
  4. What is a Tree in Scala Meta?
    • A) A representation of Scala code
    • B) A mutable data structure
    • C) A macro container
    • D) A runtime exception
  5. What replaced Scala Macros for most use cases in Scala 3?
    • A) Scala Meta
    • B) Inline and quotes
    • C) Futures
    • D) Higher-order functions
  6. Which tool in Scala Meta is used for parsing Scala code?
    • A) Tree
    • B) Parser
    • C) ToolBox
    • D) MacroUtils
  7. What is a SemanticDB in Scala Meta?
    • A) A database for storing semantic information about Scala code
    • B) A concurrency tool
    • C) A runtime debugger
    • D) A type-safe serialization library
  8. Which component of Scala Meta is used for code analysis?
    • A) ScalametaParser
    • B) SemanticDB
    • C) ToolBox
    • D) AST
  9. What is the primary goal of Scala Meta in tool development?
    • A) To enable advanced type inference
    • B) To simplify the creation of compilers and code analysis tools
    • C) To handle runtime reflection
    • D) To provide support for monads
  10. What is a common application of Scala Meta utilities?
    • A) Building IDE plugins
    • B) Creating code formatters
    • C) Developing static code analyzers
    • D) All of the above

Answer Key

QnoAnswer (Option with the text)
1B) To generate code at compile-time
2C) Type-safe code generation
3A) macro
4B) Abstract syntax trees (ASTs)
5B) Semantic analysis phase
6A) They cannot access runtime data
7A) scala.reflect.macros.blackbox.Context
8B) To eliminate boilerplate code
9A) Inline methods and quotes
10C) They lack a stable API
11B) To inspect and modify program structure at runtime
12B) scala.reflect
13A) A runtime representation of a type
14A) A deprecated way to access type information
15D) All of the above
16D) All of the above
17A) A utility to execute Scala code at runtime
18A) TypeTag provides full type information, while ClassTag focuses on erased types
19D) All of the above
20A) ToolBox.eval
21B) Compile-time code analysis and transformation
22A) Scala Meta
23A) A way to construct and deconstruct syntax trees
24A) A representation of Scala code
25B) Inline and quotes
26B) Parser
27A) A database for storing semantic information about Scala code
28B) SemanticDB
29B) To simplify the creation of compilers and code analysis tools
30D) All of the above

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