0
0
Typescriptprogramming~20 mins

Class property declarations in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Class Property Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A"Alice"
B"Bob"
Cundefined
DTypeError
Attempts:
2 left
💡 Hint
Class property initializers run before the constructor body.
Predict Output
intermediate
2: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);
A"Tesla"
B"BMW"
CCompile-time error
DRuntime error
Attempts:
2 left
💡 Hint
Readonly properties cannot be assigned after initialization.
🔧 Debug
advanced
2: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);
A"count" is not initialized before use, causing undefined increment error
B"count" should be declared as readonly
CClass must have a constructor to declare properties
D"increment" method is missing a return type
Attempts:
2 left
💡 Hint
Check if the property has a starting value before incrementing.
📝 Syntax
advanced
2: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
}
Aemail string = "user@example.com";
Bage: number;
CisActive = true;
Dname: string = "John";
Attempts:
2 left
💡 Hint
Check the order of type and variable name.
🚀 Application
expert
2: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");
A1
B3
C0
D2
Attempts:
2 left
💡 Hint
Static properties are not part of instance properties.