Classes and OOP in TypeScript — Concept Check
Questions on Classes and OOP in TypeScript. Everything Java gives you is here — access modifiers, abstract classes, interfaces — but the surrounding culture uses far fewer classes, and knowing when not to reach for one matters.
Questions in this quiz
8 questions on Classes and OOP in TypeScript — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.In TypeScript, you have a class with a private field and a field with a # prefix. What happens when you try to serialize an instance of this class using JSON.stringify?
- ABoth fields are included in the JSON output.
- BOnly the # field is included in the JSON output.
- COnly the private field is included in the JSON output.
- DNeither field is included in the JSON output.
2.You are designing a TypeScript module to handle user authentication. Which approach is more idiomatic if you only need stateless functions?
- AUse exported functions in a module.
- BUse an abstract class with implemented methods.
- CUse a class with static methods.
- DUse a class to encapsulate the methods.
3.A TypeScript class uses a # field for storing sensitive data. What is a consequence of this choice?
- AThe field can be accessed using bracket notation.
- BThe field is included in JSON serialization.
- CThe field is accessible in unit tests.
- DThe field is inaccessible at runtime, ensuring true privacy.
4.You have a TypeScript class with a private field. During testing, you notice you can access this field. Why is this happening?
- APrivate fields are accessible in test environments.
- BPrivate fields are accessible via reflection.
- CThe TypeScript compiler is misconfigured.
- DTypeScript private fields are only compile-time checks.
5.In a TypeScript project, you need to model a domain entity with behavior and state. What should you use?
- AA module of exported functions.
- BA class with only static methods.
- CAn interface with default implementations.
- DA class with instance methods and state.
6.A TypeScript developer uses a class to group three stateless utility functions. What is a more appropriate alternative?
- AKeep the class for better organization.
- BUse a class with static methods.
- CUse a module with exported functions.
- DUse an abstract class instead.
7.You are implementing a service in TypeScript that requires dependency injection. Which approach is more idiomatic if the service has state?
- AUse static methods in a class.
- BUse a class to encapsulate state and dependencies.
- CUse an interface to define the service.
- DUse a module with exported functions.
8.In TypeScript, which of the following is a runtime guarantee for field privacy?
- AUsing the # prefix for fields.
- BUsing the protected keyword.
- CUsing the private keyword.
- DUsing readonly properties.