0
0
Typescriptprogramming~20 mins

Readonly class properties in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Readonly Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of readonly property assignment
What will be the output of this TypeScript code when compiled and run with strict mode?
Typescript
class Car {
  readonly brand: string;
  constructor(brand: string) {
    this.brand = brand;
  }
  changeBrand(newBrand: string) {
    this.brand = newBrand;
  }
}
const myCar = new Car('Toyota');
console.log(myCar.brand);
myCar.changeBrand('Honda');
console.log(myCar.brand);
ARuntime error when trying to assign to readonly property
BToyota\nHonda
CToyota\nToyota
DCompilation error due to assignment to readonly property
Attempts:
2 left
💡 Hint
Readonly properties cannot be assigned outside the constructor.
🧠 Conceptual
intermediate
2:00remaining
Readonly property behavior in subclasses
Consider this TypeScript code. What will be the output when running it?
Typescript
class Animal {
  readonly species: string = 'Unknown';
  constructor(species?: string) {
    if (species) this.species = species;
  }
}
class Dog extends Animal {
  constructor() {
    super('Canine');
    this.species = 'Dog';
  }
}
const pet = new Dog();
console.log(pet.species);
ACompilation error due to assignment to readonly property in subclass
BDog
CRuntime error due to assignment to readonly property
DCanine
Attempts:
2 left
💡 Hint
Readonly properties can only be assigned once, typically in the constructor of the declaring class.
🔧 Debug
advanced
2:00remaining
Why does this readonly property assignment fail?
This TypeScript code fails to compile. Identify the cause of the error.
Typescript
class Book {
  readonly title: string;
  constructor(title: string) {
    this.title = title;
  }
  rename(newTitle: string) {
    this.title = newTitle;
  }
}
const novel = new Book('1984');
novel.rename('Animal Farm');
A'title' is not initialized in the constructor
BCannot assign to 'title' because it is a readonly property
CMethod rename is not allowed in class with readonly properties
DNo error, code runs and outputs 'Animal Farm'
Attempts:
2 left
💡 Hint
Readonly properties cannot be changed after initial assignment.
📝 Syntax
advanced
2:00remaining
Identify the syntax error with readonly property
Which option contains a syntax error related to readonly class properties?
Aclass User { readonly name: string; constructor() { this.name = 'Alice'; } changeName() { this.name = 'Bob'; } }
Bclass User { readonly name: string; constructor() { this.name = 'Alice'; } }
Cclass User { readonly name: string = 'Alice'; constructor() { this.name = 'Bob'; } }
Dclass User { readonly name = 'Alice'; constructor() {} }
Attempts:
2 left
💡 Hint
Check where readonly properties are assigned.
🚀 Application
expert
3:00remaining
Readonly properties with parameter properties
What will be the output of this TypeScript code?
Typescript
class Point {
  constructor(readonly x: number, readonly y: number) {}
  move(newX: number, newY: number) {
    this.x = newX;
    this.y = newY;
  }
}
const p = new Point(5, 10);
console.log(`${p.x},${p.y}`);
p.move(20, 30);
console.log(`${p.x},${p.y}`);
A5,10\n5,10
B5,10\n20,30
CCompilation error due to assignment to readonly properties
DRuntime error when trying to assign to readonly properties
Attempts:
2 left
💡 Hint
Readonly parameter properties cannot be reassigned after construction.