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