MCQs on Functions in TypeScript | TypeScript

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

  1. 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()
  2. 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
  3. 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
  4. 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
  5. What is the correct way to define a function type that returns nothing?
    a) () => null
    b) () => void
    c) () => undefined
    d) () => none
  6. 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
  7. 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

  1. 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)
  2. 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
  3. 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”)
  4. 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
  5. 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
  6. What does the question mark after a parameter name signify?
    a) Default parameter
    b) Optional parameter
    c) Rest parameter
    d) Type parameter
  7. 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

  1. 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
  2. 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[])
  3. 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
  4. 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)
  5. What type does the rest parameter return?
    a) Object
    b) Array
    c) Set
    d) Map
  6. 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
  7. 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

  1. 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
  2. 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
  3. 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;
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. Which keyword is used to define different return types in function overloading?
    a) type
    b) interface
    c) union
    d) overload

Answers

QnoAnswer
1b) let fn: () => void
2b) A list of parameters and their types
3b) let myFunction: () => string
4b) (a: number, b: number) => string
5b) () => void
6d) function fn(): number
7c) const
8d) function fn(param?: string)
9b) It is assigned undefined
10b) function fn(param: string = “default”)
11b) No
12c) 15
13b) Optional parameter
14a) function fn(param1?: string, param2: number)
15b) To accept multiple arguments as an array
16b) function fn(…params: number[])
17a) Yes, as long as the rest parameter is last
18a) function addNumbers(…nums: number[]): number
19b) Array
20b) They allow more flexible function definitions
21a) 4
22d) Defining multiple function signatures with the same name
23b) Writing multiple function signatures above the implementation
24a) Valid overloaded function declaration
25a) Yes
26b) Enhanced readability and type safety
27a) After all the overload signatures
28b) TypeScript throws a compile-time error
29a) Using type guards inside the implementation
30c) union

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