TypeScript Functions Quiz

This quiz tests your knowledge of functions in TypeScript, covering function types, call signatures, overloading, and type casting.

Last Updated :
Discuss
Comments

Question 1

Which of the following is the correct way to declare a function type in TypeScript?

  • function sum(x: number, y: number): number;

  • let sum: (x: number, y: number) => number;

  • let sum: (x: number, y: number) -> number;

  • function sum(x: number, y: number) -> number;

Question 2

What does the function keyword represent in TypeScript?

  • It defines a variable that can hold any value.

  • It creates a named function with parameters and return type.

  • It defines a class method.

  • It defines a constant value for a function expression.

Question 3

Which of the following is the correct signature for a function that accepts two parameters of type string and returns a boolean?

  • (x: string): boolean

  • (x: boolean): string

  • (x: number, y: number): boolean

  • (x: string, y: string): boolean

Question 4

What is function overloading in TypeScript?

  • Defining multiple functions with the same name but different implementations.

  • Defining one function that behaves differently depending on the number or type of arguments passed.

  • Allowing only one function signature for all functions.

  • Declaring a function with a fixed set of arguments.

Question 5

In function overloading, which of the following is true?

  • A function can have multiple signatures, but only one implementation.

  • A function must have at least two implementations.

  • A function cannot have more than one return type.

  • Function overloading is not supported in TypeScript.

Question 6

Which of the following correctly demonstrates function overloading in TypeScript?

  • Define multiple function signatures before a single implementation.

  • Define multiple functions with the same name and return type.

  • Use different function names for overloaded functions.

  • Define different return types for overloaded functions.

Question 7

What is the result of the following TypeScript code?

JavaScript
function add(x: number, y: number): number;
function add(x: string, y: string): string;
function add(x: any, y: any): any {
    return x + y;
}
console.log(add(5, 10));


  • 15

  • "510"

  • NaN

  • undefined

Question 8

Which of the following is the correct way to cast a string to a number in TypeScript?

  • let num: number = <string> "42";

  • let num: number = "42" as number;

  • let num: number = Number("42");

  • let num: number = String("42");

Question 9

How does type assertion work in TypeScript when casting?

  • It allows you to tell TypeScript to treat a variable as a more specific type.

  • It performs a runtime conversion of types.

  • It enforces the variable to always be of the asserted type.

  • It helps in creating custom type aliases.

Question 10

Consider the following code in TypeScript. What is the result when the function sum() is called with two string arguments?

JavaScript
function sum(a: number, b: number): number;
function sum(a: string, b: string): string;
function sum(a: any, b: any): any {
    return a + b;
}
console.log(sum("Hello", "World"));


  • HelloWorld

  • NaN

  • "Hello World"

  • undefined

There are 10 questions to complete.

Take a part in the ongoing discussion