Practice Questions on Advance dart
- Create a generic class
Pairthat holds two values of any type. Implement a method to swap the values. - Write a mixin
Loggerthat provides a methodlog(String message). Create a classServicethat uses this mixin and demonstrates logging messages. - Implement an extension on the
Listclass that adds a methodsum()to return the sum of all numeric elements in the list. - Create a class
Userwith a@deprecatedannotation on a methodgetFullName(). Add a new methodgetName()and demonstrate how to use both methods. - Define a generic function
printList<T>that takes a list of any type and prints each element. Show how to use this function with different data types. - Create a mixin
Flyerwith a methodfly(). Implement a classBirdthat uses this mixin and demonstrate its functionality. - Implement a simple annotation called
@Rangethat takes two integers,minandmax. Use this annotation on a field in a class and show how to access it using reflection. - Define a class
Container<T>that has a methodaddItem(T item). Show how to use this class with different types of items. - Create a class
Animalwith a methodspeak(). Use a mixinWalkerthat adds a methodwalk(). Implement a classDogthat combines both and demonstrates both methods. - Write a program that uses an extension on the
Stringclass to add a methodisNumeric()that checks if the string represents a number.
Answers and Explanations
Generic Class
Pairclass Pair<T, U> { T first; U second; Pair(this.first, this.second); void swap() { var temp = first; first = second; second = temp; } }This class holds two values of any types
TandU, and provides a method to swap them.Mixin
Loggermixin Logger { void log(String message) { print('Log: $message'); } } class Service with Logger { void performService() { log('Service is performed.'); } }The
Loggermixin provides a logging functionality that can be used in any class, such asService.Extension on List
extension ListSum on List<num> { num sum() { return reduce((value, element) => value + element); } }This extension adds a
sum()method to lists containing numeric elements, allowing you to easily calculate their sum.Class
Userwith@deprecatedclass User { @deprecated String getFullName() { return 'John Doe'; } String getName() { return 'John'; } }The
getFullName()method is marked as deprecated, prompting users to usegetName()instead.Generic Function
printList<T>void printList<T>(List<T> list) { for (var item in list) { print(item); } }This function takes a list of any type and prints each element, demonstrating the use of generics.
Mixin
Flyermixin Flyer { void fly() { print('Flying high!'); } } class Bird with Flyer {}The
Flyermixin adds flying functionality to theBirdclass, which can now fly.Annotation
@Rangeclass Range { final int min; final int max; const Range(this.min, this.max); } class Product { @Range(1, 100) int quantity; }This annotation marks the
quantityfield, and you can access it using reflection to validate the range.Class
Container<T>class Container<T> { List<T> items = []; void addItem(T item) { items.add(item); } }The
Containerclass can hold items of any type specified when instantiated, demonstrating the use of generics.Class
Animalwith MixinWalkerclass Animal { void speak() { print('Animal sound'); } } mixin Walker { void walk() { print('Walking'); } } class Dog extends Animal with Walker { @override void speak() { print('Woof'); } }The
Dogclass inherits fromAnimaland uses theWalkermixin to provide both walking and speaking functionalities.Extension on String for
isNumeric()extension StringExtensions on String { bool isNumeric() { return double.tryParse(this) != null; } }This extension method allows any string to check if it represents a numeric value, enhancing the string class functionality.
PLAY QUIZ