MCQs on Meta-Programming | Dart

Explore Meta-Programming in Dart through 30 multiple-choice questions covering Dart annotations and metadata, Reflection in Dart, and Code generation basics. Deepen your understanding of advanced Dart features with these questions.


Dart Annotations and Metadata

  1. What is the purpose of annotations in Dart?
    a) To add metadata to classes, methods, or variables
    b) To define the data types
    c) To improve code readability
    d) To store runtime information
  2. Which keyword is used to define annotations in Dart?
    a) @annotation
    b) #annotation
    c) annotation()
    d) @
  3. What is metadata in Dart?
    a) Data about data
    b) Data stored in variables
    c) Data about classes only
    d) Data for runtime execution
  4. Which of the following is a valid way to use an annotation in Dart?
    a) @MyAnnotation class MyClass {}
    b) MyAnnotation class MyClass {}
    c) class MyClass { @MyAnnotation }
    d) MyAnnotation() class MyClass {}
  5. Can you apply multiple annotations to a Dart class?
    a) Yes, using comma separation
    b) No, only one annotation can be used
    c) Yes, but only for methods
    d) Yes, but only for fields
  6. What is the role of the @override annotation in Dart?
    a) It marks a method as deprecated
    b) It indicates that a method is overriding a superclass method
    c) It marks a method as private
    d) It enhances method performance
  7. Which built-in annotation is used to mark an entity as deprecated in Dart?
    a) @deprecated
    b) @Override
    c) @obsolete
    d) @deprecated
  8. What does the @required annotation indicate in Dart?
    a) A method parameter must be non-null
    b) A field is mandatory
    c) A function must return a value
    d) A class is mandatory for instantiation
  9. How can you retrieve the annotations associated with a class at runtime in Dart?
    a) Using reflection
    b) Using metadata
    c) Using the @getAnnotations function
    d) Using dart:io API
  10. How do you define a custom annotation in Dart?
    a) class MyAnnotation {}
    b) @MyAnnotation()
    c) const MyAnnotation()
    d) @annotation MyAnnotation {}

Reflection in Dart

  1. What is reflection in Dart?
    a) A way to modify code at runtime
    b) A method to view and inspect the structure of objects at runtime
    c) A feature to enhance performance
    d) A tool to manipulate memory
  2. Which Dart library is required to use reflection?
    a) dart:mirror
    b) dart:reflection
    c) dart:meta
    d) dart:annotation
  3. What does reflect() do in Dart?
    a) It reflects the object’s attributes
    b) It reflects the method call of a class
    c) It returns a mirror for a class or object
    d) It returns the object’s metadata
  4. Which of the following is true about reflection in Dart?
    a) Reflection allows you to access private members
    b) Reflection allows you to change object properties at compile-time
    c) Reflection is used for debugging only
    d) Reflection is used for runtime inspection and modification of classes and objects
  5. Which function is used to get the class type of an object in Dart?
    a) getClass()
    b) reflectType()
    c) typeOf()
    d) reflect()
  6. Which method helps inspect the methods of a class using reflection?
    a) instanceMembers
    b) methodList()
    c) methods()
    d) reflectMethods()
  7. How can you invoke a method dynamically using reflection in Dart?
    a) Using invoke()
    b) Using call()
    c) Using method()
    d) Using reflectCall()
  8. What does the @mirrors annotation signify in Dart?
    a) It marks a method as inspectable during reflection
    b) It marks an object as private during reflection
    c) It allows classes and methods to be reflected at runtime
    d) It prevents reflection on methods
  9. What is the main disadvantage of using reflection in Dart?
    a) It significantly impacts performance
    b) It causes code duplication
    c) It requires a complex setup
    d) It can only be used in static environments
  10. Which of the following can reflection in Dart access at runtime?
    a) Private members only
    b) All public and private members
    c) Only static members
    d) Only methods

Code Generation Basics

  1. What is code generation in Dart?
    a) Automatically writing source code based on metadata
    b) Writing boilerplate code manually
    c) A technique for speeding up code execution
    d) A debugging technique
  2. Which library is used for code generation in Dart?
    a) dart:gen
    b) dart:codegen
    c) package:json_serializable
    d) package:build
  3. What is the build_runner package used for in Dart?
    a) It is used to generate code automatically during compilation
    b) It is used to debug code
    c) It is used to track runtime errors
    d) It is used to execute unit tests
  4. How does code generation help in Dart?
    a) By automatically generating optimized code based on input data
    b) By increasing the speed of execution
    c) By reducing the size of the code
    d) By generating code that can handle all exceptions
  5. What is the @JsonSerializable() annotation used for in Dart?
    a) To generate code for converting objects to JSON and vice versa
    b) To create custom annotations for code generation
    c) To serialize class fields automatically
    d) To execute a function after serialization
  6. Which tool is used to run code generation tasks in Dart?
    a) dart generate
    b) flutter build
    c) build_runner
    d) dart compile
  7. How can you trigger the code generation process in Dart?
    a) By running dart generate
    b) By using the build_runner command
    c) By using the flutter generate command
    d) By using dart build
  8. What happens when you run build_runner in Dart?
    a) It automatically executes the code and generates source files
    b) It compiles the code to machine code
    c) It runs the code for debugging
    d) It optimizes the code for deployment
  9. Which of the following is NOT a feature of code generation in Dart?
    a) Generating boilerplate code automatically
    b) Serializing and deserializing objects
    c) Writing tests for the code
    d) Reducing human error in repetitive coding tasks
  10. What is the role of @Generated() annotation in Dart?
    a) It indicates that a part of the code is auto-generated
    b) It generates code for debugging
    c) It marks a class for serialization
    d) It prevents code generation

Answer Key

QnoAnswer
1a) To add metadata to classes, methods, or variables
2d) @
3a) Data about data
4a) @MyAnnotation class MyClass {}
5a) Yes, using comma separation
6b) It indicates that a method is overriding a superclass method
7a) @deprecated
8a) A method parameter must be non-null
9a) Using reflection
10c) const MyAnnotation()
11b) A method to view and inspect the structure of objects at runtime
12a) dart:mirror
13c) It returns a mirror for a class or object
14d) Reflection is used for runtime inspection and modification of classes and objects
15b) reflectType()
16a) instanceMembers
17a) Using invoke()
18c) It allows classes and methods to be reflected at runtime
19a) It significantly impacts performance
20b) All public and private members
21a) Automatically writing source code based on metadata
22c) package:json_serializable
23a) It is used to generate code automatically during compilation
24a) By automatically generating optimized code based on input data
25a) To generate code for converting objects to JSON and vice versa
26c) build_runner
27b) By using the build_runner command
28a) It automatically executes the code and generates source files
29c) Writing tests for the code
30a) It indicates that a part of the code is auto-generated

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