TypeScript Mixins and Composition Quiz

TypeScript
0 Passed
0% acceptance

A 40-question TypeScript quiz covering mixin patterns, composition techniques, mixin constraints, function-based class factories, multiple mixin stacking, and practical patterns.

40 Questions
~80 minutes
1

Question 1

What is a mixin in TypeScript?

A
A class factory function that adds reusable behavior to a class
B
A type of inheritance keyword
C
A decorator used for metadata
D
A built-in language feature requiring a keyword
2

Question 2

What design principle do mixins support?

A
Composition over inheritance
B
Deep inheritance only
C
Static-only architecture
D
Runtime type erasure
3

Question 3

What does a typical mixin function return?

A
A new class extending the input class
B
A primitive type
C
Nothing
D
A constructor with no properties
4

Question 4

What does this mixin add?

typescript
type Ctor<T = {}> = new (...args: any[]) => T;
      
      function WithLogger<TBase extends Ctor>(Base: TBase) {
        return class extends Base {
          log(msg: string) { console.log(msg); }
        };
      }
A
A log() method
B
A static property
C
A private field
D
Nothing is added
5

Question 5

What pattern does this represent?

typescript
const Enhanced = Timestamped(WithLogger(Base));
A
Chained mixins
B
Multiple inheritance
C
Singleton wrapping
D
Static overloading
6

Question 6

Why is this signature common?

typescript
type Constructor<T = {}> = new (...args: any[]) => T;
A
It allows mixins to accept any class with a valid constructor
B
It restricts mixins to abstract classes only
C
It forces runtime checking
D
It prevents generic usage
7

Question 7

Composition encourages:

A
Combining behaviors instead of relying on inheritance depth
B
Always extending one superclass only
C
Flattening all functions into one class
D
Using global functions instead of classes
8

Question 8

What is a benefit of composition?

A
Avoiding brittle inheritance chains
B
Preventing all code reuse
C
Requiring static method design
D
Removing flexibility
9

Question 9

Does TypeScript have built-in syntax for mixins?

A
No, mixins are implemented with classes and functions
B
Yes, using a dedicated mixin keyword
C
Yes, using decorators
D
Yes, using interface unions
10

Question 10

Mixin constraints ensure:

A
The base class supports required members for mixin logic
B
Mixins can modify TypeScript syntax
C
Runtime-only behavior
D
Interface merging
11

Question 11

Which generic pattern applies constraints?

A
function M<TBase extends Constructor>()
B
function M(base)
C
function M<T>
D
function M(...args: any[])
12

Question 12

Constraints improve:

A
Type safety in mixin behaviors
B
Runtime performance
C
Inheritance depth
D
Static member generation
13

Question 13

What does this constraint enforce?

typescript
function WithID<TBase extends Constructor<{ id: number }>>(Base: TBase) {
        return class extends Base {};
      }
A
Base must have an id: number property
B
Base must be abstract
C
Base must not have any properties
D
Base must accept only string constructors
14

Question 14

This mixin adds which behavior?

typescript
function Movable<TBase extends Constructor>(Base: TBase) {
        return class extends Base {
          move(x: number) { return x * 2; }
        };
      }
A
A move() method
B
A read-only property
C
A protected field
D
A static helper
15

Question 15

Why is this mixin compatible?

typescript
class Point { constructor(public x: number) {} }
      const MovablePoint = Movable(Point);
A
Point matches the required constructor shape
B
Point is abstract
C
Movable only accepts empty classes
D
Constructors are ignored
16

Question 16

What is a potential issue with stacking many mixins?

A
Method name conflicts
B
Automatic override prevention
C
Loss of class identity
D
Loss of constructors
17

Question 17

Which approach helps avoid mixin conflicts?

A
Using unique method names or composing smaller behaviors
B
Forcing protected visibility
C
Using static-only classes
D
Using deep inheritance instead
18

Question 18

Multiple mixins create:

A
A linear chain of class extensions
B
True multiple inheritance
C
Only static-level augmentation
D
A new base type without behavior
19

Question 19

What does this stack produce?

typescript
const Final = Aware(Timestamped(WithLogger(Base)));
A
A class with logging, timestamps, and awareness features
B
A class with only timestamping
C
A class ignoring previous mixins
D
A class without behavior
20

Question 20

Why does this error?

typescript
function WithName<TBase extends Constructor>(Base: TBase) {
        return class extends Base { name = "x"; };
      }
      function NeededName<TBase extends Constructor<{ name: string }>>(Base: TBase) {
        return class extends Base {};
      }
      const C = NeededName(class {});
      
A
The class passed lacks a required name property
B
NeededName cannot accept classes
C
name must be a number
D
Base must be abstract
21

Question 21

What is added here?

typescript
function WithFlag<TBase extends Constructor>(Base: TBase) {
        return class extends Base { flag = true; };
      }
A
A flag property
B
A static method
C
A private field
D
A generic constraint
22

Question 22

Composition is often used to:

A
Assemble behavior from independent reusable units
B
Force classes to share a base class
C
Enforce strict inheritance
D
Remove method names
23

Question 23

Mixins enable:

A
Behavior sharing without deep inheritance
B
Strict interface-only architecture
C
Runtime multiple dispatch
D
Replacement of class syntax
24

Question 24

What is a risk when mixing numerous capabilities?

A
Overlapping method names or conflicting logic
B
Losing the constructor
C
Becoming unable to instantiate objects
D
Making types unassignable
25

Question 25

What happens here?

typescript
function Taggable<TBase extends Constructor>(Base: TBase) {
        return class extends Base { tags: string[] = []; };
      }
      class BaseClass {}
      const T = Taggable(BaseClass);
      const t = new T();
      t.tags.push("x");
A
tags is added correctly and usable
B
tags is undefined
C
Taggable removes BaseClass properties
D
tags must be readonly
26

Question 26

What does this mixin assume?

typescript
function HasPosition<TBase extends Constructor<{ x: number; y: number }>>(Base: TBase) {
        return class extends Base {
          distance() { return Math.sqrt(this.x ** 2 + this.y ** 2); }
        };
      }
A
Base must have x and y number fields
B
Base must have distance()
C
Base must be abstract
D
Base must use protected fields
27

Question 27

Why is this compatible?

typescript
class Pos { constructor(public x: number, public y: number) {} }
      const P = HasPosition(Pos);
A
Pos provides required x and y members
B
HasPosition requires no members
C
Pos is abstract
D
HasPosition enforces private visibility
28

Question 28

Mixins can:

A
Add both fields and methods
B
Modify TypeScript syntax
C
Access private fields in other classes
D
Introduce new keywords
29

Question 29

Which describes function-based mixins?

A
Higher-order functions returning extended classes
B
Functions that modify runtime syntax
C
Functions that delete constructors
D
Functions that return only interfaces
30

Question 30

How can composition handle state?

A
Through closure-based factories and delegated objects
B
Through inheritance only
C
Through static members exclusively
D
Without storing state at all
31

Question 31

What do mixins help avoid?

A
Rigid single-inheritance limitations
B
All use of classes
C
Encapsulation
D
Explicit constructors
32

Question 32

Which is a recommended mixin practice?

A
Keep mixins small and focused
B
Add every feature into one large mixin
C
Avoid constraints entirely
D
Use private fields in all mixins
33

Question 33

Which mixin structure is typical?

A
function M<TBase extends Constructor>(Base: TBase)
B
function M<T>(...args: never)
C
new M()
D
class M extends Base
34

Question 34

Behavior composition means:

A
Adding reusable behavior through mixins or object factories
B
Copying entire class trees
C
Enforcing inheritance depth
D
Removing methods from classes
35

Question 35

Which is a real scenario for mixins?

A
Adding logging or event features to multiple classes
B
Destroying constructors
C
Replacing all class hierarchies
D
Building deep static chains
36

Question 36

Which part of TypeScript supports composition strongly?

A
Functions, closures, and object literals
B
Global variables
C
Null constructors
D
Tuple-only structures
37

Question 37

Mixins typically operate:

A
At the class level
B
Only at the interface level
C
As runtime proxies
D
Only on static types
38

Question 38

Composition is often preferred because:

A
It avoids rigid inheritance structures
B
It prevents all method usage
C
It forces runtime polymorphism
D
It hides all class members
39

Question 39

What do mixins NOT provide?

A
True multiple inheritance
B
Behavior sharing
C
Reusable class extensions
D
Composable capabilities
40

Question 40

Overall, mixins encourage:

A
Building modular and reusable behavior pieces
B
Increasing inheritance depth
C
Restricting all class variation
D
Replacing all generics

QUIZZES IN TypeScript