Practice Questions on Advance dart

 

  1. Create a generic class Pair that holds two values of any type. Implement a method to swap the values.
  2. Write a mixin Logger that provides a method log(String message). Create a class Service that uses this mixin and demonstrates logging messages.
  3. Implement an extension on the List class that adds a method sum() to return the sum of all numeric elements in the list.
  4. Create a class User with a @deprecated annotation on a method getFullName(). Add a new method getName() and demonstrate how to use both methods.
  5. 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.
  6. Create a mixin Flyer with a method fly(). Implement a class Bird that uses this mixin and demonstrate its functionality.
  7. Implement a simple annotation called @Range that takes two integers, min and max. Use this annotation on a field in a class and show how to access it using reflection.
  8. Define a class Container<T> that has a method addItem(T item). Show how to use this class with different types of items.
  9. Create a class Animal with a method speak(). Use a mixin Walker that adds a method walk(). Implement a class Dog that combines both and demonstrates both methods.
  10. Write a program that uses an extension on the String class to add a method isNumeric() that checks if the string represents a number.

Answers and Explanations

  1. Generic Class Pair

    class 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 T and U, and provides a method to swap them.

  2. Mixin Logger

    mixin Logger {
      void log(String message) {
        print('Log: $message');
      }
    }
    
    class Service with Logger {
      void performService() {
        log('Service is performed.');
      }
    }
    

    The Logger mixin provides a logging functionality that can be used in any class, such as Service.

  3. 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.

  4. Class User with @deprecated

    class User {
      @deprecated   String getFullName() {
        return 'John Doe';
      }
    
      String getName() {
        return 'John';
      }
    }
    

    The getFullName() method is marked as deprecated, prompting users to use getName() instead.

  5. 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.

  6. Mixin Flyer

    mixin Flyer {
      void fly() {
        print('Flying high!');
      }
    }
    
    class Bird with Flyer {}
    

    The Flyer mixin adds flying functionality to the Bird class, which can now fly.

  7. Annotation @Range

    class Range {
      final int min;
      final int max;
      const Range(this.min, this.max);
    }
    
    class Product {
      @Range(1, 100)
      int quantity;
    }
    

    This annotation marks the quantity field, and you can access it using reflection to validate the range.

  8. Class Container<T>

    class Container<T> {
      List<T> items = [];
    
      void addItem(T item) {
        items.add(item);
      }
    }
    

    The Container class can hold items of any type specified when instantiated, demonstrating the use of generics.

  9. Class Animal with Mixin Walker

    class Animal {
      void speak() {
        print('Animal sound');
      }
    }
    
    mixin Walker {
      void walk() {
        print('Walking');
      }
    }
    
    class Dog extends Animal with Walker {
      @override   void speak() {
        print('Woof');
      }
    }
    

    The Dog class inherits from Animal and uses the Walker mixin to provide both walking and speaking functionalities.

  10. 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

What happens when you call a function that throws an exception in Dart without handling it?

The program continues without interruption

The program crashes and terminates

The exception is ignored

The program automatically retries the operation