Master TypeScript fundamentals with 30 MCQs covering Type Annotations, Primitive Types (string, number, boolean, null, undefined), any and unknown types, and Type Inference. Sharpen your TypeScript skills with these essential questions.
Chapter 2: TypeScript Basic Types
Type Annotations
What is the primary purpose of type annotations in TypeScript?
a) To improve code readability
b) To enforce data types at compile time
c) To speed up code execution
d) To reduce file size
How do you declare a variable with a type annotation in TypeScript?
a) let age: int = 25;
b) let age = 25: number;
c) let age: number = 25;
d) number age = 25;
Which of the following is a correct way to use type annotations for a string?
a) let name: String = "John";
b) let name: string = "John";
c) let name = string("John");
d) let name: Text = "John";
If you declare let count: number; without assigning a value, what will be the default value?
a) null
b) undefined
c) 0
d) NaN
What will happen if you assign a string to a variable declared with a number type annotation?
a) TypeScript will convert the string to a number
b) TypeScript will throw a compilation error
c) The value will be set to undefined
d) The assignment will succeed without any error
Primitive Types: String, Number, Boolean, Null, Undefined
Which of the following is not considered a primitive type in TypeScript?
a) string
b) number
c) object
d) boolean
What is the type of let isDone: boolean = true;?
a) Boolean
b) boolean
c) bit
d) flag
How do you explicitly declare a variable as null in TypeScript?
a) let value: null;
b) let value: void = null;
c) let value: undefined = null;
d) let value = null;
What is the default type of an uninitialized variable in TypeScript?
a) undefined
b) null
c) string
d) number
Which of the following TypeScript types is used to denote the absence of a value?
a) boolean
b) undefined
c) null
d) void
Can null and undefined be used as types in TypeScript?
a) Yes, but only in strict mode
b) No, they are reserved keywords
c) Yes, they can be used as types
d) Only undefined can be used as a type
Which primitive type would you use for representing floating-point numbers?
a) integer
b) float
c) number
d) decimal
Any and Unknown Types
What is the primary difference between any and unknown types in TypeScript?
a) unknown is more restrictive than any
b) any is more restrictive than unknown
c) Both types are identical
d) unknown can only be used for objects
Which type should be avoided for better type safety in TypeScript?
a) unknown
b) any
c) number
d) boolean
How do you declare a variable that can hold any type of value?
a) let value: any;
b) let value: unknown;
c) let value: dynamic;
d) let value: object;
Which type allows the compiler to ensure that a value is of a specific type before it’s used?
a) any
b) unknown
c) string
d) void
What happens when you try to perform operations on a variable of type unknown without type-checking?
a) It results in a runtime error
b) It is allowed, but produces unexpected results
c) TypeScript throws a compile-time error
d) It automatically casts to any
Which statement best describes the any type?
a) It enforces strict type checks
b) It disables all type checking for that variable
c) It only allows primitive values
d) It is used for defining complex objects
When would you use the unknown type over any?
a) When you need flexibility with no type checks
b) When you want to enforce type checks before using a value
c) When you are unsure of the data structure
d) When working with external libraries
How do you cast an unknown type to a specific type in TypeScript?
a) let value = <string>someValue;
b) let value: someValue as string;
c) let value = string(someValue);
d) let value: string = someValue;
Type Inference
What is type inference in TypeScript?
a) Manually assigning types to variables
b) Automatically determining the type of a variable based on its value
c) Forcing a variable to be a specific type
d) Defining types using annotations
What type will TypeScript infer for let num = 100;?
a) string
b) any
c) number
d) integer
When does TypeScript use type inference?
a) When explicit type annotations are missing
b) Only for primitive types
c) Only in functions
d) It never uses type inference
How do you explicitly declare a type while still allowing type inference?
a) let value: auto = 42;
b) let value: any = "text";
c) let value = 42;
d) let value: number | string = 42;
Which of the following cases would TypeScript infer the type as boolean?
a) let active = false;
b) let active: boolean = "true";
c) let active = "false";
d) let active: any = false;
If no type is specified, what type does TypeScript infer for function parameters?
a) any
b) unknown
c) undefined
d) void
What type does TypeScript infer for the result of the expression 1 + 2?
a) string
b) any
c) number
d) integer
How do you override TypeScript’s type inference for a variable?
a) By using as keyword
b) By reassigning the variable with a different type
c) By using a type assertion
d) By casting it using <> syntax
How would TypeScript infer the type of the following variable: let names = ["Alice", "Bob", "Charlie"];?
a) string[]
b) any[]
c) object[]
d) unknown[]
What happens if you declare let myVar; without initialization in TypeScript?
a) TypeScript infers it as any type
b) TypeScript infers it as undefined type
c) TypeScript throws an error
d) TypeScript infers it as null type
Answer Key
Qno Answer 1 b) To enforce data types at compile time 2 c) let age: number = 25; 3 b) let name: string = “John”; 4 b) undefined 5 b) TypeScript will throw a compilation error 6 c) object 7 b) boolean 8 a) let value: null; 9 a) undefined 10 d) void 11 c) Yes, they can be used as types 12 c) number 13 a) unknown is more restrictive than any 14 b) any 15 a) let value: any; 16 b) unknown 17 c) TypeScript throws a compile-time error 18 b) It disables all type checking for that variable 19 b) When you want to enforce type checks before using a value 20 a) let value = <string>someValue; 21 b) Automatically determining the type of a variable based on its value 22 c) number 23 a) When explicit type annotations are missing 24 d) let value: number | string = 42; 25 a) let active = false; 26 a) any 27 c) number 28 c) By using a type assertion 29 a) string[] 30 a) TypeScript infers it as any type
Post Views: 50