TypeScript Classes and Objects Quiz

TypeScript
0 Passed
0% acceptance

A 40-question TypeScript quiz focusing on classes, constructors, access modifiers, readonly fields, methods, inheritance, abstract classes, and object-based typing.

40 Questions
~80 minutes
1

Question 1

What keyword defines a class in TypeScript?

A
class
B
struct
C
object
D
entity
2

Question 2

What does new Person() create here?

typescript
class Person {}
const p = new Person();
A
An instance of Person
B
A function
C
A namespace
D
A type alias
3

Question 3

Which item can be inside a class?

A
Fields and methods
B
Interfaces only
C
Modules
D
Enums only
4

Question 4

What logs?

typescript
class Box { size = 10 }
console.log(new Box().size);
A
10
B
undefined
C
Error
D
null
5

Question 5

What is the purpose of a constructor?

A
Initialising instance properties
B
Destroying objects
C
Exporting modules
D
Freezing types
6

Question 6

What prints?

typescript
class A {
  constructor() { console.log('init') }
}
new A();
A
'init'
B
undefined
C
Error
D
Nothing
7

Question 7

What must a subclass call if it declares its own constructor?

A
super()
B
parent()
C
init()
D
prototype()
8

Question 8

What error occurs here?

typescript
class Base {}
class Sub extends Base {
  constructor() {}
}
A
Constructor must call super before accessing this
B
Base cannot be extended
C
Constructor overload missing
D
Type error in Base
9

Question 9

Which modifier makes a class member only accessible inside the class itself?

A
private
B
public
C
protected
D
static
10

Question 10

Which modifier allows access inside the class and subclasses?

A
protected
B
public
C
private
D
static
11

Question 11

What happens?

typescript
class Car {
  private speed = 10;
}
const c = new Car();
console.log(c.speed);
A
Property 'speed' is private
B
10
C
undefined
D
null
12

Question 12

What does readonly do for class fields?

A
Prevents further reassignment
B
Forces deep immutability
C
Hides the value
D
Creates a static member
13

Question 13

What error occurs?

typescript
class Item {
  readonly id = 1;
}
const i = new Item();
i.id = 5;
A
Cannot assign to read-only property
B
id becomes 5
C
id becomes undefined
D
No error
14

Question 14

What are parameter properties?

A
Constructor parameters that become fields automatically
B
Static parameters
C
Parameters with default values
D
Optional method arguments
15

Question 15

What is stored on the instance?

typescript
class User {
  constructor(public name: string) {}
}
const u = new User('A');
A
A public field 'name'
B
No fields
C
A private field only
D
A static field
16

Question 16

What does a method represent in a class?

A
A function belonging to an instance
B
A static value
C
A readonly tuple
D
A type guard
17

Question 17

What prints?

typescript
class Counter {
  count = 1;
  inc() { this.count++; }
}
const c = new Counter();
c.inc();
console.log(c.count);
A
2
B
1
C
undefined
D
Error
18

Question 18

Which statement about this binding in methods is correct?

A
Methods use dynamic this based on call site
B
this always refers to the class definition
C
this binds to globalThis automatically
D
this refers to the type, not the instance
19

Question 19

What prints?

typescript
class T {
  x = 5;
  getX = () => this.x;
}
console.log(new T().getX());
A
5
B
undefined
C
Error
D
null
20

Question 20

What keyword creates a subclass?

A
extends
B
inherits
C
superclass
D
chain
21

Question 21

What does super refer to?

A
The parent class
B
The root namespace
C
The global object
D
The constructor type
22

Question 22

What prints?

typescript
class A { say() { return 'A' } }
class B extends A {
  say() { return 'B' }
}
console.log(new B().say());
A
'B'
B
'A'
C
undefined
D
Error
23

Question 23

Abstract classes can:

A
Contain abstract and concrete methods
B
Be instantiated directly
C
Only contain abstract methods
D
Have no constructor
24

Question 24

What error occurs?

typescript
abstract class Shape {}
const s = new Shape();
A
Cannot create an instance of an abstract class
B
Missing constructor
C
Shape is private
D
No error
25

Question 25

What must Circle implement?

typescript
abstract class Shape {
  abstract area(): number;
}
class Circle extends Shape {}
A
The area() method
B
A constructor
C
No methods
D
A static property
26

Question 26

What does an instance type represent?

A
The shape of objects created by the class
B
The constructor type
C
The static side
D
The module interface
27

Question 27

Which object matches structural typing?

A
Any object with compatible fields
B
Only instances created with new
C
Only objects from classes
D
Only prototypes
28

Question 28

Static fields belong to:

A
The class itself
B
Each instance
C
Only subclasses
D
The global scope
29

Question 29

Which field type is shared across all instances?

A
Static fields
B
Public fields
C
Protected fields
D
Readonly instance fields
30

Question 30

Why might you use classes instead of functions for object creation?

A
To group state and behaviour together
B
To remove all prototypes
C
To eliminate inheritance
D
To create runtime types
31

Question 31

Which statement describes instance fields?

A
They belong to each object separately
B
They are shared as prototypes
C
They only exist at type level
D
They must always be static
32

Question 32

Classes compile to what underlying mechanism?

A
JavaScript prototype-based inheritance
B
C++ style memory layout
C
Java-style bytecode
D
Native machine classes
33

Question 33

What enforces compile-time typing for classes?

A
TypeScript’s static type checker
B
Runtime type inspectors
C
Prototype chains only
D
Reflection APIs
34

Question 34

Which statement is true about property initialisers?

A
They run before the constructor body
B
They prohibit constructors
C
They run after constructor returns
D
They freeze all state
35

Question 35

Which describes method overriding?

A
A subclass redefines a parent method
B
Replacing constructor logic
C
Preventing inheritance
D
Removing all parent fields
36

Question 36

What is required to override a method safely?

A
Matching parameter and return types
B
Changing the method name
C
Removing the method body
D
A static return signature only
37

Question 37

How do classes support polymorphism?

A
Through inheritance and method overriding
B
Through static initialisers only
C
Through tuple merging
D
Through primitive aliasing
38

Question 38

Which pattern uses a class to enforce a required method structure?

A
Abstract classes
B
Enums
C
Tuples
D
Modules
39

Question 39

What describes structural compatibility between classes?

A
Objects match if shapes match
B
Classes must share names
C
Instances only match with same constructors
D
Protected fields prevent all compatibility
40

Question 40

Why are classes useful in TypeScript projects?

A
They provide structured object modelling with type safety
B
They remove the need for functions
C
They create runtime-only constraints
D
They replace modules entirely

QUIZZES IN TypeScript