MCQs on Basic Types | TypeScript

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

  1. 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
  2. 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;
  3. 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";
  4. If you declare let count: number; without assigning a value, what will be the default value?
    • a) null
    • b) undefined
    • c) 0
    • d) NaN
  5. 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

  1. Which of the following is not considered a primitive type in TypeScript?
    • a) string
    • b) number
    • c) object
    • d) boolean
  2. What is the type of let isDone: boolean = true;?
    • a) Boolean
    • b) boolean
    • c) bit
    • d) flag
  3. 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;
  4. What is the default type of an uninitialized variable in TypeScript?
    • a) undefined
    • b) null
    • c) string
    • d) number
  5. Which of the following TypeScript types is used to denote the absence of a value?
    • a) boolean
    • b) undefined
    • c) null
    • d) void
  6. 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
  7. Which primitive type would you use for representing floating-point numbers?
    • a) integer
    • b) float
    • c) number
    • d) decimal

Any and Unknown Types

  1. 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
  2. Which type should be avoided for better type safety in TypeScript?
    • a) unknown
    • b) any
    • c) number
    • d) boolean
  3. 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;
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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

  1. 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
  2. What type will TypeScript infer for let num = 100;?
    • a) string
    • b) any
    • c) number
    • d) integer
  3. 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
  4. 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;
  5. 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;
  6. If no type is specified, what type does TypeScript infer for function parameters?
    • a) any
    • b) unknown
    • c) undefined
    • d) void
  7. What type does TypeScript infer for the result of the expression 1 + 2?
    • a) string
    • b) any
    • c) number
    • d) integer
  8. 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
  9. How would TypeScript infer the type of the following variable: let names = ["Alice", "Bob", "Charlie"];?
    • a) string[]
    • b) any[]
    • c) object[]
    • d) unknown[]
  10. 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

QnoAnswer
1b) To enforce data types at compile time
2c) let age: number = 25;
3b) let name: string = “John”;
4b) undefined
5b) TypeScript will throw a compilation error
6c) object
7b) boolean
8a) let value: null;
9a) undefined
10d) void
11c) Yes, they can be used as types
12c) number
13a) unknown is more restrictive than any
14b) any
15a) let value: any;
16b) unknown
17c) TypeScript throws a compile-time error
18b) It disables all type checking for that variable
19b) When you want to enforce type checks before using a value
20a) let value = <string>someValue;
21b) Automatically determining the type of a variable based on its value
22c) number
23a) When explicit type annotations are missing
24d) let value: number | string = 42;
25a) let active = false;
26a) any
27c) number
28c) By using a type assertion
29a) string[]
30a) TypeScript infers it as any type

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top