Posted in

Top 30 Dart Interview Questions and Answers for All Experience Levels

Basic Dart Interview Questions (1-10)

1. What is Dart programming language?

Dart is an object-oriented programming language developed by Google, primarily used for building fast applications across platforms with features like just-in-time (JIT) and ahead-of-time (AOT) compilation.[1][8]

2. What are the key features of Dart?

Dart offers object-oriented programming, garbage collection, async support with Futures and Streams, fast compilation, and rich standard libraries for math, crypto, and HTML.[1][3]

3. How do you define a variable in Dart?

Variables in Dart are declared using var, final, or const. The type can be inferred or explicitly specified.[6]

var name = 'Dart';
final age = 25;
const pi = 3.14;

4. What is the difference between var, dynamic, and Object in Dart?

var is type-inferred and immutable at compile time, dynamic allows runtime type changes, and Object is the root class where all types are subtypes.[4]

5. What is a Dart string and how do you create one?

Dart strings are sequences of UTF-16 code units created using single, double quotes, or triple quotes for multi-line strings.[3]

String single = 'Hello Dart';
String multi = '''
Multi-line
string
''';
String interpolated = 'Age: $age';

6. What are the types of operators in Dart?

Dart supports arithmetic (+, -, *, /), assignment (=, +=), relational (==, !=, >), logical (&&, ||, !), bitwise (&, |, ^), conditional (?:), and cascade (..) operators.[1]

7. What is the main() function in Dart?

The main() function is the entry point of every Dart application where execution begins.[2]

void main() {
  print('Hello, Dart!');
}

8. What is Fat Arrow Notation in Dart?

Fat arrow notation (=>) provides a concise way to write single-expression functions or methods.[4]

var add = (int a, int b) => a + b;
print(add(2, 3)); // 5

9. What are the types of lists in Dart?

Dart has fixed-length lists (immutable size) and growable lists (resizable at runtime).[3]

var fixed = new List.filled(3, 0); // Fixed length
var growable = <int>[1, 2, 3]; // Growable

10. What is assert used for in Dart?

assert is used for debugging to test conditions that should always be true at runtime; it throws an error if false.[4]

assert(age > 0, 'Age must be positive');

Intermediate Dart Interview Questions (11-20)

11. What is a class in Dart and what is its blueprint purpose?

A class in Dart is a blueprint for creating objects, defining properties and behaviors for instances.[2]

class Car {
  String color;
  Car(this.color);
}

12. What is the default constructor in Dart?

The default constructor has no parameters and shares the class name, automatically generated if none is defined.[2][5]

13. Can you define named constructors in Dart? Provide an example.

Yes, named constructors allow multiple constructors with different names for object creation.[2]

class Person {
  String name;
  Person(this.name);
  Person.named(String n) : name = n;
}

14. What are instance variables vs local variables in Dart?

Instance variables belong to class objects and persist across methods; local variables are scoped to functions/blocks.[2]

15. What is polymorphism in Dart?

Polymorphism allows objects to share the same interface while having different implementations, enabling code reusability.[1]

16. What are the types of inheritance in Dart?

Dart supports single and multilevel inheritance where child classes derive properties from parent classes.[1]

class Animal { void eat() {} }
class Dog extends Animal { void bark() {} }

17. What are named parameters in Dart?

Named parameters are passed by name, making function calls more readable and allowing optional arguments.[5]

void greet({String? name, int? age}) {
  print('Hello $name, age $age');
}
greet(name: 'Alice', age: 30);

18. What is method overriding in Dart?

Method overriding allows a subclass to provide a specific implementation for a parent class method using @override.[3]

class Animal {
  void sound() => print('Animal sound');
}
class Cat extends Animal {
  @override
  void sound() => print('Meow');
}

19. What is Typedef in Dart?

Typedef creates an alias for a function type, improving code readability and reusability.[4]

typedef IntList = List<int>;
typedef Calculate = int Function(int, int);

20. What are conditional expressions in Dart?

Conditional expressions include ternary (condition ? expr1 : expr2) for concise if-else logic.[2]

var result = age > 18 ? 'Adult' : 'Minor';

Advanced Dart Interview Questions (21-30)

21. How do you handle exceptions in Dart?

Dart uses try-catch-finally blocks to handle exceptions gracefully.[1]

try {
  int result = 10 ~/ 0;
} catch (e) {
  print('Error: $e');
} finally {
  print('Cleanup');
}

22. What is a pub in Dart?

Pub is Dart’s package manager for managing dependencies, libraries, and applications through pubspec.yaml.[1]

23. What are switch case statements in Dart?

Switch statements execute different code blocks based on matching cases, replacing multiple if-else chains.[2]

switch (day) {
  case 1: print('Monday'); break;
  default: print('Other day');
}

24. What are the different types of constructors in Dart?

Dart supports default, parameterized, named, constant, and factory constructors for flexible object initialization.[2]

class Point {
  final num x, y;
  const Point(this.x, this.y);
  factory Point.origin() => Point(0, 0);
}

25. Explain asynchronous programming in Dart.

Dart uses Future and Stream for async operations, with async/await for readable non-blocking code.[5]

Future<int> fetchData() async {
  await Future.delayed(Duration(seconds: 2));
  return 42;
}

26. What is the extension method in Dart?

Extensions add functionality to existing classes without modifying them.[4]

extension on String {
  bool get isPalindrome => this == this.split('').reversed.join('');
}

27. What are mixins in Dart and how do they differ from interfaces?

Mixins provide reusable code through implementation sharing using with, while interfaces define contracts via implements.[4]

mixin Flyable { void fly() {} }
class Bird with Flyable {}

28. What are the differences between JIT and AOT compilation in Dart?

JIT compiles at runtime for faster development, while AOT compiles ahead-of-time for optimal production performance.[4]

29. Scenario: At Zoho, how would you implement a growable list to track user scores dynamically?

Use a growable List<int> and add elements as needed.

List<int> scores = [];
scores.add(95);
scores.addAll([88, 92]);
print(scores); // [95, 88, 92]

30. Scenario: At Atlassian, design a parking lot system using Dart classes for different vehicle types.

Create a base Vehicle class with subclasses inheriting common properties.

abstract class Vehicle {
  String get type;
  int get size;
}
class Car implements Vehicle {
  @override String get type => 'Car';
  @override int get size => 1;
}
class ParkingLot {
  List<Vehicle> slots = List.filled(100, null);
}

Leave a Reply

Your email address will not be published. Required fields are marked *