Angular MCQs – TypeScript Basics

11.) Which TypeScript feature helps in defining a reusable structure for objects?

A) Functions
B) Interfaces
C) Arrays
D) Loops

Answer: Option B

Explanation: Interfaces define a reusable structure for objects, ensuring consistency in object properties.

12.) What is the purpose of TypeScript’s namespace?

A) To define functions
B) To handle exceptions
C) To declare variables
D) To create a module-like structure for code organization

Answer: Option D

Explanation: The namespace feature in TypeScript helps organize and encapsulate code.

13.) How do you specify a function’s return type in TypeScript?

A) function myFunc(): number { return 10; }
B) function myFunc { return number 10; }
C) function myFunc = (number) => { return 10; }
D) function myFunc -> number { return 10; }

Answer: Option A

Explanation: The return type is specified after the function parentheses using : type.

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

A) string
B) boolean
C) integer
D) number

Answer: Option C

Explanation: TypeScript does not have an integer type; number is used for both integers and floating-point numbers.

15.) How do you define an optional property in a TypeScript interface?

A) property: string;
B) property?: string;
C) property! : string;
D) property: string?;

Answer: Option B

Explanation: The ? symbol marks a property as optional in a TypeScript interface.

16.) How do you enable strict type checking in TypeScript?

A) By setting “strict”: true in tsconfig.json
B) By using the –strict flag when compiling
C) Both A and B
D) TypeScript does not support strict checking

Answer: Option C

Explanation: Strict type checking ensures better type safety in TypeScript.

17.) What is the output of console.log(10 / “2”) in TypeScript?

A) 5
B) “5”
C) NaN
D) Error

Answer: Option A

Explanation: In TypeScript, when you divide a number by a string that can be converted to a number, the string is automatically converted to a number, and the division is performed.

18.) Which module system does TypeScript use by default?

A) CommonJS
B) AMD
C) ES6 Modules
D) UMD

Answer: Option C

Explanation: TypeScript uses ES6 Modules by default but supports other module formats.

19.) What does the never type represent in TypeScript?

A) A variable that can have any value
B) A function that never returns
C) A function that always returns
D) A deprecated feature

Answer: Option B

Explanation: The never type in TypeScript represents a value that never occurs, typically used to indicate a function that never successfully completes or returns.

20.) How do you explicitly specify a return type of void in TypeScript?

A) function example(): void {}
B) function example() { return void; }
C) function example(): undefined {}
D) function example() -> void {}

Answer: Option A

Explanation: The void return type means the function does not return a value.

Leave a Reply

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