TypeScript Function Overloads Quiz

TypeScript
0 Passed
0% acceptance

A 35-question TypeScript quiz covering overload signatures, implementation signatures, narrowing behavior, return types, and best practices for overloaded functions.

35 Questions
~70 minutes
1

Question 1

Function overloads allow developers to declare:

A
Multiple call signatures for a single function name
B
Multiple variable declarations
C
Different return types at runtime only
D
Random method selection
2

Question 2

Overload signatures appear:

A
Above the implementation signature
B
Below the implementation signature
C
Inside modules only
D
After return statements
3

Question 3

Which statement about implementation signatures is correct?

A
There is only one implementation signature
B
Each overload needs its own implementation
C
Implementations can return multiple values
D
Implementations must be optional
4

Question 4

What is an overload signature here?

typescript
function load(id: number): string;
function load(id: string): string;
A
A declaration describing acceptable argument types
B
A runtime function
C
A static variable
D
A namespace
5

Question 5

Which line must come last?

typescript
function sum(a: number, b: number): number;
function sum(a: string, b: string): string;
function sum(a: any, b: any) { return a + b; }
A
The implementation signature
B
The first overload only
C
The second overload only
D
Any line can be last
6

Question 6

Function overloads allow TypeScript to:

A
Infer specific return types based on argument combinations
B
Generate overloaded functions at runtime
C
Skip type checking
D
Auto-create union types only
7

Question 7

Overloads can differ by:

A
Parameter count or parameter types
B
File extension
C
Variable hoisting rules
D
Module resolution priority
8

Question 8

Which is true for overload parameters?

A
Optional parameters must appear in all overloads if used
B
Optional parameters disappear inside implementation
C
Optional parameters must be removed in implementation
D
Optional parameters always become required
9

Question 9

Overloads help model:

A
APIs accepting multiple parameter combinations
B
File-level constants
C
Interface merging only
D
Runtime-dynamic imports
10

Question 10

Which call is allowed?

typescript
function format(x: number): number;
function format(x: string): string;
function format(x: any) { return x; }
A
format(10)
B
format(true)
C
format({})
D
All of the above
11

Question 11

Which return type will TypeScript infer?

typescript
function get(x: number): number;
function get(x: string): string;
function get(x: any) { return x; }
const result = get('hi');
A
string
B
number
C
any
D
unknown
12

Question 12

Which call will produce an error?

typescript
function combine(a: string, b: string): string;
function combine(a: number, b: number): number;
function combine(a: any, b: any) { return a + b; }
A
combine(1, 2)
B
combine('a', 'b')
C
combine('a', 1)
D
None produce errors
13

Question 13

Return-type narrowing is achieved because:

A
Each overload can specify a unique return type
B
TypeScript runs functions at compile time
C
Return types are removed after emit
D
Only one return type is allowed
14

Question 14

Overload signatures must:

A
Be compatible with the implementation signature
B
Return only void
C
Match runtime output exactly
D
Be written after implementation
15

Question 15

Which overload will match?

typescript
function parse(x: string): number;
function parse(x: number): number;
function parse(x: any) { return Number(x); }
const out = parse(100);
A
The second overload
B
The first overload
C
No overload matches
D
Both match equally
16

Question 16

Why does this error?

typescript
function select(x: number): number;
function select(x: string): string;
function select(x: boolean) { return x; }
A
The implementation signature must be compatible with all overloads
B
Boolean parameters require metadata
C
Boolean types cannot be overloaded
D
Overloads must use union types
17

Question 17

Which call is valid?

typescript
function ping(): string;
function ping(x: string): string;
function ping(x?: any) { return 'ok'; }
A
ping()
B
ping('hi')
C
Both
D
Neither
18

Question 18

Which return type is inferred?

typescript
function area(r: number): number;
function area(w: number, h: number): number;
function area(...args: any[]) { return 10; }
const a = area(5);
A
number
B
any
C
void
D
unknown
19

Question 19

Which error will occur?

typescript
function op(x: number): number;
function op(x: string): string;
function op(x: number | string) { return x; }
const v = op(true);
A
true does not match any overload signature
B
Return type mismatch
C
Implementation too strict
D
Optional argument missing
20

Question 20

Overloads differ from unions because:

A
They allow return types to depend on argument patterns
B
They disable type inference
C
They remove implementation requirements
D
They execute different functions at runtime
21

Question 21

Overloads should generally be ordered:

A
From most specific to most general
B
Alphabetically
C
Randomly
D
By return type
22

Question 22

Which is true about implementation signatures?

A
They must be compatible with all overload signatures
B
They must be more restrictive than overloads
C
They must not include parameters
D
They must exclude optional parameters
23

Question 23

Overloads can help:

A
Model APIs that behave differently depending on argument count
B
Define runtime execution order
C
Ignore type checking
D
Rewrite import paths
24

Question 24

Which approach often replaces overloads?

A
Generics with conditional return types
B
Enums
C
Namespace merging
D
Ambient declarations only
25

Question 25

Overloads should be avoided when:

A
A single union type signature can express the function clearly
B
More than one return type exists
C
Functions have optional parameters
D
Functions involve async operations
26

Question 26

Which return type is chosen?

typescript
function cast(x: number): number;
function cast(x: string): string;
function cast(x: any) { return x; }
const c = cast(10);
A
number
B
string
C
unknown
D
any
27

Question 27

If no overload matches, TypeScript:

A
Reports a compile-time error
B
Chooses the implementation signature
C
Allows the call but warns
D
Infers a union type
28

Question 28

Overloads are resolved:

A
Top-down, selecting the first matching signature
B
Bottom-up
C
Randomly
D
By alphabetical order
29

Question 29

A mismatch between overloads and implementation:

A
Causes a compile-time error
B
Is ignored by the compiler
C
Is treated as a warning
D
Produces runtime-only issues
30

Question 30

Overloads improve clarity when:

A
Different argument combinations imply different return types
B
A single type applies to all arguments
C
Interfaces are merged
D
The function uses generics exclusively
31

Question 31

Which of the following is NOT required in overload declarations?

A
A corresponding implementation signature
B
Overloads must be written above implementation
C
Overloads may differ in parameter types
D
Overloads must use union types
32

Question 32

Overloads cannot:

A
Be placed after the implementation
B
Use optional parameters
C
Use generics
D
Use different return types
33

Question 33

Overload resolution is based primarily on:

A
Argument types
B
Variable naming
C
Return type only
D
Function body content
34

Question 34

Overloads may be preferred over unions when:

A
Different argument types demand different return types
B
All argument types lead to the same result
C
A function accepts rest parameters
D
A function performs asynchronous tasks
35

Question 35

Overall, function overloads help developers:

A
Model multiple call patterns with precise return types
B
Modify runtime behavior
C
Disable type inference
D
Replace all union types

QUIZZES IN TypeScript