Challenge - 5 Problems
Class Property Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of class property initialization
What is the output of this TypeScript code when creating an instance of
Person and logging person.name?Typescript
class Person { name: string = "Alice"; constructor() { this.name = "Bob"; } } const person = new Person(); console.log(person.name);
Attempts:
2 left
💡 Hint
Class property initializers run before the constructor body.
✗ Incorrect
The property
name is first initialized to "Alice". Then the constructor runs and sets name to "Bob". So the final value is "Bob".❓ Predict Output
intermediate2:00remaining
Readonly class property behavior
What happens when this TypeScript code runs?
Typescript
class Car { readonly model: string = "Tesla"; constructor() { this.model = "BMW"; } } const car = new Car(); console.log(car.model);
Attempts:
2 left
💡 Hint
Readonly properties cannot be assigned after initialization.
✗ Incorrect
The assignment to
this.model inside the constructor is not allowed because model is readonly. TypeScript will report a compile-time error.🔧 Debug
advanced2:00remaining
Why does this code throw an error?
This TypeScript class has a property declared but the code throws an error when creating an instance. What is the cause?
Typescript
class Counter { count: number; increment() { this.count++; } } const c = new Counter(); c.increment(); console.log(c.count);
Attempts:
2 left
💡 Hint
Check if the property has a starting value before incrementing.
✗ Incorrect
The property
count is declared but not initialized, so it is undefined. Incrementing undefined causes a runtime error.📝 Syntax
advanced2:00remaining
Identify the syntax error in class property declaration
Which option contains a syntax error in declaring a class property in TypeScript?
Typescript
class User { // property declarations here }
Attempts:
2 left
💡 Hint
Check the order of type and variable name.
✗ Incorrect
Option A misses the colon between the property name and its type. It should be
email: string = "user@example.com";🚀 Application
expert2:00remaining
How many properties does this class instance have?
Given this TypeScript class, how many own properties will an instance have immediately after creation?
Typescript
class Book { title: string = ""; author: string; static category: string = "Fiction"; constructor(author: string) { this.author = author; } } const novel = new Book("Jane Austen");
Attempts:
2 left
💡 Hint
Static properties are not part of instance properties.
✗ Incorrect
The instance
novel has two own properties: title (initialized) and author (set in constructor). The static property category belongs to the class, not the instance.