This quiz explores object-oriented programming concepts in TypeScript, including classes and object types, covering topics such as class properties, access modifiers, readonly properties, getters and setters, abstract classes, methods, and interfaces. It also highlights the differences between interfaces and type aliases, offering a comprehensive understanding of TypeScript’s features.
Question 1
Which of the following defines a property in a class in TypeScript?
property: string;
var property = "value";
function property() {}
this.property: number;
Question 2
Which access modifier allows a property to be accessed only within the class in which it is defined?
public
private
protected
readonly
Question 3
What happens if you try to modify a property marked as readonly?
It throws a runtime error.
It throws a compile-time error.
It logs a warning to the console.
It silently ignores the change.
Question 4
What is the purpose of a setter in a class?
To define a method that performs an action
To retrieve the value of a private property.
To set or modify the value of a private property.
To call a constructor with parameters.
Question 5
Which of the following is a correct example of a getter method in TypeScript?
getProperty { return this.property; }
get getProperty() { return this.property; }
getter getProperty() { return this.property; }
function getProperty() { return this.property; }
Question 6
What is the key characteristic of an abstract class?
It cannot have any methods.
It can be instantiated directly.
It can have both abstract and non-abstract methods.
It must have only abstract methods.
Question 7
How is an abstract method declared in TypeScript?
abstract myMethod() {};
abstract myMethod() {}:
abstract myMethod(): void;
function abstract myMethod() {}
Question 8
What happens if a class does not implement all properties of an interface it claims to implement?
It compiles but logs a warning.
It throws a runtime error.
It fails to compile.
It silently ignores the missing properties.
Question 9
How do you declare that a class implements multiple interfaces?
class MyClass extends Interface1, Interface2 {}
class MyClass implements Interface1, Interface2 {}
class MyClass inherits Interface1, Interface2 {}
class MyClass extends Interface1 implements Interface2 {}
Question 10
Which of the following is a valid implementation of an interface in a class?
interface Vehicle {
speed: number;
move(): void;
}
class Car implements Vehicle {
speed: number;
move() {
console.log(`The car is moving at ${this.speed} km/h`);
}
}
The class Car correctly implements Vehicle.
The class Car is missing the speed property.
The class Car is missing the move method.
The move method in Car has the wrong return type.
There are 10 questions to complete.