multiple-choice questions based on TypeScript
1. What is TypeScript?
a) A superset of JavaScript
b) A completely different programming language
c) A framework for building web applications
d) A database management system
Answer: a) A superset of JavaScript
2. Which of the following is true about TypeScript?
a) It gets compiled to Python
b) It is interpreted at runtime
c) It adds static typing to JavaScript
d) It only works in the browser
Answer: c) It adds static typing to JavaScript
3. How do you declare a variable with a specific type in TypeScript?
a) let x: number = 5;
b) let x = 5 as number;
c) let x = number(5);
d) let x = 5;
Answer: a) let x: number = 5;
4. What is the TypeScript file extension?
a) .ts
b) .js
c) .txt
d) .tsx
Answer: a) .ts
5. Which of the following is a correct syntax for defining a function type in TypeScript?
a) type MyFunc = (x: number, y: number) => number;
b) let MyFunc: function(x: number, y: number) => number;
c) interface MyFunc(x: number, y: number): number;
d) function MyFunc(x: number, y: number) => number;
Answer: a) type MyFunc = (x: number, y: number) => number;
6. Which keyword is used to declare a class in TypeScript?
a) class
b) Class
c) type
d) interface
Answer: a) class
7. What is the purpose of the `any` type in TypeScript?
a) It represents a variable of any type
b) It specifies that a variable should not be assigned a value
c) It is used for defining arrays
d) It is a reserved keyword without any functionality
Answer: a) It represents a variable of any type
8. What does the `readonly` modifier do in TypeScript?
a) It indicates that a property cannot be modified after initialization
b) It prevents a class from being extended
c) It makes a property private
d) It marks a property as optional
Answer: a) It indicates that a property cannot be modified after initialization
9. How do you define an optional parameter in TypeScript?
a) function myFunc(x?: number) {}
b) function myFunc(x: number = undefined) {}
c) function myFunc(x: number!) {}
d) function myFunc(x!: number) {}
Answer: a) function myFunc(x?: number) {}
10. What does the `extends` keyword do in TypeScript?
a) It implements an interface
b) It defines a generic type
c) It merges multiple types
d) It creates a subclass relationship between classes
Answer: d) It creates a subclass relationship between classes
11. Which of the following is true about TypeScript enums?
a) They cannot have string values
b) They cannot be used in switch statements
c) They are converted to JavaScript objects during compilation
d) They must have numeric values
Answer: c) They are converted to JavaScript objects during compilation
12. What is the purpose of the `unknown` type in TypeScript?
a) It represents a variable of unknown type
b) It specifies that a variable is uninitialized
c) It defines a type that can be either string or number
d) It is a reserved keyword without any functionality
Answer: a) It represents a variable of unknown type
13. How do you import a named export in TypeScript?
a) import MyModule from './myModule';
b) import { MyModule } from './myModule';
c) require('./myModule');
d) include('./myModule');
Answer: b) import { MyModule } from './myModule';
14. What is a generic type in TypeScript?
a) A type that can represent any data type
b) A type that is specific to a particular data type
c) A type that can only be used with classes
d) A type that cannot be instantiated
Answer: b) A type that is specific to a particular data type
15. What is the purpose of the `never` type in TypeScript?
a) It represents a value that never occurs
b) It is used to declare a variable that is never assigned a value
c) It is a reserved keyword without any functionality
d) It is used to define a variable that can have any type
Answer: a) It represents a value that never occurs
16. How do you define a tuple type in TypeScript?
a) type MyTuple = (string, number);
b) type MyTuple = [string, number];
c) interface MyTuple = [string, number];
d) let MyTuple = [string, number];
Answer: b) type MyTuple = [string, number];
17. Which of the following is true about TypeScript interfaces?
a) They can be instantiated
b) They can extend classes
c) They can contain implementation details
d) They cannot define properties
Answer: c) They can contain implementation details
18. What is the purpose of the `as` keyword in TypeScript?
a) It is used for type assertion
b) It is used to define a variable
c) It is used for looping constructs
d) It is used for importing modules
Answer: a) It is used for type assertion
19. How do you define an array type in TypeScript?
a) type MyArray = array;
b) type MyArray = Array;
c) type MyArray = any[];
d) type MyArray = Array<any>;
Answer: d) type MyArray = Array<any>;
20. What is the purpose of the `export` keyword in TypeScript?
a) It is used to declare a variable
b) It is used to export a module or a member from a module
c) It is used to define a class
d) It is used for type assertion
Answer: b) It is used to export a module or a member from a module
21. What does the `!` operator do in TypeScript?
a) It performs a logical NOT operation
b) It is used for optional chaining
c) It asserts that an expression is not null or undefined
d) It is used for type assertion
Answer: c) It asserts that an expression is not null or undefined
22. Which of the following is true about TypeScript decorators?
a) They are used for commenting code
b) They are a feature of JavaScript
c) They are used for adding metadata to classes, methods, and properties
d) They are only applicable to interfaces
Answer: c) They are used for adding metadata to classes, methods, and properties
23. What is the purpose of the `readonly` modifier in an interface?
a) It indicates that a property cannot be modified after initialization
b) It prevents a class from being extended
c) It marks a property as optional
d) It specifies that a property is required
Answer: a) It indicates that a property cannot be modified after initialization
24. How do you declare an array in TypeScript with a specific type?
a) let arr: array<number>;
b) let arr: Array<number>;
c) let arr: number[];
d) let arr: NumberArray<number>;
Answer: c) let arr: number[];
25. What is the purpose of the `private` modifier in TypeScript?
a) It specifies that a property is required
b) It makes a property private to the class
c) It marks a property as optional
d) It indicates that a property cannot be modified after initialization
Answer: b) It makes a property private to the class
26. Which of the following is true about TypeScript namespaces?
a) They are used for defining classes
b) They are a feature of JavaScript
c) They are used for organizing code into logical groups
d) They are replaced by modules in TypeScript
Answer: d) They are replaced by modules in TypeScript
27. How do you declare a union type in TypeScript?
a) type MyUnion = number | string;
b) type MyUnion = number & string;
c) type MyUnion = number, string;
d) type MyUnion = number + string;
Answer: a) type MyUnion = number | string;
28. What is the purpose of the `readonly` modifier in a class property?
a) It indicates that a property cannot be modified after initialization
b) It prevents a class from being extended
c) It marks a property as optional
d) It specifies that a property is required
Answer: a) It indicates that a property cannot be modified after initialization
29. How do you define a type alias in TypeScript?
a) interface MyType = {};
b) type MyType = {};
c) class MyType = {};
d) let MyType = {};
Answer: b) type MyType = {};
30. Which of the following is true about TypeScript type assertion?
a) It is used for defining new types
b) It is used for type conversion when TypeScript can't infer the type
c) It is only applicable to primitive types
d) It is used for checking the type of a variable at runtime
Answer: b) It is used for type conversion when TypeScript can't infer the type
-Code With VDK.