MCQs on Interfaces | TypeScript

Interfaces in TypeScript provide a powerful way to define custom types, enforce structure, and enhance code flexibility. This set of 30 MCQs covers the fundamentals of interfaces, including their definition, usage, optional and readonly properties, and function interfaces.


Typescript Interfaces – MCQs

1. What are Interfaces?

  1. What is the primary purpose of an interface in TypeScript?
    • a) To define the structure of an object
    • b) To compile TypeScript code into JavaScript
    • c) To execute TypeScript code on the server
    • d) To handle asynchronous operations
  2. Which of the following best describes a TypeScript interface?
    • a) A blueprint for creating instances of a class
    • b) A way to define the structure and types of an object
    • c) A function that returns an object
    • d) A method to create modules in TypeScript
  3. Can an interface in TypeScript include methods?
    • a) No, interfaces can only define properties
    • b) Yes, interfaces can include method signatures without implementations
    • c) Yes, but only if they are static methods
    • d) No, interfaces are strictly for defining constants
  4. Which keyword is used to define an interface in TypeScript?
    • a) define
    • b) type
    • c) interface
    • d) struct
  5. When should you use an interface over a type alias in TypeScript?
    • a) When you want to define a union of types
    • b) When you need to define a class
    • c) When you want to extend or implement a structure
    • d) When you need to work with primitive data types
  6. Which of the following is NOT true about TypeScript interfaces?
    • a) They can extend other interfaces
    • b) They are used to define the shape of data
    • c) They can be used to enforce function parameter types
    • d) They can have implemented methods

2. Defining and Using Interfaces

  1. How do you define an interface with properties in TypeScript?
    • a) Using the define keyword
    • b) Using the interface keyword
    • c) Using the class keyword
    • d) Using the module keyword
  2. Can you assign a type that matches the structure of an interface without explicitly using the implements keyword?
    • a) Yes, TypeScript uses duck typing
    • b) No, the implements keyword is required
    • c) Yes, but only for classes
    • d) No, TypeScript enforces strict typing
  3. What is the output of the following code?typescriptCopy codeinterface User { name: string; age: number; } const user: User = { name: "Alice", age: 25 }; console.log(user.name);
    • a) Alice
    • b) undefined
    • c) Error: Type mismatch
    • d) null
  4. How can you make a property mandatory in an interface?
    • a) Prefix it with a question mark ?
    • b) Just declare it without a modifier
    • c) Prefix it with an exclamation mark !
    • d) Use the required keyword
  5. What is the result of attempting to assign a property that does not exist in an interface?
    • a) The assignment will silently fail
    • b) TypeScript will throw a compilation error
    • c) The property will be ignored
    • d) It will convert the interface to a type alias
  6. Can interfaces be used to describe array types in TypeScript?
    • a) No, interfaces are only for objects
    • b) Yes, using index signatures
    • c) Yes, but only with generics
    • d) No, arrays use type aliases only

3. Optional and Readonly Properties

  1. How do you define an optional property in a TypeScript interface?
    • a) By using the optional keyword
    • b) By adding a question mark ? after the property name
    • c) By using the nullable keyword
    • d) By setting the property to undefined
  2. What is the purpose of the readonly modifier in an interface?
    • a) To prevent a property from being enumerated
    • b) To make a property immutable after initialization
    • c) To allow a property to accept multiple types
    • d) To delay the initialization of the property
  3. Can optional properties in an interface be used together with readonly?
    • a) No, they are mutually exclusive
    • b) Yes, they can be combined
    • c) Yes, but only in classes
    • d) No, only methods can be marked readonly
  4. What happens if you try to modify a readonly property?
    • a) It will be modified successfully
    • b) It will throw a runtime error
    • c) TypeScript will throw a compilation error
    • d) The value will reset to its default
  5. How do you indicate a property is optional in the following interface?typescriptCopy codeinterface Car { model: string; year?: number; }
    • a) The property year is optional
    • b) The property model is optional
    • c) Both properties are optional
    • d) Neither property is optional
  6. Which of the following interface properties is both optional and readonly?
    • a) readonly name: string
    • b) name?: string
    • c) readonly name?: string
    • d) name: string | undefined

4. Function Interfaces

  1. What is a function interface in TypeScript?
    • a) An interface used exclusively for classes
    • b) An interface that describes the signature of a function
    • c) An interface that returns an object
    • d) An interface with only primitive properties
  2. How do you define a function signature in an interface?
    interface Greet { (name: string): string; }
    • a) The interface defines a class method
    • b) The interface defines a function signature
    • c) The interface defines an object with methods
    • d) The interface is invalid
  3. Which of the following interfaces defines a function that accepts two numbers and returns their sum?
    • a) interface Sum { (a: number, b: number): number }
    • b) interface Sum { a: number; b: number }
    • c) interface Sum { calculate(a: number, b: number): void }
    • d) interface Sum { sum(a: string, b: string): string }
  4. Can function interfaces be used with arrow functions?
    • a) No, they only work with regular functions
    • b) Yes, arrow functions can match interface signatures
    • c) Yes, but only for async functions
    • d) No, interfaces do not support arrow functions
  5. How do you implement a function interface in a class?
    • a) By using the implements keyword
    • b) By using the extends keyword
    • c) By assigning the function directly to a property
    • d) By defining the function separately
  6. Which of the following uses a function interface correctly?
    interface Calculate { (x: number, y: number): number; } const add: Calculate = (a, b) => a + b;
    • a) This code will throw an error
    • b) This code is valid and defines a function type
    • c) This code only works in JavaScript
    • d) This code is missing a return type

5. Advanced Use Cases

  1. Can interfaces extend multiple other interfaces in TypeScript?
    • a) No, TypeScript does not support multiple inheritance
    • b) Yes, using the extends keyword
    • c) Yes, using the implements keyword
    • d) No, interfaces cannot extend other interfaces
  2. How do you extend an interface in TypeScript?
    • a) interface NewInterface extends ExistingInterface
    • b) class NewInterface implements ExistingInterface
    • c) type NewInterface = ExistingInterface
    • d) const NewInterface = ExistingInterface
  3. Can interfaces in TypeScript have private properties?
    • a) Yes, but only in classes
    • b) No, interfaces cannot have access modifiers
    • c) Yes, using the private keyword
    • d) No, only public properties are allowed
  4. How do you mark a property as deprecated in an interface?
    • a) Use the deprecated keyword
    • b) Use the @deprecated annotation
    • c) Use the readonly keyword
    • d) Use the ? to indicate optionality
  5. What is the benefit of using interfaces in TypeScript?
    • a) They enhance runtime performance
    • b) They enforce type safety and code consistency
    • c) They reduce code execution time
    • d) They allow dynamic typing
  6. Can interfaces be used to type-check function parameters in TypeScript?
    • a) No, only types can be used for this purpose
    • b) Yes, interfaces can enforce the structure of function parameters
    • c) No, TypeScript only uses types for functions
    • d) Yes, but only with default parameters

Answers

QNoAnswer
1a) To define the structure of an object
2b) A way to define the structure and types of an object
3b) Yes, interfaces can include method signatures without implementations
4c) interface
5c) When you want to extend or implement a structure
6d) They can have implemented methods
7b) Using the interface keyword
8a) Yes, TypeScript uses duck typing
9a) Alice
10b) Just declare it without a modifier
11b) TypeScript will throw a compilation error
12b) Yes, using index signatures
13b) By adding a question mark ? after the property name
14b) To make a property immutable after initialization
15b) Yes, they can be combined
16c) TypeScript will throw a compilation error
17a) The property year is optional
18c) readonly name?: string
19b) An interface that describes the signature of a function
20b) The interface defines a function signature
21a) interface Sum { (a: number, b: number): number }
22b) Yes, arrow functions can match interface signatures
23a) By using the implements keyword
24b) This code is valid and defines a function type
25b) Yes, using the extends keyword
26a) interface NewInterface extends ExistingInterface
27b) No, interfaces cannot have access modifiers
28b) Use the @deprecated annotation
29b) They enforce type safety and code consistency
30b) Yes, interfaces can enforce the structure of function parameters

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top