TypeScript Functions and Parameters Quiz

TypeScript
0 Passed
0% acceptance

A 30-question TypeScript quiz covering function declarations, expressions, parameter types, optional and default parameters, rest arguments, and return type handling.

30 Questions
~60 minutes
1

Question 1

Which syntax defines a function declaration?

A
function add(a, b) { return a + b }
B
const add = (a, b) => a + b
C
add: function(a, b)
D
method add(a, b)
2

Question 2

Which statement is true about function expressions?

A
They assign a function value to a variable
B
They always require names
C
They cannot accept parameters
D
They must be exported
3

Question 3

What is the type of fn?

typescript
const fn = (x: number): number => x * 2;
A
(x: number) => number
B
number
C
any
D
(x: string) => string
4

Question 4

Why is it helpful to annotate a function's return type?

A
It prevents unintended changes to return values
B
It speeds up runtime execution
C
It hides type errors
D
It forces all parameters to be optional
5

Question 5

Which is a valid function type annotation?

A
let add: (a: number, b: number) => number
B
let add: function(a: number)
C
let add => number
D
let add(): number
6

Question 6

What does this parameter list mean?

typescript
function greet(name: string, age?: number) {}
A
age is optional
B
age must be provided always
C
name is optional
D
Both are optional
7

Question 7

Which is true about optional parameters?

A
They automatically become type | undefined
B
They cannot have explicit types
C
They must always be the first argument
D
They disable type checking
8

Question 8

What type does TypeScript infer for the return value?

typescript
function add(a: number, b: number) { return a + b; }
A
number
B
any
C
unknown
D
void
9

Question 9

Which statement about parameter types is correct?

A
Every parameter can be typed individually
B
All parameters must share one type
C
TypeScript forbids mixing typed and untyped parameters
D
Types are only allowed on return values
10

Question 10

What does the parameter type indicate?

typescript
function log(value: string | number) {}
A
value may be a string or a number
B
value must be a string only
C
value is optional
D
value is a function
11

Question 11

What is true about default parameters?

A
They apply when a value is undefined
B
They override provided arguments
C
They must be the first parameter
D
They cannot have explicit types
12

Question 12

What value does this function return?

typescript
function multiply(a: number, b: number = 2) {
  return a * b;
}
multiply(5);
A
10
B
5
C
undefined
D
Error
13

Question 13

Which syntax defines a rest parameter?

A
...args: number[]
B
args...
C
rest args[]
D
args: rest
14

Question 14

What does total receive?

typescript
function sum(...nums: number[]) {
  return nums.length;
}
sum(1, 2, 3);
A
An array containing [1,2,3]
B
Only the last argument
C
The first argument only
D
undefined
15

Question 15

Which rule applies to rest parameters?

A
They must be the final parameter
B
They cannot have types
C
They cannot be used with arrays
D
They only accept strings
16

Question 16

What is the inferred return type?

typescript
function hello() {
  return 'hi';
}
A
string
B
any
C
void
D
unknown
17

Question 17

When is it useful to annotate return types?

A
When inferring might hide subtle bugs
B
Only in async functions
C
Only when parameters are optional
D
Whenever using var
18

Question 18

Which return type indicates a function never finishes?

A
never
B
void
C
undefined
D
null
19

Question 19

What is the return type?

typescript
function fail(msg: string): never {
  throw new Error(msg);
}
A
never
B
void
C
string
D
unknown
20

Question 20

What does void represent as a return type?

A
A function that returns nothing
B
A function that returns undefined explicitly
C
A function that returns any value
D
A function that must throw
21

Question 21

What is the type of this function?

typescript
const fn = function(a: number, b: number): number {
  return a + b;
};
A
(a: number, b: number) => number
B
number
C
any
D
void
22

Question 22

What does a function overload define?

A
Multiple call signatures for the same function
B
Multiple return values at once
C
Alternate variable declarations
D
A new scope block
23

Question 23

Which statement about arrow functions is correct?

A
Their return type can be inferred or annotated
B
They cannot have typed parameters
C
They must always return void
D
They create new scopes for this binding
24

Question 24

What rule applies to optional parameters?

A
They must come after required parameters
B
They must be the first parameter
C
They require a default value
D
They cannot have types
25

Question 25

Which pattern ensures a function always returns a string?

A
Annotating the return type as : string
B
Omitting return entirely
C
Using var inside the function
D
Declaring no parameters
26

Question 26

Which keyword is used for asynchronous functions?

A
async
B
await
C
asyncfn
D
promise
27

Question 27

What is the return type of an async function?

A
Promise<ReturnValue>
B
ReturnValue only
C
never
D
void always
28

Question 28

Rest parameters are useful because they:

A
Allow a function to accept unlimited arguments
B
Guarantee faster execution
C
Remove the need for optional parameters
D
Disable type checking
29

Question 29

Which type best represents a function with no parameters and no return value?

A
() => void
B
() => never
C
(arg: any) => any
D
() => undefined
30

Question 30

Why is typing parameters important?

A
It ensures functions receive the right kinds of data
B
It changes runtime behaviour
C
It prevents using arrow functions
D
It disables inference

QUIZZES IN TypeScript