TypeScript Object Oriented Programming Quiz (original) (raw)
Which of the following defines a property in a class in TypeScript?
Which access modifier allows a property to be accessed only within the class in which it is defined?
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.
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.
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; }
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.
How is an abstract method declared in TypeScript?
- abstract myMethod(): void;
- function abstract myMethod() {}
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 silently ignores the missing properties.
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 {}
Which of the following is a valid implementation of an interface in a class?
TypeScript ``
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.
Take a part in the ongoing discussion