Dart Classes & Mixins Questions with Answers

15 practice questions on Dart Classes & Mixins (Flutter & Dart) with answers and step-by-step solutions. Try 10 samples below, then practise all 15 free in the AptiNest app.

Sample questions

Question 1Easy

Which keyword applies a mixin to a class in Dart?

  1. implements
  2. mixin
  3. extends
  4. with

Answer: D. with

Explanation

class C extends B with M {} applies mixin M. mixin is the keyword for declaring one, not applying it.

Step-by-step solution

  1. Mixins are applied with with.
  2. class C extends B with M.

Why the other options are wrong

  • A. implements is for interfaces.
  • B. mixin declares a mixin.
  • C. extends is for inheritance.

Tip: with = apply mixin.

Question 2Easy

Which is a named constructor in Dart?

  1. Point::origin() { x = 0; y = 0; }
  2. new Point origin() { x = 0; y = 0; }
  3. constructor origin() { x = 0; y = 0; }
  4. Point.origin(): x = 0, y = 0;

Answer: D. Point.origin(): x = 0, y = 0;

Explanation

Named constructors use the ClassName.name syntax, which lets a class offer several clearly named ways to create instances.

Step-by-step solution

  1. Named constructor uses ClassName.name syntax.
  2. Point.origin() is correct named constructor syntax.
  3. Colon: is used for initializing variables in the constructor.
  4. x = 0, y = 0 sets initial values correctly.
  5. Final answer: Point.origin(): x = 0, y = 0;

Why the other options are wrong

  • A. Point::origin is C++ scope syntax, not a Dart named constructor, which uses a dot between names.
  • B. The new keyword with a space is not Dart constructor naming; the name follows a dot after the class.
  • C. constructor is not a Dart keyword; named constructors use ClassName.name, as in Point.origin.

Tip: Use ClassName.name() for named constructors in Dart.

Question 3Easy

Dart has no separate interface declaration in classic syntax. How do you define an interface?

  1. Interfaces are declared using the mixin keyword instead
  2. Dart does not support interfaces; only extends is allowed
  3. Only abstract classes can be used with the implements keyword
  4. Every class implicitly defines one, usable with implements

Answer: D. Every class implicitly defines one, usable with implements

Explanation

Each class implicitly exposes an interface of all its instance members. Writing class A implements B requires A to provide all of B's members.

Step-by-step solution

  1. Every class in Dart automatically defines an interface for its public members.
  2. Using implements forces a class to provide all the members of the interface.
  3. This means every class implicitly has an interface that can be implemented.
  4. So, any class can be used with implements as it already defines an interface.
  5. Therefore, every class implicitly defines one interface usable with implements.
  6. Final answer: Every class implicitly defines one, usable with implements.

Why the other options are wrong

  • A. mixin declares a reusable code block, not an interface; a class can be implemented without that keyword.
  • B. Dart does support interfaces: every class can be implemented with the implements keyword.
  • C. Abstract classes are not the only option; any class can be used with implements, abstract or not.

Tip: Remember, every class in Dart has an implicit interface you can implement.

Question 4Easy

How do you make a class member private in Dart?

  1. Prefix its name with an underscore
  2. Annotate it with @private above the declaration
  3. Mark it with the private keyword before its type
  4. Declare it inside a separate private { } block

Answer: A. Prefix its name with an underscore

Explanation

Dart has no private keyword. A leading underscore hides the identifier from other libraries (typically other files).

Step-by-step solution

  1. Dart does not have a private keyword for class members.
  2. To hide a member from other libraries, prefix its name with an underscore.
  3. This makes the identifier inaccessible outside the current library.
  4. Other options like @private or separate blocks are not valid in Dart.
  5. The correct way is to use a leading underscore for privacy.

Why the other options are wrong

  • B. @private is not a Dart annotation; Dart uses the leading underscore for library-level privacy.
  • C. Dart has no private keyword for members; a name is private only through its leading underscore.
  • D. Separate private blocks do not exist in Dart; members are hidden by the underscore prefix.

Tip: Use a leading underscore (_) to make a class member private in Dart.

Question 5Medium

What do Dart extension methods allow?

  1. Overriding methods of a final class from a different library
  2. Changing the runtime class of an object after it has been created
  3. Adding new fields that store extra state inside existing objects
  4. Adding methods to existing types like String without subclassing

Answer: D. Adding methods to existing types like String without subclassing

Explanation

extension StringX on String { ... } adds members that are resolved statically on the declared type. They cannot add instance state.

Step-by-step solution

  1. Extension methods let you add functions to existing types like String.
  2. You don't need to subclass or modify the original class to use them.
  3. They work by adding new methods to the type's declaration, not its instances.
  4. This lets you extend functionality without changing the original code.
  5. So, extension methods help add methods to existing types like String.
  6. Final answer: Adding methods to existing types like String without subclassing.

Why the other options are wrong

  • A. Extensions cannot override methods of a final class; they only add new methods on a type.
  • B. An object's runtime class never changes after creation; extensions do not alter it.
  • C. Extensions cannot add instance fields that store state; they only add methods and getters.

Tip: Use extension methods to add functions to existing types without changing their code.

Question 6Medium

You override == in a value class used as a key in a Set or Map. What else must you override?

  1. compareTo, so that the Set can sort its elements
  2. toString, so that equal objects print the same text
  3. hashCode, so that equal objects produce the same hash
  4. noSuchMethod, so that missing members can be handled

Answer: C. hashCode, so that equal objects produce the same hash

Explanation

Hash-based collections first compare hash codes. If equal objects have different hash codes, lookups and de-duplication break.

Step-by-step solution

  1. Sets and Maps check hashCode first, then ==.
  2. Equal objects must have equal hash codes.
  3. So override hashCode together with ==.

Why the other options are wrong

  • A. compareTo is for ordering, not hashing.
  • B. toString does not affect lookups.
  • D. noSuchMethod is unrelated.

Tip: Override == and hashCode together.

Question 7Medium

What is the key difference between class A extends B and class A implements B?

  1. implements copies B's code but not its fields or getters
  2. There is no difference other than the keyword chosen
  3. extends inherits no code; A must define every member of B
  4. implements inherits no code; A must define every member of B

Answer: D. implements inherits no code; A must define every member of B

Explanation

extends inherits B's implementation. implements only takes B's interface, so A must provide all members itself.

Step-by-step solution

  1. Extends is used for classes and inherits both fields and methods.
  2. Implements is used for interfaces and does not inherit any code.
  3. When a class implements an interface, it must define every member.
  4. Thus, implements inherits no code; A must define every member of B.

Why the other options are wrong

  • A. implements does not copy any code; it is the extends keyword that inherits fields and methods.
  • B. The keywords matter: extends inherits code, while implements inherits none and requires every member.
  • C. extends does inherit code; A gets B's fields and methods rather than writing every member itself.

Tip: Use extends for code reuse and implements to define a contract.

Question 8Medium

Given int get area => width * height; inside a class, how is it used?

  1. As r.area, with no parentheses, like a field
  2. As r.area(), like any other instance method
  3. As area(r), like a top-level function call
  4. As r.get('area'), using the reflection API

Answer: A. As r.area, with no parentheses, like a field

Explanation

A getter is accessed like a field but computes its value each time. Callers do not need to know whether area is stored or computed.

Step-by-step solution

  1. Get area => width * height; defines a property that calculates area on access.
  2. Properties are accessed like fields, so use r.area without parentheses.
  3. No method call is needed because it's not a function but a computed value.
  4. Callers treat it as a regular field, not a method or function.
  5. Final answer: As r.area, with no parentheses, like a field.

Why the other options are wrong

  • B. A getter is read without parentheses; adding them would try to call the int result, which fails.
  • C. area(r) is not a top-level function here; it is a getter that belongs to the class instance.
  • D. Dart has no reflection-based get('area') access here; the getter is read directly from the object.

Tip: Treat getters like fields in syntax, even though they compute values.

Question 9Hard

What does the on Base clause in mixin Logger on Base { ... } do?

  1. Restricts it to subclasses of Base and allows super calls to Base
  2. Runs the mixin's methods only when an object of type Base is passed
  3. Converts Logger into an abstract class that must extend Base itself
  4. Makes Base inherit from Logger automatically when Logger is declared

Answer: A. Restricts it to subclasses of Base and allows super calls to Base

Explanation

An on clause sets a superclass constraint. The mixin can then call super methods that Base declares, and it can only be applied to subtypes of Base.

Step-by-step solution

  1. on Base is a superclass constraint.
  2. The mixin can only be applied to classes that extend Base.
  3. Inside it, super calls to Base members are allowed.

Why the other options are wrong

  • B. on has nothing to do with passing objects.
  • C. The mixin does not become an abstract class.
  • D. It does not change Base's inheritance.

Tip: on = required superclass.

Question 10Hard

What is printed when B() is created?

class A {
  A() { print('A'); }
}

class B extends A {
  final int x;
  B() : x = f() { print('B'); }
  static int f() { print('init'); return 1; }
}

void main() { B(); }
  1. A, init, B
  2. A, B, init
  3. init, A, B
  4. init, B, A

Answer: C. init, A, B

Explanation

Dart runs the initializer list first, then the superclass constructor, then the class's own constructor body.

Step-by-step solution

  1. Creating B runs its initializer list first, so x = f() prints init.
  2. Then the superclass constructor A runs and prints A.
  3. Then B's constructor body runs and prints B, giving init, A, B (option C).

Why the other options are wrong

  • A. The initializer list runs before the superclass constructor, so init comes first.
  • B. B is printed last, after the initializer and the superclass constructor.
  • D. The order is reversed; the body runs last.

Tip: Dart order: initializer list, then superclass constructor, then the constructor body.

Practise all 15 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Flutter & Dart topics · All topics