0
0
Typescriptprogramming~20 mins

Super keyword behavior in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Super Keyword Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of super method call in subclass
What is the output of this TypeScript code when the greet method is called on an instance of Child?
Typescript
class Parent {
  greet() {
    return "Hello from Parent";
  }
}

class Child extends Parent {
  greet() {
    return super.greet() + " and Child";
  }
}

const c = new Child();
console.log(c.greet());
A"Hello from Parent and Child"
B"Hello from Child"
C"Hello from Parent"
DTypeError: super.greet is not a function
Attempts:
2 left
💡 Hint
Remember that super calls the method from the parent class.
Predict Output
intermediate
2:00remaining
Using super in constructor
What will be logged when creating an instance of Child?
Typescript
class Parent {
  name: string;
  constructor(name: string) {
    this.name = name;
    console.log('Parent constructor: ' + this.name);
  }
}

class Child extends Parent {
  constructor(name: string) {
    super(name);
    console.log('Child constructor: ' + this.name);
  }
}

const c = new Child('Alice');
AParent constructor: Alice\nChild constructor: Alice
BChild constructor: Alice\nParent constructor: Alice
CParent constructor: undefined\nChild constructor: Alice
DReferenceError: Must call super constructor before accessing 'this'
Attempts:
2 left
💡 Hint
The parent constructor runs first and sets the name.
Predict Output
advanced
2:00remaining
super in arrow function method
What is the output of this code when child.method() is called?
Typescript
class Parent {
  method() {
    return 'Parent method';
  }
}

class Child extends Parent {
  method = () => {
    return super.method();
  };
}

const child = new Child();
console.log(child.method());
A"Parent method"
B"Child method"
CTypeError: super.method is not a function
DSyntaxError: 'super' keyword unexpected here
Attempts:
2 left
💡 Hint
Arrow functions do not have their own super binding.
Predict Output
advanced
2:00remaining
super in getter and setter
What will be the output of this code snippet?
Typescript
class Parent {
  private _value = 10;
  get value() {
    return this._value;
  }
  set value(val: number) {
    this._value = val;
  }
}

class Child extends Parent {
  get value() {
    return super.value * 2;
  }
  set value(val: number) {
    super.value = val / 2;
  }
}

const c = new Child();
c.value = 40;
console.log(c.value);
A80
B40
C20
DTypeError: Cannot set property 'value' of undefined
Attempts:
2 left
💡 Hint
Setter divides by 2, getter multiplies by 2.
🧠 Conceptual
expert
3:00remaining
Behavior of super in multiple inheritance via mixins
Consider the following TypeScript code using mixins. What will be the output when obj.method() is called?
Typescript
class Base {
  method() {
    return 'Base';
  }
}

function Mixin1<T extends new (...args: any[]) => {}>(BaseClass: T) {
  return class extends BaseClass {
    method() {
      return 'Mixin1 -> ' + super.method();
    }
  };
}

function Mixin2<T extends new (...args: any[]) => {}>(BaseClass: T) {
  return class extends BaseClass {
    method() {
      return 'Mixin2 -> ' + super.method();
    }
  };
}

class Combined extends Mixin1(Mixin2(Base)) {}
const obj = new Combined();
console.log(obj.method());
A"Base"
B"Mixin2 -> Mixin1 -> Base"
C"Mixin1 -> Mixin2 -> Base"
DTypeError: super.method is not a function
Attempts:
2 left
💡 Hint
Mixins wrap classes inside each other, so the outermost calls super of the inner.