Learn TypeScript

Quiz

Quiz

This quiz will test what you have learned about working with classes with TypeScript.

1.

What are the types of the a and b fields in the following class?

class Calculator {
a = 0;
b = 0;
}
2.

What is the return type of the add method in the following class?

class Calculator {
static add(a: number, b: number) {
return a + b;
}
}
3.

What is the problem with the following code?

class Calculator {
constructor (private a = 0, private b = 0) {}
add() {
return this.a + this.b;
}
}
const calc = new Calculator();
calc.a = 1;
calc.b = 2;
calc.add();
4.

What will be output to the console after the following code is executed? Will any type errors occur?

abstract class Animal {
constructor (public name: string) {}
protected log(message: string) {
console.log(message);
}
}
class Dog extends Animal {
bark() {
this.log(this.name + " Bark");
}
}
const dog = new Dog("Fudge");
dog.bark();
5.

Will the following code raise a type error?

class Logger {
static log(message: any): void
static log(message: string, category: string): void
static log(message: any, category?: string): void {
console.log(message, category);
}
}
Logger.log({name: "Bob"});
© 2024 Carl Rippon
Privacy Policy
This site uses cookies. Click here to find out more