TypeScript MCQ

Welcome to the TypeScript quiz for beginners! TypeScript has quickly become popular in modern web development because of its powerful type system on top of JavaScript. It’s time to test your knowledge of the basics of TypeScript! Here are 30+ multiple-choice questions to check your TypeScript foundational understanding.

Note that each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. What is TypeScript primarily used for?

A. Memory Management
B. Dynamic Typing
C. Static Typing
D. Asynchronous operations

Answer:

C. Static Typing

Explanation:

TypeScript is primarily used to add static typing to JavaScript, enabling developers to catch type-related errors during compile time.

2. Which of the following is NOT a valid TypeScript data type?

A. void
B. any
C. dynamic
D. tuple

Answer:

C. dynamic

Explanation:

TypeScript does not have a “dynamic” type. It has the “any” type which can represent any JavaScript value.

3. How do you denote a variable as readonly in TypeScript?

A. const
B. static
C. readonly
D. fixed

Answer:

C. readonly

Explanation:

The readonly keyword in TypeScript ensures that a property cannot be re-assigned after its initial assignment.

4. How do you specify that a function does not return anything in TypeScript?

A. function myFunc(): undefined
B. function myFunc(): void
C. function myFunc(): null
D. function myFunc(): None

Answer:

B. function myFunc(): void

Explanation:

In TypeScript, a function that doesn’t return anything should have its return type specified as void.

5. How do you define a custom type in TypeScript?

A. interface
B. typedef
C. type
D. Both A and C

Answer:

D. Both A and C

Explanation:

In TypeScript, custom types can be defined using both interface and type keywords.

6. What is the primary purpose of TypeScript interfaces?

A. To create new classes
B. To describe the shape of an object
C. To generate HTML templates
D. To manage asynchronous code

Answer:

B. To describe the shape of an object

Explanation:

Interfaces in TypeScript are primarily used to describe the shape or structure of an object. They ensure that objects have the correct properties and methods as described by the interface.

7. What is a union type in TypeScript?

A. A type that can be any value
B. A type that can be one of several types
C. A type that can be both a string and a number simultaneously
D. A type that can be an object

Answer:

B. A type that can be one of several types

Explanation:

A union type describes a value that could be one of several different types.

8. Which TypeScript feature allows for checking the type of a variable at runtime?

A. Type guard
B. Runtime type
C. Dynamic type
D. Typeof

Answer:

A. Type guard

Explanation:

Type guards allow you to narrow down the type of an object within a conditional block.

9. What TypeScript compiler option ensures strict type checking?

A. –strict
B. –strictTypes
C. –typeCheck
D. –enforceTypes

Answer:

A. –strict

Explanation:

The –strict compiler option enables a wide range of type-checking behavior to ensure that type definitions are accurate.

10. How do you define an optional parameter in the TypeScript function?

A. function foo(param: string?)
B. function foo(param?: string)
C. function foo(param string=)
D. function foo(param string?)

Answer:

B. function foo(param?: string)

Explanation:

In TypeScript, you can define an optional parameter by appending a ‘?’ to the parameter name.

11. Which of the following will transpile a TypeScript file (example.ts) to JavaScript?

A. typescript example.ts
B. ts-compile example.ts
C. tsc example.ts
D. ts example.ts

Answer:

C. tsc example.ts

Explanation:

The tsc (TypeScript Compiler) command is used to transpile TypeScript files to JavaScript.

12. How do you declare a variable that can be either a string or null in TypeScript?

A. let variable: string || null;
B. let variable: string | null;
C. let variable: string & null;
D. let variable: string && null;

Answer:

B. let variable: string | null;

Explanation:

The union type (using ‘|’) allows a variable to have one of multiple types.

13. What is the purpose of the never type in TypeScript?

A. To indicate that a variable can be any type.
B. To represent the absence of values.
C. To indicate a function always throws an exception or never returns.
D. To represent the absence of a type.

Answer:

C. To indicate a function always throws an exception or never returns.

Explanation:

The never type represents the type of values that never occur. For instance, a function that always throws an exception or one that never returns.

14. How can you allow an object to have any number of properties of a given type in TypeScript?

A. { [key: any]: string; }
B. { [key: string]: any; }
C. { [property: string]: string; }
D. { [value: string]: string; }

Answer:

B. { [key: string]: any; }

Explanation:

This syntax denotes an object that can have any number of properties, where the keys are strings and the values can be of any type.

15. Which command would you use to install TypeScript globally using npm?

A. npm install typescript
B. npm global install typescript
C. npm install -g typescript
D. npm typescript install global

Answer:

C. npm install -g typescript

Explanation:

The -g flag in npm is used to install packages globally.

16. How do you define private property in a TypeScript class?

A. def property: string;
B. private property: string;
C. #property: string;
D. property: private string;

Answer:

B. private property: string;

Explanation:

In TypeScript, the private keyword is used to define private properties in a class.

17. Which of the following TypeScript types can the unknown type be assigned to without type assertion?

A. string
B. number
C. any
D. void

Answer:

C. any

Explanation:

The unknown type is a safe counterpart of any type. It can be assigned to any type without a type assertion.

18. In TypeScript, what does an enum allow you to do?

A. Store a list of numeric values.
B. Store a set of named constants, numeric or string.
C. Define a new data type.
D. Assign multiple types to a variable.

Answer:

B. Store a set of named constants, numeric or string.

Explanation:

Enums in TypeScript allow for a way of giving more friendly names to sets of numeric or string values.

19. Which TypeScript feature allows for declaring new names for existing types?

A. Aliases
B. Enums
C. Interfaces
D. Decorators

Answer:

A. Aliases

Explanation:

Type aliases allow for creating new names (aliases) for types. It’s a way to give a type a new name.

20. How do you specify a function type in TypeScript that takes in a number and returns a string?

A. function(num: number) -> string
B. function: (number) => string
C. (num: number) => string
D. Function(number): string

Answer:

C. (num: number) => string

Explanation:

In TypeScript, function types can be described using the parameter type and return type syntax, such as (parameterType) => returnType.

21. What does the extends keyword allow you to do in TypeScript?

A. Add methods to an existing function.
B. Increase the value of a number variable.
C. Create a subclass from a superclass.
D. Extend the length of an array.

Answer:

C. Create a subclass from a superclass.

Explanation:

In TypeScript, as in many object-oriented languages, the extends keyword is used for class declarations or class expressions to create a class as a child of another class.

22. Which TypeScript keyword allows for a child class to override a method of its parent class?

A. override
B. super
C. over
D. extends

Answer:

B. super

Explanation:

The super keyword in TypeScript is used to call functions on an object’s parent. It is often used within an overridden method to refer to the version of the function in the parent class.

23. How do you define an array of strings in TypeScript?

A. Array<string>
B. string[]
C. Both A and B
D. List<string>

Answer:

C. Both A and B

Explanation:

In TypeScript, both Array<string> and string[] are valid ways to define an array of strings.

24. In TypeScript, how do you enforce a variable to be of a specific type at compile time?

A. Using the force keyword.
B. Using the type keyword.
C. By using type annotations.
D. By casting the variable.

Answer:

C. By using type annotations.

Explanation:

Type annotations in TypeScript allow developers to enforce a variable to be of a specific type at compile time.

25. Which TypeScript feature provides static typings for dynamic properties in objects and arrays?

A. Generics
B. Type guards
C. Dynamic types
D. Index signatures

Answer:

D. Index signatures

Explanation:

Index signatures in TypeScript allow us to describe types of properties that might not be known until runtime.

26. In TypeScript, how can a subclass access a method from its superclass?

A. Using the extends keyword
B. Using the inherits keyword
C. Using the super keyword
D. Using the base keyword

Answer:

C. Using the super keyword

Explanation:

The super keyword in TypeScript is used to call functions on an object’s parent. When overriding a method in a derived class, the super keyword can be used to call the method on the parent class.

27. How do you declare a class in TypeScript?

A. def ClassName:
B. class ClassName {}
C. new Class ClassName {}
D. object ClassName {}

Answer:

B. class ClassName {}

Explanation:

In TypeScript, you use the class keyword followed by the class name to define a new class, similar to ES6 in JavaScript.

28. How do you create an instance of a TypeScript class?

A. new MyClass()
B. MyClass.new()
C. MyClass.create()
D. instance MyClass()

Answer:

A. new MyClass()

Explanation:

You create a new instance of a TypeScript class using the new keyword followed by the class name and parentheses.

29. What does the extends keyword do in TypeScript?

A. It imports a module.
B. It creates an alias for a type.
C. It allows a class to inherit from another class.
D. It allows extending an array.

Answer:

C. It allows a class to inherit from another class.

Explanation:

The extends keyword is used for class inheritance in TypeScript. It allows a class to inherit methods and properties from another class.

30. What is the purpose of a constructor in TypeScript classes?

A. To create a static method.
B. To initialize object properties.
C. To destroy an object.
D. To run asynchronous code.

Answer:

B. To initialize object properties.

Explanation:

The constructor in TypeScript is a special function that is automatically called when an object is created from a class. It is primarily used to initialize object properties.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top