MCQs on Generics in Dart | Dart

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

  1. 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
  2. How are generics declared in Dart?
    a) class <T> ClassName {}
    b) class ClassName<T> {}
    c) generic <T> class ClassName {}
    d) T class ClassName {}
  3. 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.
  4. What is the default type used in Dart when no type is specified for a generic?
    a) Object
    b) dynamic
    c) var
    d) void
  5. 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
  6. 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

  1. 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
  2. How do you declare a generic function in Dart?
    a) void function<T>() {}
    b) T function() {}
    c) function<T>() {}
    d) void function() <T> {}
  3. 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; }
  4. 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
  5. 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);
  6. 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

  1. 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
  2. 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 {}
  3. 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>();
  4. 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
  5. 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
  6. 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> {}
  7. 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
  8. 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.
  9. 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> {}
  10. 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
  11. 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
  12. 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>
  13. 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
  14. 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
  15. 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
  16. 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> {}
  17. 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> {}
  18. 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

QNoAnswer (Option with text)
1b) To create reusable and type-safe components
2b) class ClassName<T> {}
3b) It ensures type safety without the need for casting
4b) dynamic
5c) A generic type placeholder
6a) List<String> list = [];
7b) A function that can accept multiple types of parameters
8a) void function<T>() {}
9a) T add<T>(T a, T b) { return a + b; }
10a) The same type
11b) add<int>(1, 2)
12c) A type that can be replaced with any type at runtime
13c) A class that can accept a type parameter
14a) class ClassName<T> {}
15a) var obj = ClassName<T>();
16a) To define limits on the types that can be used with the generic
17c) By using the where keyword
18a) class Box<T extends num> {}
19b) Only classes that extend num
20b) The class Container only accepts types that extend Animal.
21a) class Container<T extends String> {}
22b) It defaults to accepting all types
23a) Yes, using &
24a) class Box<T extends num>
25c) To specify that the type T must be a subclass of a certain class
26b) implements
27a) Yes, they can store type-safe values in lists and maps
28a) class Box<T extends List<Object>> {}
29a) class Employee<T extends Person> {}
30b) By allowing type flexibility while ensuring type safety

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