TypeScript Abstract Classes and Access Modifiers Quiz

TypeScript
0 Passed
0% acceptance

A 40-question TypeScript quiz covering abstract classes, abstract methods, public/private/protected access, readonly properties, inheritance behavior, and visibility constraints.

40 Questions
~80 minutes
1

Question 1

What is an abstract class?

A
A class that cannot be instantiated directly
B
A class that must have no methods
C
A class that automatically freezes instances
D
A class used only for static fields
2

Question 2

What is true about abstract methods?

A
They must be implemented in subclasses
B
They can contain method bodies
C
They can only appear in interfaces
D
They are automatically optional
3

Question 3

Abstract classes can contain:

A
Both abstract and non-abstract methods
B
Only abstract methods
C
Only static methods
D
Only private fields
4

Question 4

Which access modifier makes a member visible everywhere?

A
public
B
protected
C
private
D
internal
5

Question 5

A private member is accessible to:

A
Only its own class
B
Its class and subclasses
C
Any class in the same file
D
Any caller using object reference
6

Question 6

A protected member is accessible by:

A
The class itself and its subclasses
B
All external callers
C
Only functions inside the same namespace
D
Only static methods
7

Question 7

What is wrong with this code?

typescript
abstract class Shape {}
const s = new Shape();
A
Abstract classes cannot be instantiated
B
Abstract classes cannot be declared
C
Shape must extend another class
D
Abstract classes cannot contain fields
8

Question 8

What must a subclass do here?

typescript
abstract class Base {
  abstract move(): void;
}
A
Implement move()
B
Declare Base again
C
Make move() public
D
Remove abstract keyword
9

Question 9

What is the visibility of x?

typescript
class A {
  protected x = 10;
}
A
Visible only to A and its subclasses
B
Visible everywhere
C
Visible to the same file only
D
Visible only through static methods
10

Question 10

readonly fields:

A
Can only be assigned during declaration or in the constructor
B
Cannot be read at runtime
C
Cannot exist inside classes
D
Must always be public
11

Question 11

readonly works with which modifiers?

A
public, protected, and private
B
Only public
C
Only protected
D
Only private
12

Question 12

Subclasses of abstract classes must:

A
Implement all abstract members
B
Override all methods
C
Use only public methods
D
Declare their own abstract fields
13

Question 13

Which is true about overriding access modifiers?

A
You cannot make visibility more restrictive when overriding
B
You cannot make visibility more open when overriding
C
You must keep visibility the same always
D
Visibility is ignored during overriding
14

Question 14

Why is this valid?

typescript
abstract class A {
  abstract draw(): void;
}
abstract class B extends A {}
A
Because B is abstract and does not need to implement draw()
B
Because A has no constructor
C
Because draw() is private
D
Because abstract classes cannot extend others
15

Question 15

What is the visibility of run()?

typescript
class A {
  private run() {}
}
A
Accessible only within class A
B
Accessible in subclasses
C
Accessible everywhere
D
Accessible to objects of type A
16

Question 16

What can access this field?

typescript
class A {
  protected value = 5;
}
A
A and its subclasses
B
External callers only
C
Only static methods
D
Any file that imports A
17

Question 17

Abstract classes may contain:

A
Constructors
B
No constructors ever
C
Only private fields
D
Only static methods
18

Question 18

Can abstract properties exist?

A
Yes, subclasses must implement them
B
No, only methods can be abstract
C
Yes, but only if static
D
Yes, but only if private
19

Question 19

Access modifiers can be applied to:

A
Fields, methods, accessors, and constructors
B
Only fields
C
Only constructors
D
Only static members
20

Question 20

Why does this error?

typescript
abstract class A {
  abstract x: number;
}
class B extends A {}
A
Subclass must implement x
B
Abstract classes cannot have fields
C
x cannot be a number
D
B must also be abstract
21

Question 21

Why is this valid?

typescript
abstract class A {
  abstract act(): void;
}
abstract class B extends A {
  abstract step(): void;
}
A
Abstract subclasses may defer abstract methods
B
act() is private
C
step() is optional
D
B overrides act()
22

Question 22

Which visibility is most restrictive?

A
private
B
protected
C
public
D
readonly
23

Question 23

public members are accessible:

A
From any location
B
From subclasses only
C
From same file only
D
From static contexts only
24

Question 24

protected prevents access from:

A
External callers outside the class hierarchy
B
Subclasses
C
Constructor functions
D
Methods inside the class
25

Question 25

private prevents access from:

A
Subclasses and external callers
B
Only external callers
C
Only constructors
D
Only abstract methods
26

Question 26

Why is this invalid?

typescript
class A {
  protected x = 3;
}
const a = new A();
console.log(a.x);
A
x is not visible outside the class hierarchy
B
protected cannot be used on fields
C
x must be private
D
protected fields cannot be logged
27

Question 27

Why does this error?

typescript
class A {
  private run() {}
}
class B extends A {
  run() {}
}
A
private methods cannot be overridden
B
run() must be static
C
B must be abstract
D
A must be abstract
28

Question 28

Why is this valid?

typescript
abstract class A {
  protected abstract step(): void;
}
class B extends A {
  step() {}
}
A
Subclasses must implement protected abstract members using compatible visibility
B
step() becomes public automatically
C
Abstract methods must become private
D
Abstract methods cannot be protected
29

Question 29

Abstract classes help:

A
Define shared templates with enforced subclass behavior
B
Prevent inheritance
C
Disable visibility rules
D
Replace all interfaces
30

Question 30

Access modifiers help:

A
Control which parts of a class can be accessed
B
Determine runtime performance
C
Auto-generate code
D
Force static-only usage
31

Question 31

readonly helps ensure:

A
Values do not change after construction
B
Fields cannot be read
C
Only private fields exist
D
Values must be numbers
32

Question 32

protected is useful when:

A
You want subclasses to access members without exposing them publicly
B
You want the strictest visibility
C
You need to lock all access
D
You want members available everywhere
33

Question 33

private is helpful when:

A
You want to enforce full encapsulation inside a class
B
You want subclass access
C
You need public inheritance
D
You want fields to be readonly
34

Question 34

Abstract classes differ from interfaces because:

A
They can contain actual implementation
B
They cannot contain methods
C
They cannot be extended
D
They cannot contain fields
35

Question 35

Subclasses may override methods with:

A
Equal or more accessible visibility
B
Less accessible visibility
C
Only private visibility
D
Any visibility ignoring the base class
36

Question 36

Abstract methods must:

A
Have no implementation in the base class
B
Always be static
C
Be private
D
Return void only
37

Question 37

public allows:

A
Full access with no restrictions
B
Access from subclasses only
C
Access from same file only
D
No access outside method bodies
38

Question 38

private prevents:

A
Subclass access completely
B
Access within same class
C
Access from other modules only
D
Access from static contexts
39

Question 39

Which scenario best fits abstract classes?

A
Sharing base behavior while requiring subclasses to complete details
B
Modeling shapes only through interfaces
C
When no shared behavior is needed
D
When constructors must be private
40

Question 40

Overall, access modifiers support:

A
Encapsulation and safe class design
B
Runtime mutation of visibility
C
Removing type checking
D
Overriding constructors only

QUIZZES IN TypeScript