Dart Quiz

Dart Quiz

Assessment

Quiz

Science

Hard

Created by

Alvin Tseng

Used 1+ times

FREE Resource

Student preview

quiz-placeholder

12 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which of the following is a requirement for generative constructors in an enhanced enum in Dart?

Must be non-constant

Must be abstract

Must be final

Must be static

Answer explanation

在 Dart 中,增強型列舉(enhanced enums)中的生成構造器(generative constructors)必須是 非常量的(Must be non-constant)。這是因為增強型列舉允許列舉值在構造時執行一些邏輯或操作,而這些操作通常需要使用非常量的構造器。

2.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

How can you access enumerated values in Dart?

Through extension methods

Through static variables

Through instance methods

Through static methods

Answer explanation

In Dart, you can access enumerated values through static variables.

3.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is the purpose of using extension methods in Dart?

To add new functionality to existing classes

To remove functionality from existing classes

To modify existing classes

To create new classes

Answer explanation

Extension methods in Dart are used to add new functionality to existing classes, allowing developers to extend the capabilities of those classes without modifying their source code.

4.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which of the following is a valid example of an extension method in Dart?

extension { ... }

extension on String { ... }

extension StringParsing { ... }

extension StringParsing on String { ... }

Answer explanation

The correct choice is 'extension StringParsing on String { ... }' because it follows the syntax for defining an extension method in Dart by specifying the extension name 'StringParsing' and the target type 'String'.

5.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is the benefit of using unnamed extensions in Dart?

They are only visible within the declaring library

They are visible across all libraries

They are faster than named extensions

They can be explicitly applied to resolve API conflicts

Answer explanation

In Dart, unnamed extensions are extensions without a specific name. The primary benefit of using unnamed extensions is that they are only visible within the library where they are declared. This means that they do not leak into other libraries, providing better encapsulation.

Benefits of unnamed extensions:

Encapsulation: Since unnamed extensions are only visible within the declaring library, this helps in preventing unintended usage or conflicts in other libraries.

Avoiding API conflicts: By keeping the extension private to the library, it avoids the risk of API conflicts when multiple libraries define similar extensions on the same type.

6.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

How can you access static members defined in an extension method in Dart?

By using the extension method's prefix

By importing the extension method

By using the extension method's name

By calling the static members directly

Answer explanation

### Correct Answer

By using the extension method's name

### Explanation

In Dart, static members defined in an extension method can be accessed by using the extension's name, followed by the static member's name. This is different from instance members, which are accessed through the object that the extension is applied to.

For example:

```dart

extension StringExtension on String {

static String get empty => '';

int get lengthPlusTwo => this.length + 2;

}

void main() {

// Accessing a static member

print(StringExtension.empty); // Outputs: ''

// Accessing an instance member

print('hello'.lengthPlusTwo); // Outputs: 7

}

```

In this example, `StringExtension.empty` accesses the static member `empty` of the `StringExtension`. This is how you access static members defined in an extension method.

7.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is a key feature of implementing generic extensions in Dart?

They can only be applied to specific data types

They allow for dynamic typing

They can have type parameters

They are limited to primitive data types

Answer explanation

### Correct Answer

They can have type parameters

### Explanation

A key feature of implementing generic extensions in Dart is that they can have type parameters. This allows you to create extensions that work with a variety of data types while still being type-safe.

For example:

```dart

extension ListUtils<T> on

Create a free account and access millions of resources

Create resources
Host any resource
Get auto-graded reports
or continue with
Microsoft
Apple
Others
By signing up, you agree to our Terms of Service & Privacy Policy
Already have an account?