MCQs on Classes and Object-Oriented Features | TypeScript

Deep dive into TypeScript’s object-oriented features with 30 MCQs focused on Classes, Constructors, Access Modifiers, Readonly Properties, Getters/Setters, and Static Members. Boost your TypeScript OOP skills!


Chapter 7: Classes and Object-Oriented Features

Classes and Constructors

  1. In TypeScript, how do you declare a class?
    • a) class ClassName { }
    • b) function ClassName { }
    • c) object ClassName { }
    • d) class: ClassName { }
  2. How do you define a constructor method in TypeScript?
    • a) constructor() { }
    • b) class constructor() { }
    • c) init() { }
    • d) constructorFunction() { }
  3. What keyword is used to create an instance of a class in TypeScript?
    • a) create
    • b) new
    • c) this
    • d) instance
  4. How do you call the constructor of a class in TypeScript?
    • a) By using the new keyword
    • b) By directly calling the constructor name
    • c) By using the call() function
    • d) By defining the constructor function again
  5. In TypeScript, how would you define a class with a constructor that accepts two arguments?
    • a) class Person { constructor(name, age) { } }
    • b) class Person { constructor(name: string, age: number) { } }
    • c) class Person(name: string, age: number) { }
    • d) class Person { name: string, age: number; constructor() { } }

Public, Private, and Protected Access Modifiers

  1. Which access modifier makes a class member accessible everywhere, both inside and outside the class?
    • a) private
    • b) public
    • c) protected
    • d) readonly
  2. What does the private access modifier do in TypeScript?
    • a) Makes the member accessible only within the class
    • b) Makes the member accessible inside and outside the class
    • c) Makes the member accessible in child classes only
    • d) Makes the member read-only
  3. Which modifier allows access to a class member only within the class and its subclasses?
    • a) private
    • b) public
    • c) protected
    • d) readonly
  4. Can a private member of a class be accessed outside the class in TypeScript?
    • a) Yes, through a getter function
    • b) Yes, using reflection
    • c) No, it is completely inaccessible
    • d) Yes, if the class is extended
  5. Which of the following will result in a compilation error?
    • a) class Person { public name: string; }
    • b) class Person { private name: string; }
    • c) class Person { protected name: string; }
    • d) class Person { name: string; }

Readonly Properties

  1. What does the readonly modifier do in TypeScript?
    • a) Makes the property immutable after initialization
    • b) Allows the property to be modified only within the class
    • c) Prevents the class from being inherited
    • d) Makes the property accessible only within the constructor
  2. How do you declare a readonly property in TypeScript?
    • a) readonly name: string;
    • b) const name: string;
    • c) static readonly name: string;
    • d) let readonly name: string;
  3. Can a readonly property be modified after initialization?
    • a) Yes, if declared as static readonly
    • b) Yes, but only inside the constructor
    • c) No, it cannot be modified
    • d) Yes, if accessed via a setter
  4. Can a readonly property be assigned to a new value inside a method?
    • a) Yes, using a setter method
    • b) Yes, but only in the constructor
    • c) No, it cannot be reassigned
    • d) Yes, using direct property assignment
  5. Which of the following will result in a compilation error?
    • a) readonly age: number;
    • b) readonly name: string = "John";
    • c) readonly name: string; name = "John";
    • d) readonly name: string = "Alice";

Getters and Setters

  1. What is the purpose of a getter method in TypeScript?
    • a) To modify the value of a property
    • b) To return the value of a property
    • c) To validate the value of a property
    • d) To restrict access to a property
  2. What keyword is used to define a getter method in TypeScript?
    • a) get
    • b) setter
    • c) access
    • d) return
  3. Which of the following is the correct syntax for defining a getter method in a class?
    • a) get age() { return this._age; }
    • b) getter age() { return this._age; }
    • c) get age: number { return this._age; }
    • d) get return age { return this._age; }
  4. What is the purpose of a setter method in TypeScript?
    • a) To prevent access to a property
    • b) To validate or modify the value of a property
    • c) To define the initial value of a property
    • d) To create a constant property
  5. How do you define a setter method in TypeScript?
    • a) set property(value: string) { this._property = value; }
    • b) setter property(value: string) { this._property = value; }
    • c) set property = (value: string) { this._property = value; }
    • d) set property(value: string): void { this._property = value; }
  6. Can a class have both getter and setter methods for the same property?
    • a) Yes, but only for private properties
    • b) No, only one can exist
    • c) Yes, you can have both
    • d) Only for readonly properties
  7. Which of the following is true about getters and setters in TypeScript?
    • a) They can be accessed as regular properties
    • b) They must be manually invoked
    • c) Getters and setters must return void
    • d) Setters can only return a string

Static Members

  1. What does a static member in a class mean in TypeScript?
    • a) It can only be accessed by instances of the class
    • b) It is shared among all instances of the class
    • c) It can only be accessed from outside the class
    • d) It can only be accessed in the constructor
  2. How do you declare a static property in TypeScript?
    • a) static property: string;
    • b) static string property;
    • c) property static string;
    • d) string static property;
  3. Can static methods access instance properties in TypeScript?
    • a) Yes, through the this keyword
    • b) No, they cannot access instance properties
    • c) Yes, but only with inheritance
    • d) Yes, if declared inside the constructor
  4. How do you call a static method in TypeScript?
    • a) instance.method();
    • b) ClassName.method();
    • c) this.method();
    • d) Class.method();
  5. Which of the following is true for static methods?
    • a) They are bound to instances of the class
    • b) They can be called without creating an instance of the class
    • c) They cannot be inherited
    • d) They are not part of the class
  6. Can static properties be accessed using this in a non-static method?
    • a) Yes, without issues
    • b) No, it results in a compile-time error
    • c) Yes, but only inside the constructor
    • d) Yes, using the super keyword
  7. What is the advantage of using static members in a class?
    • a) They reduce memory usage by not duplicating data in instances
    • b) They help create private properties
    • c) They ensure that every instance has a unique copy of the property
    • d) They improve the performance of instance methods
  8. What happens if you try to access a non-static method with ClassName.method();?
    • a) The method will execute normally
    • b) TypeScript will throw an error
    • c) The method will be automatically converted to static
    • d) It will return undefined

Answer Key

QnoAnswer
1a) class ClassName { }
2a) constructor() { }
3b) new
4a) By using the new keyword
5b) class Person { constructor(name: string, age: number) { } }
6b) public
7a) Makes the member accessible only within the class
8c) protected
9c) No, it is completely inaccessible
10c) class Person { protected name: string; }
11a) Makes the property immutable after initialization
12a) readonly name: string;
13c) No, it cannot be modified
14c) No, it cannot be reassigned
15c) readonly name: string; name = “John”;
16b) To return the value of a property
17a) get
18a) get age() { return this._age; }
19b) To validate or modify the value of a property
20a) set property(value: string) { this._property = value; }
21c) Yes, you can have both
22a) They can be accessed as regular properties
23b) It is shared among all instances of the class
24a) static property: string;
25b) No, they cannot access instance properties
26b) ClassName.method();
27b) They can be called without creating an instance of the class
28b) No, it results in a compile-time error
29a) They reduce memory usage by not duplicating data in instances
30b) TypeScript will throw an error

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top