0
0
Typescriptprogramming~20 mins

Parameter properties shorthand in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Parameter Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of class with parameter properties shorthand
What is the output of this TypeScript code when compiled to JavaScript and run?
Typescript
class Person {
  constructor(public name: string, private age: number) {}
  greet() {
    return `Hello, my name is ${this.name}`;
  }
}
const p = new Person('Alice', 30);
console.log(p.greet());
AHello, my name is undefined
BHello, my name is Alice
CTypeError: p.greet is not a function
DCompilation error due to private modifier
Attempts:
2 left
💡 Hint
Remember that parameter properties automatically create and initialize class members.
🧠 Conceptual
intermediate
1:30remaining
Understanding parameter properties shorthand effect on class members
Which statement about parameter properties shorthand in TypeScript is correct?
AIt can only be used with public access modifier.
BIt only declares class members but does not initialize them.
CIt declares and initializes class members from constructor parameters automatically.
DIt requires explicit property declarations outside the constructor.
Attempts:
2 left
💡 Hint
Think about what happens when you add 'public' or 'private' before a constructor parameter.
🔧 Debug
advanced
2:00remaining
Identify the error in parameter properties usage
What error will this TypeScript code produce?
Typescript
class Car {
  constructor(readonly model: string, speed: number) {}
  getModel() {
    return this.model;
  }
}
const c = new Car('Tesla', 120);
console.log(c.getModel());
ANo error, outputs 'Tesla'
BProperty 'speed' is not initialized and causes an error
CCannot use 'readonly' in constructor parameters
DMethod getModel is not defined
Attempts:
2 left
💡 Hint
Check which constructor parameters become class properties and which do not.
📝 Syntax
advanced
1:30remaining
Which constructor uses parameter properties shorthand correctly?
Select the constructor that correctly uses parameter properties shorthand to declare and initialize class members.
Aconstructor(name: string, private age: number) { this.name = name; }
Bconstructor(name: string, age: number) { this.name = name; this.age = age; }
Cconstructor(private name: string, age: number) { this.age = age; }
Dconstructor(public name: string, private age: number) {}
Attempts:
2 left
💡 Hint
Parameter properties shorthand requires access modifiers on parameters and no manual assignments.
🚀 Application
expert
2:00remaining
Count class members created by parameter properties shorthand
How many class members are created by this constructor using parameter properties shorthand?
Typescript
class Book {
  constructor(public title: string, private author: string, year: number, readonly pages: number) {}
}
A3
B4
C2
D1
Attempts:
2 left
💡 Hint
Only parameters with access modifiers become class members.