Lets look up few question on dart which help us to understand whether dart is easy or not.
Introduction:
Dart Question |
Question on Dart
1) For what iterable Collection in dart?
Ans : The iterable Collection in dart :The Iterable class—for example List and Set. Iterables are basic building blocks for all sorts of Dart applications, and you’re probably already using them, even without noticing. This codelab helps you make the most out of them.
2)What are type of loops in dart programming?
Ans :/ for in loop ,switch
Dart programming language supports several types of loops for controlling the flow of execution. Here are the main types of loops in Dart:
- For Loop:
- The
for
loop is used to iterate a specific number of times. It consists of an initialization, a condition, and an iteration statement.
for (int i = 0; i < 5; i++) {print(i);} - The
- While Loop:
- The
while
loop repeats a block of code as long as a specified condition is true.
int i = 0;while (i < 5) {print(i);i++;} - The
- Do-While Loop:
- The
do-while
loop is similar to thewhile
loop, but the condition is evaluated after the block of code is executed, meaning the block of code will always run at least once.
int i = 0;do {print(i);i++;} while (i < 5); - The
- For-In Loop:
- The
for-in
loop is used to iterate over elements of an iterable (such as a list or a map).
List<int> numbers = [1, 2, 3, 4, 5];for (int number in numbers) {print(number);} - The
- For-Each Loop:
- Dart also supports a more concise way to iterate over elements using the
forEach
method.
List<int> numbers = [1, 2, 3, 4, 5];numbers.forEach((int number) {print(number); }); - Dart also supports a more concise way to iterate over elements using the
These loops provide flexibility for different looping scenarios in Dart. You can choose the loop type that best fits the requirements of your code.
Class related question
3) Do dart has overloading and overriding like feautes ?
Ans:dart do have overriding feature.
Dart don't have over loading feature.
Dart does not support traditional method overloading with multiple methods having the same name but different parameter lists. However, Dart supports named parameters and default values, providing a form of optional parameters. This allows you to achieve similar functionality by specifying default values for parameters or using named parameters with different combinations.
4) Do dart multiple Inheritance?
Ans:
5 ) Does Dart has Interface Concept?
Ans 》Yes, Dart doesn't have a dedicated "interface" keyword, but it supports abstract classes and implements keyword to achieve similar functionality. You can create abstract classes with abstract methods, and then a class can implement those methods using the implements keyword, effectively serving as an interface in Dart.
6 ) Does Dart supports all types of Inheritance?
Ans 》Dart does not support all types of inheritance. It only supports single inheritance, which means that a class can only inherit from one parent class. This is in contrast to some other programming languages, such as C++, which support multiple inheritance, where a class can inherit from multiple parent classes.
Dart does not support multiple inheritance for several reasons. One reason is that multiple inheritance can make code more difficult to understand and maintain. When a class inherits from multiple parent classes, it can be difficult to determine which parent class a particular method or property comes from. This can lead to confusion and errors in code.
Another reason why Dart does not support multiple inheritance is that it can make code more difficult to compile. When a class inherits from multiple parent classes, the compiler must determine which method or property to call when a method or property is accessed on an instance of the class. This can be a complex task, and it can make the compiler more difficult to write and maintain.
Dart does, however, support mixins. Mixins are a way of sharing code between classes without using inheritance. Mixins are similar to interfaces, but they can also include implementation code. This means that mixins can be used to add new methods and properties to classes without modifying the code of the classes themselves.
Mixins are a powerful tool for code reuse, and they can be used to achieve many of the same things that multiple inheritance can be used for. However, mixins are not as complex as multiple inheritance, and they can make code more easier to understand and maintain.
7 ) Does dart support support all class features like polymorphism, Inheritance ,abstract,data hiding ,data linking ?
Ans 》Yes, Dart supports single inheritance, where a class can inherit from only one superclass. However, Dart allows for mixin-based inheritance, which enables a class to reuse code from multiple sources. This provides flexibility in code sharing without the constraints of traditional multiple inheritance.
8) How Dart is different from Javascript?
Ans: )
Dart and JavaScript are programming languages with key differences:
- Typing:
- Dart is optionally statically typed, allowing type specification but not enforcing it.
- JavaScript is dynamically typed, determining types at runtime.
- Compilation:
- Dart can be compiled ahead-of-time (AOT) or just-in-time (JIT), offering performance advantages.
- JavaScript is executed by browsers at runtime, but tools like Babel can transpile it to older versions or different ECMAScript standards.
- Usage:
- Dart is commonly used with the Flutter framework for cross-platform mobile, web, and desktop applications.
- JavaScript is the primary language for web development, used for both front-end and back-end.
- Frameworks:
- Dart has Flutter for UI-focused apps and AngularDart for web apps.
- JavaScript has numerous frameworks like React, Angular, and Vue.js for front-end, and Node.js for server-side.
- Asynchronous Programming:
- Dart has native support for asynchronous programming with
async
andawait
.
- JavaScript uses callbacks, promises, and more recently, async/await for handling async operations.
- Dart has native support for asynchronous programming with
- Tooling:
- Dart has an integrated development environment, and DartPad for online experimentation.
- JavaScript has various tools, including VSCode, Webpack, and Babel for transpilation.
- Community and Ecosystem:
- JavaScript has a larger, mature ecosystem with numerous libraries and frameworks.
- Dart's ecosystem is growing, particularly with Flutter, but not as extensive as JavaScript's.
Understanding these differences is crucial when choosing between Dart and JavaScript for a project, considering factors like performance, platform, and preferences.
9) Does dart has array functions?
yes dart does support array functions.
Dart doesn't have a built-in data structure called an "array," but it has a similar concept called a "List." In Dart, you can use the List
class to work with ordered collections of items. Here are some common operations related to lists in Dart:
-
Creating a List:
- You can create a list in Dart using the
List
constructor or by using a list literal.
List<int> numbers = List();numbers.add(1);numbers.add(2);numbers.add(3);// Using list literalList<String> fruits = ['Apple', 'Banana', 'Orange']; - You can create a list in Dart using the
-
Accessing Elements:
- You can access elements in a list using square brackets and the index of the element.
print(numbers[0]); // Output: 1print(fruits[1]); // Output: Banana -
Updating Elements:
- You can update the value of an element at a specific index.
fruits[0] = 'Mango'; -
Iterating Through a List:
- You can use various loops or the
forEach
method to iterate through the elements of a list.
for (int number in numbers) {print(number);}fruits.forEach((fruit) {print(fruit);}); - You can use various loops or the
-
List Length:
- You can get the length of a list using the
length
property.
print(numbers.length); // Output: 3 - You can get the length of a list using the
Adding and Removing Elements:
- You can use methods like
add
,addAll
,remove
,removeAt
,clear
, etc., to modify the contents of a list.
numbers.add(4);numbers.addAll([5, 6]);fruits.remove('Banana');- You can use methods like
These are some basic operations you can perform with lists in Dart. Dart's List
class provides various other methods for working with lists efficiently. Keep in mind that Dart lists are dynamically sized and can contain elements of different types unless you specify a specific type using generics, as shown in the examples.
10) What this Error tell and what you need to do?
test.dart:4:3: Error: Expected an identifier, but got '3243'.
Try inserting an identifier before '3243'.
Ans>> identifier are class like String,void inside functions
Error code:
Correct code :
This just function declaration using arrow function.
11) Give a example of aync ,await,future in dart code ?
Ans)) Here is an example of using async, await, and Future in Dart code:
Future<String> fetchData() async { await Future.delayed(Duration(seconds: 2)); // Simulating a network delay return 'Data fetched successfully';}
void main() { print('Fetching data...'); fetchData().then((result) { print(result); }); print('Continuing with other tasks...');}
Output :
Fetching data...
Continuing with other tasks...
Data fetched successfully
In this example, the fetchData
function returns a Future<String>
which represents an asynchronous operation. The await
keyword is used to pause the execution of the function until the Future
completes. Meanwhile, the print('Continuing with other tasks...')
statement is executed immediately. The then
method is used to handle the result of the asynchronous operation once it is completed.
12) Why assert() Key word is used for?
Ans : assert() function simply return true or false based on suggestions.
Example assert(2==2);
13) Why Future is import keyword in dart?
Future function has many roles that we need to observe.
- When declaring a function with return type as Future<String> we could use async and await like keyword. and under return type we could use
FunctionCalling().then((result){ print(result);})
- I have tried making function without Future<String> return type mean to say like this
String fetchData() {Future.delayed(Duration(seconds: 23)); // Simulating a network delayreturn 'Data fetched successfully';}
function return the string and output differs means for above code
'Fetching data... Data fetched successfully Continuing with other tasks...
with async function and await executes lastly.and Without future control executes normally.
In Dart, when using async
and await
, the execution order is different from non-async code. In an asynchronous function, the await
keyword is used to pause the execution of the function until the awaited operation completes. This allows other code to run while waiting for the awaited operation to finish.
Comments
Post a Comment