The constructor sets name to 'Book' and price to 15.
Step 2: Check display method output
The display method returns a string combining name and price exactly as "Book costs $15".
Final Answer:
Book costs $15 -> Option A
Quick Check:
Display method returns formatted string = C [OK]
Hint: Check constructor values and string template output [OK]
Common Mistakes:
Expecting price to show decimals automatically
Confusing undefined property values
Thinking type mismatch causes runtime error here
4. Identify the error in this TypeScript class and fix it:
class Person {
age: number;
constructor(age: string) {
this.age = age;
}
}
medium
A. Change constructor parameter type to number
B. Change property type to string
C. Remove type annotations completely
D. No error, code is correct
Solution
Step 1: Compare property and constructor types
The property age is a number, but constructor expects a string parameter.
Step 2: Fix type mismatch
Change constructor parameter type to number to match property type.
Final Answer:
Change constructor parameter type to number -> Option A
Quick Check:
Property and parameter types must match = A [OK]
Hint: Match constructor parameter types with property types [OK]
Common Mistakes:
Ignoring type mismatch between property and parameter
Changing property type unnecessarily
Assuming no error when types differ
5. You want to create a typed class in TypeScript that holds a list of users with their names and ages. Which approach best ensures type safety and easy maintenance?
hard
A. Use a class with properties typed as strings only.
B. Use a class with any type for properties to allow flexibility.
C. Use plain JavaScript objects without types for faster coding.
D. Use a class with typed properties and an array of user objects typed with an interface.
Solution
Step 1: Consider type safety and maintenance
Typed properties and interfaces help catch errors and make code clear for teamwork.
Step 2: Evaluate options
A class with typed properties and an array of user objects typed with an interface ensures safety and clarity. Other options sacrifice safety or clarity.
Final Answer:
Use a class with typed properties and an array of user objects typed with an interface. -> Option D
Quick Check:
Typed classes + interfaces = best practice [OK]
Hint: Combine typed classes with interfaces for clear, safe code [OK]
Common Mistakes:
Using any type loses type safety
Ignoring interfaces reduces code clarity
Typing all properties as strings limits data accuracy
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.