Search Header Logo

Vispro Week 1 - OOP concept in DART

Authored by Kasmir Syariati

Computers

University

Used 1+ times

Vispro Week 1 - OOP concept in DART
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

33 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is DART?

programming language

programming framework

programming architecture

programming paradigm ✅

2.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Media Image

What will be the type of x in the following Dart code?

dynamic

int

Object

num

Answer explanation

When declared with var, Dart infers the type from the first assignment. Here, x becomes int. If you want it to be truly dynamic, you must explicitly declare dynamic.

3.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which of the following correctly explains the difference between final and const in Dart?

final can be set only once at runtime, const is a compile-time constant

const can be reassigned, final cannot

Both are identical

final must always be declared with a value, const does not

Answer explanation

final is initialized once and can hold a runtime value. const must be assigned a compile-time constant.

4.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Media Image

What will this Dart code print?

null

Guest

Compilation error

Empty string

Answer explanation

Since name is nullable and currently null, the null-coalescing operator ?? provides "Guest" as the fallback.

5.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which scenario is the best use case for the late keyword in Dart?

When you want to delay initialization of a non-nullable variable

When you want to make a variable nullable

When you want a compile-time constant

When you want to create a global variable

Answer explanation

late is used when a non-nullable variable cannot be initialized immediately, but you guarantee it will be set before use.

The late keyword in Dart is used to declare a variable or field that will be initialized at a later time, specifically when it is first accessed. This keyword is particularly useful in the context of Dart's sound null safety, as it allows for the declaration of non-nullable variables without requiring immediate initialization. 

The primary use case for late is to declare non-nullable variables that cannot be initialized at the point of declaration. Without late, such variables would either need to be marked as nullable (using ?) or initialized immediately, which might not always be feasible.

6.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Media Image

What will this Dart code print?

42

"hello"

Compilation error

dynamic

Answer explanation

A dynamic variable can change its type at runtime. Initially it is an int, then reassigned to a String.

In Dart, the dynamic keyword allows for the declaration of a variable whose type can change at runtime. This means a variable declared with dynamic can hold values of any type, and its type can be reassigned throughout its lifecycle. 

Runtime Type Flexibility:

Unlike var (which infers a type at compile-time and then restricts the variable to that type), dynamic truly allows the type to change during program execution.

  • Reduced Type Safety:

    While offering flexibility, using dynamic sacrifices compile-time type checking. This means potential type-related errors might only be discovered at runtime, leading to less robust code compared to using specific types or var with type inference.

  • Use Cases:

    dynamic is typically used when the exact type of a variable is genuinely unknown or when interoperating with external systems (like JSON parsing) where the data structure might be unpredictable.

  • Best Practice:

    In general, it is recommended to use dynamic sparingly and only when necessary due to the reduced type safety. Prefer specific types or var with type inference for better code maintainability and error prevention.

7.

MULTIPLE CHOICE QUESTION

30 sec • 2 pts

What is the purpose of extension methods in Dart?

To allow adding new methods to existing classes without modifying them

To create subclasses dynamically

To override private methods

To replace inheritance

Answer explanation

Media Image

Extension methods in Dart allow developers to add new functionalities to existing classes without modifying their original source code or using inheritance. This feature is particularly useful for extending the behavior of classes from core libraries or third-party packages that cannot be directly altered.

  • Extension methods enable the addition of methods, getters, setters, and operators to a class as if they were part of its original definition.

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Classlink

Continue with Classlink

Clever

Continue with Clever

or continue with

Microsoft

Microsoft

Apple

Apple

Others

Others

Already have an account?