Dart Interview Questions

Dart Interview Questions

Dart Interview Questions

1. Remove point (float) value or rounding value in Dart?

var num = 10.12345678;
var removePointValue = double.parse(num.toStringAsFixed(2));

Get Round Value
var roundingValue= double.parse(num.toStringAsFixed(0));

2. What is collection framework?

Collections are a set of interfaces and classes that implement highly optimised data structures.

3. Dart Collection

Dart doesn’t support the array to store the data, unlike the other programming language. We can use the Dart collection in place of array data structure. We can enable the other classes of the collection in our Dart script by using the dart::core library.

[1] List: A list is the collection of the ordered group of collection. The dart::core library offers the list class that allows us to create and modify the list. It provides the following types of lists.

[2] Set: A set is the collection of the objects in which each object can be declared at once. The dart::core library offers the Set class to use its facilities.

[3] Maps: The maps are the collection of the key-value pair of data. Each value is stored with a particular key. The key and value can be any type in the dart. A Map is a dynamic collection. We can say that map can be modified at the run-time. The dart::core library makes available the Map class to work with it.

[4] Queue: A queue is the collection of the where data stores in the first-in-first-out format. It can be manipulated at both ends. Simply, we can enter the element from one end and delete it from another end.

4. List

Lists are 0-indexed collections of objects with length.
In other programming languages, they are called arrays. Lists are Iterable and the iteration occurs in the index order. If the length of the list is changed during an iteration, a ConcurrentModificationError is thrown.

5. Types of list in dart?

Fixed Length List – We cannot change the list’s length at runtime. List<> fixedLengthList = new List(5);
Growable List – Default growable list. We can change the length of the list at run-time. List growableList = new List();
A GrowableList is an internal type that represents a growable List. It keeps an internal buffer that grows when necessary. This guarantees that a sequence of add operations will each execute in amortized constant time.

6. Syntax of list

  1. Fixed: List<> myList = List<>(3);
    2) growable list: List<> myList = List<>();
    3) initialize List with values: List<> intList = [1, 2, 3];
    4) by default, growable: false: var fixedList = List.filled(3, 1);
    5) growable true: var growableList = List.filled(3, 2, growable: true);
    6) list from: var growableList = List.from([1, 2, 3]);
    7) unmodifiable: var unmodifiableList = List.unmodifiable([1, 2, 3]);

7. Empty collection define.

Map: final param = <>{};
List: List emptyList = List.empty();

         OR

        List<> vals = [];

8. Remove list method?

remove()
removeAt()
removeLast()
removeRange()

9. skip()

This method ignores the elements starting from index 0 till count and returns remaining iterable from given list.

10. isEmpty()

Returns true if the collection has no elements.

11. length()

Returns the size of the list.

12. followebBy()

List methods that may not be used frequently. This method appends new iterables to the given list.

13. reduce()

This method is very similar to fold and returns a single value by iterating all elements of given list. Only difference is, this method doesn’t take any initialValue and the given list should be non-empty.

14. take()

This method returns iterable starting from index 0 till the count provided from given list.

15. sublist()

This method returns a new list containing elements from the index between start and end. Note that an element is exclusive while start is inclusive.
If the endameter is not provided, it returns all elements starting from start till length of the list.

16. Single

Checks if the list has only one element and returns it.

17. Last()

Returns the last element in the list.

18. IndexOf()

Returns the first index of the first element in the list that equals to a given element. Returns -1 if nothing found.

19. IndexWhere()

Returns the first index in the list that satisfies the given conditions. If nothing found, returns -1.

20. followedBy()

This method appends new iterables to the given list.

21. Reversed

Returns an iterable object containing the lists values in the reverse order.

22. LastIndexWhere

Returns the last index in the list that satisfies the given conditions. If nothing found, returns -1.

23. Get the Index of a Specific Element in a List (indexOf, indexWhere, lastIndexOf, lastIndexWhere)

indexOf: Returns the first index of the first element in the list that equals to a given element. Returns -1 if nothing found.
indexWhere: Returns the first index in the list that satisfies the given conditions. If nothing found, returns -1.
lastIndexOf: Returns the index of the last element in the list that equals to a given element. Returns -1 if nothing found.
lastIndexWhere: Returns the last index in the list that satisfies the given conditions. If nothing found, returns -1.

24. lastIndexOf()

Returns the index of the last element in the list that equals to a given element. Returns -1 if nothing found.

25. What is difference between remove and removeWhere?

remove: Single element remove
removeWhere: match all element remove

26. Every

This method returns a boolean depending upon whether all elements satisfy the condition or not.

27. clear()

clears the given list.

28. Revesed

Revesed is a getter which reverses iteration of the list depending upon given list order.

29. whereType()

This method returns iterable with all elements of specific data type.
Let’s say we have a list with mix data such as String and int and we just want to retrieve int data from it, then we would use whereType() method.

30. getRandge()

This method returns elements from specified range [start] to [end] in same order as in the given list. Note that, start element is inclusive but end element is exclusive.

31. Any

This method returns a boolean depending upon whether any element satisfies the condition or not.

32. fold()

This method returns a single value by iterating all elements of given list along with an initialValue, i.e. if we want sum of all elements or product of all elements, then, fold helps us to do that.

33. firstWhere()

This method returns the first element from the list when the give condition is safisfied.

34. shuffle()

This method re – arranges order of the elements in the given list randomly.

35. replaceRange()

This method helps to replace / update some elements of the given list with the new ones. The start and end range need to be provided alongwith the value to be updated in that range.

36. What is the difference between Set and List?

Set is a collection of objects in which each object can occur only once. In List, the same elements can occur simultaneously.

37. Dart types of sorting main (Comparator and Comparable)?

  1. Comparator: [].sort((a, b) => a.length.compareTo(b.length));
    2) Comparable:

    class Item implements Comparable<> {
    int id;
    String name;
    int price;

    Item({this.id, this.name, this.price, });

    @override
    int compareTo(Item other) {
    return price – other.price;
    }
    }

38. Set

Sets are collections of objects in which every object can only occur once.
Two objects are considered indistinguishable, hence can occur only once, if they are equal with regard to the Object.== operator. Sets are Iterable and the order of iteration depends on the specific Set implementations.

39. HashSet

– A HashSet is an unordered hash-table implementation of Set.
– All the elements of a HashSet must have consistent equality and hashCode implementation.
– Depending on the distribution of hash codes of the elements, the add, remove, contains and length operations run in potentially amortized constant time.
– Since the elements of a HashSet are not ordered, iteration order is unspecified, but it is consistent between changes to the set.

40. LinkedList

– A LinkedList is a specialized double-linked list of elements that extend LinkedListEntry.
– Despite its name, LinkedList is not an implementation of List, hence it does not provide constant-time index lookup.
– Since every LinkedListEntry element knows its position in the list, this enables constant time insertAfter, insertBefore and unlink operations on the LinkedListEntry element.
– The LinkedList allows constant-time adding and removing at either end, as well as constant-time length getter.

41. Queue

A Queue is a collection that can be manipulated at both ends.
As Queue is an abstract class in Dart, there are two main implementations available:
1) ListQueue
2) DoubleLinkedQueue
– ListQueue keeps a cyclic buffer of elements. When the buffer fills up, it doubles its size to accommodate the new items.
It guarantees constant time peek and remove operations and amortized constant-time for add operations (due to buffer size adjustments).
– DoubleLinkedQueue is a Queue implementation based on a double-linked list.
It allows constant-time add, remove-at-ends and peek operations.

42. SplayTreeSet

– A SplayTreeSet is a self-balancing binary tree implementation of Set. It allows most operations in amortized logarithmic time.
– The elements of the set can be ordered relative to each other using a compare function that is passed in the constructor.
– If the compare function is omitted, the objects have to be Comparable and compared using their Comparable.compareTo method.

43. LinkedHashMap

The LinkedHashMap releases the data in the sorted order same as the insertion order. If you add the X key first and then insert another key as Y, you will receive the X value first when the iteration is done through the Map.

44. Map

Maps are collections of key/value pairs, from which you retrieve a value using its associated key.
Every key in the map is unique.
Maps and the keys and values are Iterable and the order of iteration depends on the specific Map implementations.

45 HashMap

– HashSet, a HashMap is an unordered hash-table implementation of Set.
– The keys of a HashMap must have consistent equality and hashCode implementation.
– Simple operations are done in (potentially amortized) constant time: add, contains, remove, and length, given that the hash codes of objects are well distributed.
– Iteration order of keys, values and entries is unspecified and might happen in any order but is consistent between changes. Values are iterated in the same order as their associated keys — iterating keys and values in parallel will give matching key/value pairs.

46. asMap()

This method returns a map representation of the given list. The key would be tge indices and valye would be tge correspondubg elements of the list.

47. MapEntry

A key/value pair representing an entry in a Map.

48. SplayTreeMap

– A SplayTreeMap is a Map implementation based on a self-balancing binary tree. It allows most single-entry operations is amortized logarithmic time.
– Keys of the map are compared using the compare function passed in the constructor.
– If the compare function is omitted, then the keys are considered to be Comparable and their associated Comparable.compareTo functions are used for setting their order in the map.

49. What is iterator?

An iterator is an interface for getting items, one at a time, from an object. All Iterable types have iterators. Iterators, in reality, are just objects that tell Dart in what order to access objects one at a time. This is what allows us to use language features like for in loops.

50. dart group by data?

var data = [{“title”: ‘Android’, “release_date”: ’10/01/2019′}, {“title”: ‘Flutter Tutorial’, “release_date”: ’10/01/2019′}, {“title”: ‘Java’, “release_date”: ’30/10/2019′}];
var groupByData = groupBy(data, (obj) => obj[‘release_date’]);

51. join() (List get string comma separated value)

var values = [‘dart’, ‘flutter’, ‘programming’];
print(values.join(‘,’));

52. Comma separated list remove the space.

var values = [‘ dart’, ‘flutter ‘, ‘programming’];
String result = values .map((val) => val.trim()).join(‘,’);
print(result);

53. Dart total sum using fold?

var foldDart = listASCGiveCharge.fold(0, (t, e) => t + e.amount * e.qty);
print(‘foldDart’ + data.toString());

54. Sort a Map in Dart

We are going to check out how to quickly sort a Map in Flutter/Dart using fromEntries() and List sort() method.

Map myMap = {1: ‘Apple’, 2: ‘Orange’, 3: ‘Cherry’, 4: ‘Plum’};
var mapSort = Map.fromEntries( myMap.entries.toList()..sort((e1, e2) => e1.value.compareTo(e2.value)) );

Key using sorted
var sortedByValue = new SplayTreeMap.from(fruit, (key1, key2) => fruit[key1].compareTo(fruit[key2]));

Value using sorted
var sortedByValue = new SplayTreeMap.from(fruit, (key1, key2) => fruit[key1].compareTo(fruit[key2]));

55. How to use Map syntax (literal)?

To declare a list, we use [] (array bracket) and to declare maps we have to use {}(curly braces).
var myMap = {“name”: “kamlesh”};
Map map = Map< , >();

Dart Interview Questions Help You to Learn Dart and and Crack the Dart Interview Easily

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application.

For more information about Flutter. visit www.fluttertutorial.in