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
In TypeScript, how do you declare a class?
a) class ClassName { }
b) function ClassName { }
c) object ClassName { }
d) class: ClassName { }
How do you define a constructor method in TypeScript?
a) constructor() { }
b) class constructor() { }
c) init() { }
d) constructorFunction() { }
What keyword is used to create an instance of a class in TypeScript?
a) create
b) new
c) this
d) instance
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
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
Which access modifier makes a class member accessible everywhere, both inside and outside the class?
a) private
b) public
c) protected
d) readonly
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
Which modifier allows access to a class member only within the class and its subclasses?
a) private
b) public
c) protected
d) readonly
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
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
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
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;
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
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
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
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
What keyword is used to define a getter method in TypeScript?
a) get
b) setter
c) access
d) return
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; }
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
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; }
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
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
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
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;
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
How do you call a static method in TypeScript?
a) instance.method();
b) ClassName.method();
c) this.method();
d) Class.method();
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
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
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
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
Qno
Answer
1
a) class ClassName { }
2
a) constructor() { }
3
b) new
4
a) By using the new keyword
5
b) class Person { constructor(name: string, age: number) { } }
6
b) public
7
a) Makes the member accessible only within the class
8
c) protected
9
c) No, it is completely inaccessible
10
c) class Person { protected name: string; }
11
a) Makes the property immutable after initialization
12
a) readonly name: string;
13
c) No, it cannot be modified
14
c) No, it cannot be reassigned
15
c) readonly name: string; name = “John”;
16
b) To return the value of a property
17
a) get
18
a) get age() { return this._age; }
19
b) To validate or modify the value of a property
20
a) set property(value: string) { this._property = value; }
21
c) Yes, you can have both
22
a) They can be accessed as regular properties
23
b) It is shared among all instances of the class
24
a) static property: string;
25
b) No, they cannot access instance properties
26
b) ClassName.method();
27
b) They can be called without creating an instance of the class
28
b) No, it results in a compile-time error
29
a) They reduce memory usage by not duplicating data in instances