Functions in TypeScript are a powerful feature that enhances code flexibility and reusability. This chapter covers function types, signatures, optional and default parameters, rest parameters, and function overloading—essential concepts for efficient TypeScript programming.
Multiple Choice Questions (MCQs)
Section 1: Function Types and Signatures
How do you define a function type in TypeScript? a) let fn: Function b) let fn: () => void c) let fn: (number, string) => void d) let fn: function()
What is a function signature? a) A special type of comment b) A list of parameters and their types c) The actual implementation of a function d) A return type only
Which of the following is a valid TypeScript function type? a) let myFunction: () -> void b) let myFunction: () => string c) let myFunction: [] => number d) let myFunction: {} => boolean
How do you specify a function that takes two parameters of type number and returns a string? a) (a: number, b: number) -> string b) (a: number, b: number) => string c) (a: string, b: string) => number d) (a: number, b: string) => number
What is the correct way to define a function type that returns nothing? a) () => null b) () => void c) () => undefined d) () => none
How can you define a function with a specific return type in TypeScript? a) function fn(): void b) function fn(): return c) function fn(): any d) function fn(): number
In TypeScript, which keyword is used to declare a function expression? a) declare b) type c) const d) export
Section 2: Optional and Default Parameters
How do you define an optional parameter in a TypeScript function? a) function fn(param?) b) function fn(param = undefined) c) function fn(param?) d) function fn(param?: string)
What happens if you don’t pass an optional parameter in TypeScript? a) It throws an error b) It is assigned undefined c) It defaults to an empty string d) It uses the previous value
How do you assign a default value to a parameter in TypeScript? a) function fn(param = “default”) b) function fn(param: string = “default”) c) function fn(param?: “default”) d) function fn(param default “default”)
Can optional parameters appear before required parameters in a function? a) Yes b) No c) Only in strict mode d) Only with a specific flag
What is the output of the following function call: myFunction(5) if myFunction is defined as function myFunction(x: number, y: number = 10) { return x + y; }? a) 5 b) 10 c) 15 d) 20
What does the question mark after a parameter name signify? a) Default parameter b) Optional parameter c) Rest parameter d) Type parameter
How do you make the first parameter optional in TypeScript? a) function fn(param1?: string, param2: number) b) function fn(param1: string?, param2: number) c) function fn(param1: optional string, param2: number) d) function fn(param1: string, param2: number?)
Section 3: Rest Parameters
What are rest parameters used for in TypeScript? a) To specify the return type b) To accept multiple arguments as an array c) To set default parameter values d) To declare constants
How do you define a rest parameter in a TypeScript function? a) function fn(…params) b) function fn(…params: number[]) c) function fn(*params: string[]) d) function fn(params: rest[])
Can you mix regular and rest parameters in TypeScript? a) Yes, as long as the rest parameter is last b) No, rest parameters must come first c) Yes, in any order d) No, it is not allowed
Which of the following function definitions is correct for a rest parameter? a) function addNumbers(…nums: number[]): number b) function addNumbers(…nums): number[] c) function addNumbers(nums…: number[]) d) function addNumbers(…nums: string)
What type does the rest parameter return? a) Object b) Array c) Set d) Map
How do rest parameters affect function overloading? a) They disable overloading b) They allow more flexible function definitions c) They limit function signatures d) They require default values
What will the following code return:typescriptCopy codefunction sumAll(...nums: number[]) { return nums.length; } sumAll(1, 2, 3, 4); a) 4 b) 10 c) 15 d) Error
Section 4: Overloading Functions
What is function overloading in TypeScript? a) Declaring multiple functions with the same name and different return types b) Writing a function inside another function c) Declaring multiple functions with different names but similar parameters d) Defining multiple function signatures with the same name
How do you define multiple function signatures in TypeScript? a) Using the overload keyword b) Writing multiple function signatures above the implementation c) Using type aliases d) Defining multiple implementations in one function
Which of the following is a valid overloaded function declaration? a)typescriptCopy codefunction add(x: number, y: number): number; function add(x: string, y: string): string; b)typescriptCopy codefunction add(x: number | string, y: number | string): any; c)typescriptCopy codefunction add(x: any, y: any): any; d)typescriptCopy codefunction add(x, y): number | string;
Can overloaded functions have the same number of parameters but different types? a) Yes b) No c) Only in ES6 d) Only with type assertion
What is the main benefit of using function overloading? a) Improved memory usage b) Enhanced readability and type safety c) Faster code execution d) Reduced code size
Where do you write the actual implementation of an overloaded function? a) After all the overload signatures b) Before the overload signatures c) Inside the first signature d) In a separate file
What happens if a function call does not match any overload signature? a) TypeScript uses the last signature b) TypeScript throws a compile-time error c) It uses the first signature as a fallback d) It runs without type checking
How do you return different types from an overloaded function based on input types? a) Using type guards inside the implementation b) Using separate functions c) Using conditional statements outside the function d) Using the typeof operator only
Which keyword is used to define different return types in function overloading? a) type b) interface c) union d) overload
Answers
Qno
Answer
1
b) let fn: () => void
2
b) A list of parameters and their types
3
b) let myFunction: () => string
4
b) (a: number, b: number) => string
5
b) () => void
6
d) function fn(): number
7
c) const
8
d) function fn(param?: string)
9
b) It is assigned undefined
10
b) function fn(param: string = “default”)
11
b) No
12
c) 15
13
b) Optional parameter
14
a) function fn(param1?: string, param2: number)
15
b) To accept multiple arguments as an array
16
b) function fn(…params: number[])
17
a) Yes, as long as the rest parameter is last
18
a) function addNumbers(…nums: number[]): number
19
b) Array
20
b) They allow more flexible function definitions
21
a) 4
22
d) Defining multiple function signatures with the same name
23
b) Writing multiple function signatures above the implementation