Generics in Dart offer a powerful way to write flexible, reusable, and type-safe code. Learn how to create generic functions, classes, and implement constraints for better code structure.
Introduction to Generics
What is the purpose of using generics in Dart? a) To store any data type b) To create reusable and type-safe components c) To enforce strict type rules d) To simplify code execution
How are generics declared in Dart? a) class <T> ClassName {} b) class ClassName<T> {} c) generic <T> class ClassName {} d) T class ClassName {}
Which of the following is a benefit of using generics in Dart? a) It allows any type of object to be stored in a class. b) It ensures type safety without the need for casting. c) It avoids the use of functions. d) It reduces code readability.
What is the default type used in Dart when no type is specified for a generic? a) Object b) dynamic c) var d) void
What does the angle bracket <T> signify in Dart generics? a) A method argument b) A return type c) A generic type placeholder d) A class name
Which of the following would be an example of a generic list in Dart? a) List<String> list = []; b) List<Object> list = []; c) List<T> list = []; d) List<> list = [];
Generic Functions
What is a generic function in Dart? a) A function that can return multiple types b) A function that can accept multiple types of parameters c) A function that only accepts one type of argument d) A function with no return value
How do you declare a generic function in Dart? a) void function<T>() {} b) T function() {} c) function<T>() {} d) void function() <T> {}
Which of the following would be a correct declaration of a generic function in Dart? a) T add<T>(T a, T b) { return a + b; } b) void add<T>(T a, T b) { return a + b; } c) T add<T>(T a, T b) { a + b; } d) T add(T a, T b) { return a + b; }
In a generic function, what type must the parameters have? a) The same type b) Different types c) Any type d) The type specified in the function call
Which of the following is a correct way to call a generic function add() in Dart with integers? a) add(1, 2) b) add<int>(1, 2) c) add<int>(1, 2); d) add(1, 2);
What does the generic type T represent in a generic function? a) A fixed type b) A class type c) A type that can be replaced with any type at runtime d) A special type used for error handling
Generic Classes and Constraints
What is a generic class in Dart? a) A class that accepts only one data type b) A class with multiple constructors c) A class that can accept a type parameter d) A class that can only store dynamic data
Which syntax is used to define a generic class in Dart? a) class ClassName<T> {} b) class <T> ClassName {} c) class ClassName[] {} d) class T ClassName {}
How do you instantiate a generic class in Dart? a) var obj = ClassName<T>(); b) var obj = ClassName(); c) var obj = new ClassName<T>(); d) var obj = ClassName<T>();
What is the purpose of adding constraints to a generic class in Dart? a) To define limits on the types that can be used with the generic b) To convert the type to a specific value c) To enable the use of multiple data types d) To prevent class inheritance
How can a constraint be applied to a generic type in Dart? a) By using the extends keyword b) By using the implements keyword c) By using the where keyword d) By using the limit keyword
Which of the following applies a constraint on a generic in Dart? a) class Box<T extends num> {} b) class Box<T of num> {} c) class Box<T< num> {} d) class Box<T only num> {}
In a generic class with constraints, which type is allowed as a type for T? a) Any type b) Only classes that extend num c) Only primitive types d) Only interfaces
What does the following code signify? class Container<T extends Animal> a) The class Container accepts any type T. b) The class Container only accepts types that extend Animal. c) The class Container only accepts Animal. d) The class Container accepts Animal as a data member.
How do you restrict the type of T to only be String or its subclasses in a generic class? a) class Container<T extends String> {} b) class Container<T only String> {} c) class Container<T of String> {} d) class Container<T as String> {}
What happens if a constraint is not applied in a generic class? a) The type is restricted to certain classes b) It defaults to accepting all types c) Only primitive types are allowed d) It results in an error
Can a Dart class with a generic type have multiple constraints? a) Yes, using & b) No, only one constraint is allowed c) Yes, using or d) Yes, but only for interfaces
How would you define a class that accepts num or its subclasses as a generic type in Dart? a) class Box<T extends num> b) class Box<T< num> c) class Box<T of num> d) class Box<T is num>
What is the purpose of the extends keyword in a constraint? a) To specify that the type T must implement a class b) To specify that the type T can extend a class c) To specify that the type T must be a subclass of a certain class d) To limit the data type to a primitive type
What is the constraint applied to ensure the generic class only accepts types implementing an interface in Dart? a) extends b) implements c) where d) instanceof
Can Dart generics be used with collections like lists and maps? a) Yes, they can store type-safe values in lists and maps b) No, Dart generics do not support collections c) Yes, but only for lists d) Yes, but only for maps
How can you apply a constraint for a list of objects in a generic class? a) class Box<T extends List<Object>> {} b) class Box<T extends Object[]> {} c) class Box<T[] extends Object> {} d) class Box<T extends List> {}
Which of the following demonstrates how to restrict a generic type to a subclass of Person? a) class Employee<T extends Person> {} b) class Employee<T of Person> {} c) class Employee<T as Person> {} d) class Employee<T< Person> {}
How can generics improve code reusability in Dart? a) By using dynamic types b) By allowing type flexibility while ensuring type safety c) By enforcing type restrictions d) By using only primitive types
Answer Key
QNo
Answer (Option with text)
1
b) To create reusable and type-safe components
2
b) class ClassName<T> {}
3
b) It ensures type safety without the need for casting
4
b) dynamic
5
c) A generic type placeholder
6
a) List<String> list = [];
7
b) A function that can accept multiple types of parameters
8
a) void function<T>() {}
9
a) T add<T>(T a, T b) { return a + b; }
10
a) The same type
11
b) add<int>(1, 2)
12
c) A type that can be replaced with any type at runtime
13
c) A class that can accept a type parameter
14
a) class ClassName<T> {}
15
a) var obj = ClassName<T>();
16
a) To define limits on the types that can be used with the generic
17
c) By using the where keyword
18
a) class Box<T extends num> {}
19
b) Only classes that extend num
20
b) The class Container only accepts types that extend Animal.
21
a) class Container<T extends String> {}
22
b) It defaults to accepting all types
23
a) Yes, using &
24
a) class Box<T extends num>
25
c) To specify that the type T must be a subclass of a certain class
26
b) implements
27
a) Yes, they can store type-safe values in lists and maps
28
a) class Box<T extends List<Object>> {}
29
a) class Employee<T extends Person> {}
30
b) By allowing type flexibility while ensuring type safety