Any2Quiz
Computer Science / Languages / TypeScript

Interfaces Quiz - Languages TypeScript

Free practice quiz: Interfaces (Languages TypeScript). 15 multiple-choice questions with answers and explanations. Play online, no sign-up needed.

Play all 15 questions free

Sample questions

  1. In TypeScript, what is the primary purpose of an interface?

    1. To define the structure of an object or a class.
    2. To provide default implementations for methods.
    3. To manage memory allocation automatically.
    4. To enforce strict type checking on function arguments only.
    Show answer

    Answer: A. To define the structure of an object or a class.

    Interfaces in TypeScript define the contract or shape that an object or class must adhere to. They specify the properties and method signatures that an object should have, but do not provide implementation details. This helps in achieving type safety and code organization.

  2. Can an interface in TypeScript define optional properties? If so, how?

    1. Yes, by using the `optional` keyword before the property name.
    2. No, all properties in an interface must be mandatory.
    3. Yes, by appending a `?` after the property name.
    4. Yes, by enclosing the property in parentheses.
    Show answer

    Answer: C. Yes, by appending a `?` after the property name.

    Optional properties in TypeScript interfaces are declared by appending a `?` after the property name. This signifies that the property might not be present on objects implementing the interface.

  3. What is interface inheritance in TypeScript?

    1. A mechanism where one interface extends another, inheriting its members.
    2. A mechanism where a class inherits from multiple interfaces.
    3. A mechanism where an interface inherits from a class, copying its properties.
    4. A mechanism where an interface defines default implementations for its methods.
    Show answer

    Answer: A. A mechanism where one interface extends another, inheriting its members.

    Interfaces can extend other interfaces using the `extends` keyword. This allows for creating a hierarchy of interfaces, where a new interface inherits all members of the interface it extends.

  4. Consider the following interface: `interface Config { readonly timeout: number; logEnabled?: boolean; }`. Which of the following object literal is compatible with `Config`?

    1. `{ timeout: 1000, logEnabled: true, retries: 3 }`
    2. `{ timeout: 500 }`
    3. `{ logEnabled: false }`
    4. `{ timeout: 'infinite', logEnabled: true }`
    Show answer

    Answer: B. `{ timeout: 500 }`

    The object literal must have a `timeout` property of type `number`. `logEnabled` is optional. The first option has an extra property `retries`, which is fine. However, the `timeout` in the fourth option is a string, not a number. The third option is missing the mandatory `timeout` property. The second option correctly provides the mandatory `timeout` property.

  5. What is a 'partial' type in the context of TypeScript interfaces?

    1. A utility type that makes all properties of an interface optional.
    2. A utility type that makes all properties of an interface readonly.
    3. A utility type that makes all properties of an interface required.
    4. A utility type that makes all properties of an interface nullable.
    Show answer

    Answer: A. A utility type that makes all properties of an interface optional.

    The `Partial<T>` utility type in TypeScript takes an interface `T` and returns a new type where all properties of `T` are made optional. This is useful when you need to create an object that might only contain a subset of the properties defined by an interface.

Take the full quiz

More quizzes

Browse all TypeScript quizzes