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
2. Which of the following is NOT a valid TypeScript data type?
A. void
B. any
C. dynamic
D. tuple
3. How do you denote a variable as readonly in TypeScript?
A. const
B. static
C. readonly
D. fixed
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
5. How do you define a custom type in TypeScript?
A. interface
B. typedef
C. type
D. Both A and C
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
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
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
9. What TypeScript compiler option ensures strict type checking?
A. --strict
B. --strictTypes
C. --typeCheck
D. --enforceTypes
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?)
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
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;
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.
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; }
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
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;
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
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.
19. Which TypeScript feature allows for declaring new names for existing types?
A. Aliases
B. Enums
C. Interfaces
D. Decorators
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
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.
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
23. How do you define an array of strings in TypeScript?
A. Array<string>
B. string[]
C. Both A and B
D. List<string>
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.
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
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
27. How do you declare a class in TypeScript?
A. def ClassName:
B. class ClassName {}
C. new Class ClassName {}
D. object ClassName {}
28. How do you create an instance of a TypeScript class?
A. new MyClass()
B. MyClass.new()
C. MyClass.create()
D. instance MyClass()
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.
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.
Comments
Post a Comment
Leave Comment