Sample questions
Question 1Easy
What does public in this constructor do?
class User {
constructor(public name: string) {}
}
console.log(new User("Asha").name);
- Only documents the parameter; it has no runtime effect
- Makes name a static property of User
- Makes the constructor callable without the new keyword
- Declares a name property and assigns the argument to it
Answer: D. Declares a name property and assigns the argument to it
Explanation
A parameter property is shorthand for declaring a field and writing this.name = name in the constructor.
Step-by-step solution
- A parameter property with public declares a field called name on the class.
- The constructor assigns the argument to this.name.
- New User('Asha').name is 'Asha'.
- So option D is correct.
Why the other options are wrong
- A. It has a runtime effect: it creates the field and assigns it.
- B. It is an instance field, not a static one.
- C. The constructor still needs new; it is not callable without it.
Tip: A parameter property such as public name: string creates and assigns the field in one step.
Question 2Easy
How many classes can a TypeScript class directly extend?
- None; it must use implements
- Any number of classes
- Two at most
- Exactly one
Answer: D. Exactly one
Explanation
TypeScript follows JavaScript's single inheritance. A class can extend one class but implement any number of interfaces.
Step-by-step solution
- TypeScript has single inheritance.
- A class extends one class.
- It can implement many interfaces.
Why the other options are wrong
- A. It can extend.
- B. Multiple inheritance is not allowed.
- C. No such limit of two.
Tip: extends one, implements many.
Question 3Easy
Where can a protected class member be accessed?
- Anywhere in the same file
- Anywhere, but it cannot be reassigned
- Only inside the class that declares it
- Inside the class and its subclasses
Answer: D. Inside the class and its subclasses
Explanation
protected is like private but also allows access from derived classes. Code outside the class hierarchy cannot use it.
Step-by-step solution
- A protected member is private to its class.
- A subclass can also access it.
- Code outside the class hierarchy cannot access it.
- So it is accessible inside the class and its subclasses (option D).
Why the other options are wrong
- A. The same file is not enough; protected is about the class hierarchy.
- B. Protected says nothing about reassignment; readonly does that.
- C. Subclasses can also access a protected member.
Tip: Private is class-only, protected adds subclasses, and public is open to all.
Question 4Easy
Which line fails to compile?
class Order {
readonly id: number;
constructor() { this.id = 1; } // line A
reset() { this.id = 2; } // line B
}
- Only line B
- Neither line
- Only line A
- Both lines
Answer: A. Only line B
Explanation
A readonly field can be assigned in its declaration or in the constructor, but not in other methods.
Step-by-step solution
- Readonly id can be set in its declaration or in the constructor.
- Line A is in the constructor, so it is allowed.
- Line B is in method reset, which cannot assign a readonly field.
- So only line B fails (option A).
Why the other options are wrong
- B. Line B assigns the readonly field in a method, which is an error.
- C. Line A is fine in the constructor, so the error is only on line B.
- D. Line A does not fail.
Tip: Readonly fields can be assigned only where declared or in the constructor.
Question 5Medium
Why does writing getLabel = () => this.label; as a class property avoid losing this when the method is passed as a callback?
- Arrow functions are automatically bound by the compiler
- TypeScript inserts a call to bind in every callback
- Arrow functions capture this from the new instance
- Class properties are always stored on the prototype
Answer: C. Arrow functions capture this from the new instance
Explanation
An arrow function has no own this; it uses the this of the scope where it was created, which for a class field is the new instance. The cost is one function per instance.
Step-by-step solution
- A class field holding an arrow function is created for each instance.
- An arrow function has no own this; it uses the this of the scope where it was created.
- For a class field that scope is the new instance, so this.label still points to the right object.
- So the callback keeps the right this, and the answer is option C.
Why the other options are wrong
- A. The compiler does not bind arrows automatically; they capture this lexically.
- B. TypeScript does not insert bind calls into callbacks.
- D. Arrow class fields are stored on each instance, not on the prototype.
Tip: Use an arrow function class field for a callback that needs this; a normal method needs bind or an arrow wrapper.
Question 6Medium
What does TypeScript report for Square?
abstract class Shape {
abstract area(): number;
}
class Square extends Shape {
constructor(public side: number) { super(); }
}
- Error:
Square needs the abstract keyword on side - Error:
Square does not implement the abstract area() - No error;
Square silently becomes abstract as well - No error;
area is inherited from Shape
Answer: B. Error: Square does not implement the abstract area()
Explanation
A non-abstract subclass must implement every abstract member, or be declared abstract itself.
Step-by-step solution
- Square extends the abstract class Shape.
- area() is abstract in Shape, and Square does not define it.
- A non-abstract subclass must implement every abstract member.
- So TypeScript reports that Square does not implement area() (option B).
Why the other options are wrong
- A. Side needs no abstract keyword; a parameter property is allowed.
- C. Square is not made abstract; it is an error instead.
- D. Abstract members are not inherited as implementations.
Tip: A concrete subclass must implement every abstract method, or be declared abstract itself.
Question 7Medium
With --strict on, what does TypeScript report?
class Profile {
name: string;
}
- No error; name defaults to an empty string
- Error: classes need an explicit constructor
- No error; name is typed string | undefined
- Error: name is never initialised
Answer: D. Error: name is never initialised
Explanation
strictPropertyInitialization requires every non-optional field to be initialised in its declaration or definitely assigned in the constructor.
Step-by-step solution
- --strict turns on strictPropertyInitialization.
- Name is a non-optional field with no initialiser.
- There is no constructor to assign it, so name is never initialised.
- TypeScript reports that name has no initialiser (option D).
Why the other options are wrong
- A. No default is given to name, so this is an error.
- B. An explicit constructor is not required.
- C. Name is not optional, so its type stays string and it is still an error.
Tip: Under strict, give each non-optional field an initialiser or assign it in the constructor.
Question 8Medium
What effect does implements Printable have on the emitted JavaScript class?
- It copies Printable's default methods into the class
- It adds a runtime check called by the constructor
- None; it only checks the class shape at compile time
- It makes the class inherit from a Printable object
Answer: C. None; it only checks the class shape at compile time
Explanation
Interfaces are erased. implements only asks the compiler to verify that the class has the required members; it adds no code or inheritance.
Step-by-step solution
- Implements Printable tells the compiler to check that the class has Printable's members.
- Interfaces are erased at compile time.
- The emitted JavaScript class is unchanged.
- So implements has no runtime effect (option C).
Why the other options are wrong
- A. An interface has no default methods to copy.
- B. No runtime check is added.
- D. Implements does not inherit; only extends creates inheritance.
Tip: Implements is a compile-time check only; use extends for inheritance.
Question 9Hard
What happens when the compiled code runs?
class Btn {
label = "OK";
getLabel() { return this.label; }
}
const get = new Btn().getLabel;
console.log(get());
- It prints undefined and carries on
- It does not compile, since get is untyped
- It prints "OK", since getLabel is a class method
- It throws a TypeError because this is undefined
Answer: D. It throws a TypeError because this is undefined
Explanation
Calling a detached method loses its this. Class bodies run in strict mode, so this is undefined and reading this.label throws. TypeScript does not catch this case.
Step-by-step solution
- Get holds the method getLabel detached from the Btn object, so it has no bound this.
- Class bodies run in strict mode, so a plain call gives this as undefined.
- Return this.label then reads a property of undefined.
- That throws a TypeError at runtime (option D).
Why the other options are wrong
- A. Reading a property of undefined throws; it does not carry on.
- B. It compiles; TypeScript does not catch this case here.
- C. A method called detached does not keep its object as this.
Tip: Detached class methods lose this; bind them in the constructor or use arrow function properties.
Question 10Hard
What does TypeScript report for the last line?
class A { private x = 1; }
class B { private x = 1; }
const a: A = new B();
- Error: B must extend A explicitly
- No error; the two classes have the same shape
- Error: types have separate declarations of a private x
- No error; private members are ignored for assignability
Answer: C. Error: types have separate declarations of a private x
Explanation
Structural typing has an exception: private and protected members are only compatible if they come from the same declaration. This makes classes with private members behave nominally.
Step-by-step solution
- Classes A and B each declare their own private x.
- Private members are compatible only when they come from the same declaration.
- B's private x is not A's private x, so a B value is not assignable to A.
- So TypeScript reports the separate declarations error (option C).
Why the other options are wrong
- A. No extends relation is needed; the mismatch is about the private members.
- B. The shapes look the same, but private members break structural matching here.
- D. Private members are not ignored for assignability.
Tip: Classes with private or protected members are compared by declaration, not only by shape.
Practise all 18 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →