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
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
Which keyword is used to define annotations in Dart? a) @annotation b) #annotation c) annotation() d) @
What is metadata in Dart? a) Data about data b) Data stored in variables c) Data about classes only d) Data for runtime execution
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 {}
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
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
Which built-in annotation is used to mark an entity as deprecated in Dart? a) @deprecated b) @Override c) @obsolete d) @deprecated
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
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
How do you define a custom annotation in Dart? a) class MyAnnotation {} b) @MyAnnotation() c) const MyAnnotation() d) @annotation MyAnnotation {}
Reflection in Dart
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
Which Dart library is required to use reflection? a) dart:mirror b) dart:reflection c) dart:meta d) dart:annotation
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
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
Which function is used to get the class type of an object in Dart? a) getClass() b) reflectType() c) typeOf() d) reflect()
Which method helps inspect the methods of a class using reflection? a) instanceMembers b) methodList() c) methods() d) reflectMethods()
How can you invoke a method dynamically using reflection in Dart? a) Using invoke() b) Using call() c) Using method() d) Using reflectCall()
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
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
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
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
Which library is used for code generation in Dart? a) dart:gen b) dart:codegen c) package:json_serializable d) package:build
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
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
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
Which tool is used to run code generation tasks in Dart? a) dart generate b) flutter build c) build_runner d) dart compile
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
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
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
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
Qno
Answer
1
a) To add metadata to classes, methods, or variables
2
d) @
3
a) Data about data
4
a) @MyAnnotation class MyClass {}
5
a) Yes, using comma separation
6
b) It indicates that a method is overriding a superclass method
7
a) @deprecated
8
a) A method parameter must be non-null
9
a) Using reflection
10
c) const MyAnnotation()
11
b) A method to view and inspect the structure of objects at runtime
12
a) dart:mirror
13
c) It returns a mirror for a class or object
14
d) Reflection is used for runtime inspection and modification of classes and objects
15
b) reflectType()
16
a) instanceMembers
17
a) Using invoke()
18
c) It allows classes and methods to be reflected at runtime
19
a) It significantly impacts performance
20
b) All public and private members
21
a) Automatically writing source code based on metadata
22
c) package:json_serializable
23
a) It is used to generate code automatically during compilation
24
a) By automatically generating optimized code based on input data
25
a) To generate code for converting objects to JSON and vice versa
26
c) build_runner
27
b) By using the build_runner command
28
a) It automatically executes the code and generates source files
29
c) Writing tests for the code
30
a) It indicates that a part of the code is auto-generated