Dart is a general-purpose, open-source programming language developed by Google. It is generally used for Android and iOS mobile app development using the Flutter framework. It is also used for server-side development and desktop applications.
Dart is based on object-oriented programming principles and has a C-style syntax, making it an easy-to-learn programming language.
Let’s review some Dart interview questions and answers to help you prepare for your next Dart job interview.
Below are the main features of Dart:
Dart is cross-platform compatible
It is a type-safe language
Based on Object Oriented Programming principles
Automatic memory management with garbage collection
Supports Just-in-time and ahead-of-time compilation
Supports mixins, allowing classes to inherit properties without establishing a direct parent-child relationship
Control the flow of code execution with conditional statements and loops
Null safety provided by default non-nullable code
Dart used both Just-in-Time and Ahead-of-Time compilation.
Ahead-of-time compilation is used to convert the source code of the program to machine code before it is executed. AOT compilation usually takes place during the build process.
Just-in-time compiler translate the source code to machine code at runtime.
While AOT compilation leads to efficient execution and faster startup, JIT compilation is slow at startup but can have higher peak performance during execution.
The following are the different ways to execute a Dart program.
1. Command-Line
2. Dart console applications
The following are the different types of Dart Language:
Booleans: Dart uses the bool keyword to represent a Boolean Value
Strings: To create a string, you can use single quotes or double quotes
Lists: A list is an ordered group of objects
Maps: Represents a set of values in the Key Value Pair
The Dynamic Type: Used as a type annotation explicitly
Assert statements in Dart are used for debugging. They help identify logical errors during development rather than during production.
The assert statement uses a boolean condition as its argument and if the boolean condition is true, then the program continues to execute. If the value of the condition is false, indicating that the assertion or the condition is not met, then the program terminates with an AssertionError.
The following is the syntax for assert:
assert(booleanCondition, optionalMessage);
The keyword dynamic declares a variable whose value can change at runtime. A variable of any value, such as int, double, string, or float, can be declared dynamic.
Syntax:
dynamic variable_name
Exception handling in Dart is done with try-catch blocks, like in most programming languages.
When an error occurs, an exception object is thrown. This is done implicitly by the Dart runtime system or explicitly by code using the throw keyword.
The control flow then takes the exception object to the matching catch block. If the matching catch block exists, then the control flow jumps to that catch block to handle the exception.
If there are no catch blocks found, Dart propagates the exception up the call stack until a suitable catch block is found. If still a matching catch block is not found, then the program terminates.
While function declaration is a way to declare a function, function expression is a way to define an anonymous function in Dart, meaning the function does not have a name. Dart also supports named function expressions, but the function name is local to the function itself and is not accessible to the surrounding scope.
In Dart, a future is an object of the class Future. It is used in asynchronous operations as a return value that will eventually be completed since asynchronous operations cannot produce an immediate result.
The future can be in a completed and uncompleted state.
A completed future completes with a value when an asynchronous operation succeeds. If the synchronous operation fails, it completes with an error.
A future is uncompleted when waiting for the asynchronous operation to finish or throwing an error.
Lists in Dart are an ordered list of objects; each object is identifiable by an index.
Syntax:
List numbers = [1, 2, 3, 4];
A set in Dart is an unordered collection of unique items
Syntax:
Set week ={‘Sunday’, ‘Monday’, ‘Tuesday, Thursday’, ‘Friday’, ‘Saturday’}
The super keyword refers to the instance of the immediate parent class of the current child class.
The keyword super can be especially helpful when the parent class and the child class have properties or methods with the same name. The super keyword can be used to refer to the property of the parent class, avoiding ambiguity.
To understand the Asynchronous programming in Dart, we must understand the Future and Stream classes.
The object future of the class Future holds the result of an asynchronous program that will eventually be completed and contains the result once the program execution succeeds.
A stream is a sequence of asynchronous events. It is used for operations that produce multiple results over time.
The async and await keywords can be used to perform an asynchronous function in a more structured and readable manner. The async keyword defines an asynchronous function; the await keyword waits for a future to complete.
These concepts allow you to Dart applications that can handle complex asynchronous applications.
The “is” keyword in Dart checks variable datatype.
Example:
void main() { dynamic car="Maruthi"; if(car is String){ print("string"); } }
The “as” keyword in Dart gives an alias name.
Example:
import 'package:flutter/material.dart' as p;