Any2Quiz
Quizzes / 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.

This Languages TypeScript practice quiz has 15 multiple-choice questions on Interfaces, and 15 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.

Start quiz: 15 questions, free

Sample questions

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

    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?

    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?

    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`?

    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?

    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 questions in this quiz

Answers and explanations are in the full quiz.

  1. Which keyword is used to declare an interface in TypeScript?
  2. Consider an interface `Person` with a property `name: string`. If an object `john` is declared as `{ name: 'John', age: 30 }`, which of the following statements is true regarding assigning `john` to a variable of type `Person`?
  3. What does it mean for an interface to be 'readonly'?
  4. Consider an interface `Shape` with a method `getArea(): number`. If a class `Circle` implements `Shape`, what must `Circle` provide?
  5. Can a TypeScript interface extend multiple other interfaces?
  6. What is the purpose of an interface as an 'index signature'?
  7. What is the difference between `interface` and `type` aliases in TypeScript?
  8. When using an interface to describe a function signature, what is the correct syntax?
  9. Consider an interface `User` with a method `login(): void`. If a class `Admin` implements `User` and has a method `login(username: string): void`, will this compile?
  10. What does `interface ReadonlyArray<T>` represent in TypeScript?

More quizzes

Browse all TypeScript quizzes

More to play

Daily ChallengeA new quiz every day. Keep your streak.HangmanGuess the word, letter by letter.Story QuestLearn through short interactive stories.All quizzesCBSE, ICSE, competitive exams and more.