0
0
Typescriptprogramming~20 mins

Readonly 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
What is the output of this TypeScript code with readonly property?
Consider the following TypeScript code. What will be logged to the console?
Typescript
class Person {
  readonly name: string;
  constructor(name: string) {
    this.name = name;
  }
}

const p = new Person('Alice');
console.log(p.name);
// p.name = 'Bob'; // Uncommenting this line causes an error
A"Alice"
B"Bob"
CError at runtime
Dundefined
Attempts:
2 left
💡 Hint
Readonly properties can be assigned only once, usually in the constructor.
Predict Output
intermediate
2:00remaining
What error does this code raise when modifying a readonly property?
What error will TypeScript report for the following code snippet?
Typescript
interface Config {
  readonly version: number;
}

const config: Config = { version: 1 };
config.version = 2;
ANo error, assignment succeeds
BCannot assign to 'version' because it is a read-only property.
CSyntaxError: Unexpected token '='
DTypeError: Cannot assign to read only property 'version' of Object
Attempts:
2 left
💡 Hint
Readonly properties cannot be assigned after initialization.
🔧 Debug
advanced
2:00remaining
Why does this code fail to compile when modifying a readonly property?
Identify the reason why the following TypeScript code does not compile.
Typescript
class Car {
  readonly model: string = 'Sedan';
  constructor() {
    this.model = 'Coupe';
  }
}

const car = new Car();
car.model = 'Convertible';
AClass properties cannot be assigned in constructor.
BCannot assign to 'model' inside constructor because it is readonly.
CReadonly properties cannot have default values.
DCannot assign to 'model' outside of constructor because it is readonly.
Attempts:
2 left
💡 Hint
Readonly properties can be assigned in the constructor or at declaration, but not after.
📝 Syntax
advanced
2:00remaining
Which option correctly declares a readonly property in a TypeScript interface?
Select the correct syntax to declare a readonly property 'id' of type number in an interface.
Ainterface User { readonly: number id; }
Binterface User { id: readonly number; }
Cinterface User { readonly id: number; }
Dinterface User { id: number readonly; }
Attempts:
2 left
💡 Hint
The 'readonly' keyword comes before the property name.
🚀 Application
expert
2:00remaining
How many properties can be modified after object creation?
Given the following TypeScript code, how many properties of the object 'settings' can be changed after creation?
Typescript
type Settings = {
  readonly theme: string;
  volume: number;
  readonly language: string;
  notifications: boolean;
};

const settings: Settings = {
  theme: 'dark',
  volume: 10,
  language: 'en',
  notifications: true
};

settings.volume = 20;
settings.notifications = false;
// settings.theme = 'light'; // Error
// settings.language = 'fr'; // Error
A2
B4
C0
D3
Attempts:
2 left
💡 Hint
Readonly properties cannot be changed after initialization.