Dart interview questions and answers

CodingWithTashi
7 min readJun 25, 2022

--

Whether you are a fresher or an experienced developer, it is always good to enhance your knowledge by reading/practicing interview questions and answers and dart is no difference.

Hello guys, this is CodingWithTashi and in this shitty post, we are going to discuss some of the commonly asked questions during an interview.

Medium Referral Program: If you like the experience here on medium, consider supporting me and thousands of other writers by signing up for a membership. By signing up with this link, you’ll support me directly with a portion of your fee, it won’t cost you more. If you do so, thank you a million times!

Note:

  • We all know Almost everything in flutter is widgets and but what we didn’t realize is that Dart was the guy through which all these widgets are built. needless to mention all the business logic, model class, service layer, etc. that you are implementing in your application.
  • Last week I wrote a post on flutter interview questions and answers, and I realized that dart is equally important as well. There are tons of questions that the interviewer might ask even if you are an experienced, developer.
  • I don’t deserve all the credit here; These interview questions and answers are prepared from my knowledge, Stack overflow post, and dart documentation.

With that said, Let's get started with the Junior level interview questions first

Dart junior-level interview questions and answers

You might already know these answers if you are an experienced, developer. But it is always good to brush up on your knowledge.

  1. What is a Dart?

Dart is a programming language designed for building mobile, web, and desktop applications. It is developed by Google and can also be used to build server applications.

2. What are the data types in Dart.

Dart data types include a number (int, double, num), string, Boolean, list, and map.

3. Is dart Object Oriented? If so explain.

Yes, Dart is an object-oriented program. Dart can have

  • Classes
  • Objects
  • Data Encapsulation
  • Inheritance
  • Inheritance

4. Explain an interface in dart with an example?

Unlike other languages dart does not have any interface keyword, You can implement it with an abstract keyword

abstract class Joker{
void makePeopleLaugh();
}

class Clown implements Joker{
void makePeopleLaugh() {
// Here is where the magic happens
}
}

class Comedian implements Joker{
void makePeopleLaugh() {
// Here is where the magic happens
}
}

5. What is the difference between final and const?

Const: Value must be known at compile-time, const birthday = "2008/12/25" and it can't be changed after initialized.

Final: Value must be known at run-time, final birthday = getBirthDateFromDB() and it can't be changed after initialized.

6. What is sound null safe in dart?

Dart’s null safety is sound, which enables compiler optimizations. If the type system determines that something isn’t null, then that thing can never be null.

7. What is the late keyword used for?

In Dart, we use the late keyword to declare variables that will be initialized later point in time.

8. How to run Dart application?

You can run any dart application by providing main as an entry method.

void main() {
for (int i = 0; i < 5; i++) {
print('hello ${i + 1}');
}
}

10. What “is” and “as” keywords in dart?

is keyword is used to check variable data type whereas “as” keyword is used to give alias name.

is Keyword

void main() {
dynamic a="test";
if(a is String){
print("string");
}
}

as Keyword

import 'package:flutter/material.dart' as m;

11. How do you catch an error in the Dart program?

Unlike other languages, dart try and catch syntax are different

try {
// ...
} on SomeException catch(e) {
//Handle exception of type SomeException
} catch(e) {
//Handle all other exceptions
}

12. What is a constructor? Name types of constructor in dart?

A constructor is a special function of the class that is responsible for initializing the variables of the class.

There are three types of constructors in Dart as given below.

  • Default Constructor or no-arg Constructor.
  • Parameter Constructor.
  • Named Constructor.

13. Implement getter and setter in dart?

You can use the “get ”and “set ” keywords to implement getter and setter.

Example to get and set id:

double get id => _id;

set id(double value) {
_id = value;
}

14. What are “async”, “await ”and “Future”?

In dart, Async means that this function is asynchronous, and you might need to wait a bit to get its result.

Await literally means — wait here until this function is finished and you will get its return value.

Future is a type that “comes from the future ” and returns a value from your asynchronous function. It can complete with success(.then) or with an error(.catchError)

Mid-level dart interview questions and answers

  1. What is the extension function?

Extension methods are adding new functionality to existing features.

Eg. to convert a string to an integer we use int.parse('42')but with extension, we can add function like '42'.parseInt()instead of int.parse('42') .

2. What are the differences between Object and dynamic?

dynamic is not a type; it just disables type checking. The object is the ‘union’ of all non-nullable types, type checking rules still apply.

With dynamic

// a 'dynamic' variable can be assigned value of any type
dynamic a = 2;

// assign 'dynamic' value to any variable and code checker will not complain
int b = a;
// even when there is a bug
String c = a;

With Object

// It is OK to assign a 'int' value to an 'Object' variable, because 'int' is a subtype of 'Object'
Object a = 2;

// will get type error: "A value of type 'Object' can't be assigned to a variable of type 'int'"
int b = a;

// typecast is required when assign a 'Object' value to a variale of one of its subtypes.
int c = a as int;

3. What is assert in dart?

assert statements are useful for debugging a dart project. It is used mainly in development mode. assert takes one expression and checks if it is true or false. If it is true, the program runs normally and if it is false, it stops the execution and throws one error called AssertionError.

4. What is Cascade notation in dart?

Cascades (.., ?..) allow you to make a sequence of operations on the same object. In addition to accessing instance members, you can also call instance methods on that same object.

var paint = Paint()
..color = Colors.black
..strokeCap = StrokeCap.round
..strokeWidth = 5.0;

5. What is the spread operator in dart?

Dart supports the spread operator, which allows inserting multiple elements into a collection.

Instead of calling add() or addAll(), You can simply use … operator to insert multiple records

return [
...someVariable.toList(),
anotherObject,
...anotherListOfObjects
];

6. What is “mixin”? When do we use it?

Mixins are very helpful when we want to share behavior across multiple classes that don’t share the same class hierarchy, or when it doesn’t make sense to implement such behavior in a superclass

7. What is isolate?

To achieve concurrency dart, make use of isolate. Isolate is similar to multithread in other languages.

8. Is Method Overloading possible in dart? If not, what is the alternative approach?

Dart doesn’t have overloading as it is our experience that overloading leads to confusion. Especially when you intend to override a method but accidentally overload it.

We can use optional arguments (named or not) as a better alternative.

Senior level dart interview questions and answers

  1. How to achieve “isolate ”in dart?

We can use Dart compute and spawn method to achieve “isolate”. For more, you can read here.

2. Implement the extension method with an example

You can simply create an extension using the “extension ”keyword.

extension FancyNum on num {
num plus(num other) => this + other;

num times(num other) => this * other;
}

Then you can easily use it like below

print(5.plus(3)); // Equal to "5 + 3".
print(5.times(8)); // Equal to "5 * 8".
print(2.plus(1).times(3)); // Equal to "(2 + 1) * 3".

3. How does Dart/Flutter get compiled to native android or ios?

Dart is compiled to native machine code (ARM, Intel, …) executable and bundled with native platform code (Java, Kotlin, Objective-C/Swift) to interact with the native platform.

For more read from here and here

4. Does dart uses VM, if so, how?

Yes, Dart VM is being used when you build your mobile app. Dart VM has two different operation modes a JIT one and an AOT one.

In the JIT mode, Dart VM is capable of dynamically loading Dart source, parsing it, and compiling it to native machine code on the fly to execute it. This mode is used when you develop your app and provides features such as debugging, hot reload, etc.

In the AOT mode, Dart VM does not support dynamic loading/parsing/compilation of Dart source code. It only supports loading and executing precompiled machine code. However even precompiled machine code still needs VM to execute, because VM provides a runtime system that contains garbage collector, various native methods needed for dart:* libraries to function, runtime type information, dynamic method lookup, etc. This mode is used in your deployed app.

You can read more about how Dart VM executes Dart code here.

Note: This is not the end of post, I will actively update this post base on my knowledge and your feedback. If there is any mistake, please do let me know.

That’s all for now, Thanks guys, I hope you like this shitty post. please make sure to give a clap 👏 and leave some engagement.

Check out more similar posts from CodingWithTashi. Connect me on Twitter, Linkedin

--

--

CodingWithTashi
CodingWithTashi

Responses (1)