TypeScript Optional and Default Parameters Quiz

TypeScript
0 Passed
0% acceptance

A 30-question TypeScript quiz exploring optional parameters, default parameter values, undefined handling, rest parameters, function declaration rules, and parameter type inference.

30 Questions
~60 minutes
1

Question 1

What does a question mark after a parameter name represent?

A
The parameter is optional
B
The parameter must be null
C
The parameter can only be a boolean
D
The parameter is readonly
2

Question 2

What type does an optional parameter implicitly include?

A
undefined
B
null
C
never
D
unknown
3

Question 3

What is the type of msg?

typescript
function log(msg?: string) {
  return msg;
}
A
string | undefined
B
string
C
undefined only
D
never
4

Question 4

Optional parameters must appear:

A
After all required parameters
B
Before all required parameters
C
Anywhere in the list without restriction
D
Only when using enums
5

Question 5

A default parameter value applies when:

A
The argument is explicitly undefined or omitted
B
The argument is null
C
The argument is zero
D
The argument is an empty string
6

Question 6

What value does count receive?

typescript
function increment(count: number = 10) {
  return count;
}
increment();
A
10
B
undefined
C
0
D
null
7

Question 7

What does a default parameter imply about requiredness?

A
A parameter with a default is considered optional
B
It is required even with a default
C
It becomes readonly
D
It must be a number
8

Question 8

What is the type of n?

typescript
function use(n = 5) {
  return n;
}
A
number
B
number | undefined
C
any
D
string
9

Question 9

A parameter cannot be both:

A
Optional and defaulted syntactically at once
B
Used and unused
C
Typed and untyped
D
Function and variable
10

Question 10

Defaults are evaluated:

A
At call time
B
At compile time
C
Only once globally
D
When initializing the file
11

Question 11

What does this return?

typescript
function f(v: number = 3) {
  return f(undefined);
}
A
3
B
undefined
C
NaN
D
0
12

Question 12

Optional parameters implicitly behave like:

A
param?: T is param: T | undefined
B
param?: T is param: T
C
param?: T is param: any
D
param?: T is param: never
13

Question 13

Why must optional parameters come last?

A
To avoid ambiguity in call expressions
B
To reduce compile times
C
To support enums
D
To match JavaScript runtime rules
14

Question 14

Why is this invalid?

typescript
function bad(a?: number, b: string) {}
A
Optional param cannot precede a required one
B
string cannot follow number
C
Functions must have defaults
D
The types conflict automatically
15

Question 15

Overloads often avoid optional parameters when:

A
Call signatures would become ambiguous
B
The function returns void
C
Using object parameters
D
Using arrow functions
16

Question 16

Rest parameters behave like:

A
An always-present array type
B
A literal tuple
C
An undefined-only parameter
D
A union of primitives
17

Question 17

What does this call output?

typescript
function join(sep = ',', ...items: string[]) {
  return items.join(sep);
}
join('-', 'a', 'b');
A
a-b
B
ab
C
undefined
D
error
18

Question 18

Rest parameters cannot be:

A
Followed by additional parameters
B
Used with arrays
C
Used with tuples
D
Typed explicitly
19

Question 19

Why does this compile?

typescript
function show(x?: number) {}
show(undefined);
A
Optional parameters accept undefined
B
All parameters accept undefined
C
undefined coerces to zero
D
The function returns void
20

Question 20

Optional does NOT imply:

A
That null is allowed
B
That undefined is allowed
C
That the parameter can be omitted
D
That narrowing applies
21

Question 21

What value is assigned to rate?

typescript
function calc(total: number, rate = total * 0.1) {
  return rate;
}
calc(50);
A
5
B
undefined
C
0
D
null
22

Question 22

What is true about default parameters?

A
Defaults may rely on earlier parameter values
B
Defaults must be literals
C
Defaults must be functions
D
Defaults cannot use expressions
23

Question 23

When is an optional parameter preferred over a default?

A
When you need to detect whether the argument was provided
B
When you want a computed fallback
C
When the function must always initialize the variable
D
When you need numeric coercion
24

Question 24

Which call distinguishes actual undefined from omission?

typescript
function check(v?: number) {
  return arguments.length;
}
A
arguments.length
B
typeof v
C
v === null
D
v > 0
25

Question 25

Default parameters are useful when:

A
A missing argument should fall back to a known value
B
You must detect omitted arguments
C
You need a required argument
D
You want to forbid undefined
26

Question 26

A parameter with a default is considered:

A
Optional
B
Required
C
never
D
unknown
27

Question 27

Optional parameters should be avoided when:

A
They may cause overload ambiguities
B
They appear last
C
They are typed as numbers
D
They appear in arrow functions
28

Question 28

Which is true about function calls involving default parameters?

A
Parameters with defaults can be skipped using undefined
B
Parameters with defaults cannot use undefined
C
Default parameters must be constants
D
Default parameters cannot follow rest parameters
29

Question 29

Optional parameters are commonly used for:

A
Config objects and flexible APIs
B
Forcing runtime type errors
C
Controlling tuple types
D
Automatic narrowing
30

Question 30

Overall, default parameters help developers by:

A
Providing clear fallback values without requiring callers to supply arguments
B
Removing parameter types
C
Enforcing strict null checks automatically
D
Eliminating undefined

QUIZZES IN TypeScript