MCQs on Advanced Module Resolution | TypeScript

Mastering advanced module resolution in TypeScript is essential for optimizing your application structure. This includes dynamic imports, lazy loading, path aliases, and managing complex imports and exports to boost development efficiency.


Advanced Module Resolution in TypeScript – 30 Multiple Choice Questions

1. Dynamic Imports and Lazy Loading

  1. What is a dynamic import in TypeScript?
    • A) A way to import a module dynamically at runtime
    • B) A module imported at compile-time
    • C) A static import that does not change
    • D) A function used to execute a module
  2. Which syntax is used for dynamic imports in TypeScript?
    • A) import(moduleName)
    • B) require(moduleName)
    • C) import('moduleName')
    • D) async import('moduleName')
  3. What is the main benefit of using dynamic imports in an application?
    • A) To load modules in parallel at runtime
    • B) To enable lazy loading of modules, reducing initial loading time
    • C) To execute code before modules are loaded
    • D) To handle errors during module imports
  4. How does lazy loading improve application performance?
    • A) By preloading all modules at startup
    • B) By loading modules only when needed, reducing the initial load time
    • C) By increasing the size of the application
    • D) By avoiding the use of external libraries
  5. Which of the following is an example of lazy loading in TypeScript?
    • A) const module = import('./module');
    • B) import('./module').then(module => { ... });
    • C) require('./module')
    • D) import('./module').catch(error => { ... });
  6. Can dynamic imports be used with asynchronous code in TypeScript?
    • A) Yes, dynamic imports are asynchronous by nature
    • B) No, dynamic imports are synchronous
    • C) Only for specific types of imports
    • D) No, dynamic imports work only in JavaScript
  7. How can you ensure that a dynamically imported module is only loaded once?
    • A) By using a cache mechanism
    • B) By importing it in the main entry file
    • C) By using a singleton pattern
    • D) By making the module a static import
  8. What is the effect of dynamic imports on code splitting in TypeScript?
    • A) It disables code splitting completely
    • B) It enables code splitting, breaking up code into smaller chunks
    • C) It combines all modules into one file
    • D) It reduces the size of the final build
  9. What type of module can be dynamically imported in TypeScript?
    • A) Only ECMAScript modules
    • B) Only CommonJS modules
    • C) Both ECMAScript and CommonJS modules
    • D) Only modules with static imports
  10. Which TypeScript feature works with dynamic imports to manage large applications?
  • A) Module resolution
  • B) Code splitting
  • C) Type declarations
  • D) Static imports

2. Path Aliases in tsconfig.json

  1. What is the purpose of path aliases in TypeScript’s tsconfig.json?
  • A) To create shortcuts for module imports, making the code cleaner and more maintainable
  • B) To define the physical location of modules in the file system
  • C) To enable module exports for third-party libraries
  • D) To define module names at runtime
  1. How do you configure path aliases in tsconfig.json?
  • A) By using the paths property inside the compilerOptions object
  • B) By using import-alias in the module imports
  • C) By manually specifying paths in each import statement
  • D) By using a pathMappings property
  1. Which of the following is a valid example of configuring path aliases in tsconfig.json?
  • A) "paths": { "@utils/*": ["src/utils/*"] }
  • B) "paths": { "utils": "src/utils" }
  • C) "paths": { "src/utils": "@utils/*" }
  • D) "pathAliases": { "@utils": "src/utils" }
  1. How does using path aliases benefit a TypeScript project?
  • A) It prevents circular dependencies
  • B) It allows for cleaner, more concise imports
  • C) It improves the build time
  • D) It automatically resolves module conflicts
  1. Can path aliases be used in combination with dynamic imports?
  • A) Yes, path aliases can be used with dynamic imports
  • B) No, path aliases are only for static imports
  • C) Path aliases are not supported in TypeScript
  • D) Path aliases are only used for file system locations
  1. How do path aliases affect the import statements in a TypeScript file?
  • A) They allow you to use shorthand for module paths
  • B) They make imports case-sensitive
  • C) They enforce stricter typing in the imports
  • D) They enable runtime module resolution
  1. What happens if a path alias is incorrectly configured in tsconfig.json?
  • A) TypeScript will throw a compile-time error indicating an unknown module
  • B) TypeScript will ignore the alias and proceed with regular imports
  • C) The project will fail to run at runtime
  • D) TypeScript will automatically resolve the path
  1. Can path aliases be used across multiple TypeScript projects?
  • A) Yes, by configuring a common tsconfig.json in a shared directory
  • B) No, each project needs its own configuration
  • C) Yes, but only for relative imports
  • D) No, path aliases only work for single projects
  1. What role does the baseUrl option play in configuring path aliases?
  • A) It sets the base directory for resolving non-relative module imports
  • B) It defines the external URLs for module resolution
  • C) It restricts module imports to specific directories
  • D) It enables global imports across all projects
  1. Can path aliases be used with third-party modules installed via npm?
  • A) Yes, but only if the module is included in the node_modules directory
  • B) No, path aliases only work with local files
  • C) Yes, by using the types property in tsconfig.json
  • D) No, path aliases are exclusive to custom code

3. Managing Complex Imports and Exports

  1. How do you manage complex imports and exports in TypeScript?
  • A) By using default and named imports/exports for better organization
  • B) By avoiding any form of export to simplify code
  • C) By using require instead of import/export
  • D) By importing all files as a single module
  1. What is the difference between a default export and a named export in TypeScript?
  • A) A default export exports a single value, while a named export exports multiple values
  • B) A default export can only export functions, while named exports can export any value
  • C) There is no difference between them
  • D) A default export is for use with classes only
  1. What is the syntax for importing a default export in TypeScript?
  • A) import myModule from 'module';
  • B) import { myModule } from 'module';
  • C) import 'module';
  • D) import * as myModule from 'module';
  1. What happens if you try to import a non-existent export in TypeScript?
  • A) TypeScript will throw a compile-time error
  • B) TypeScript will silently ignore the import
  • C) TypeScript will use a fallback value
  • D) TypeScript will create a new export automatically
  1. How do you export multiple values from a module in TypeScript?
  • A) export { value1, value2 };
  • B) export value1, value2;
  • C) module.exports = { value1, value2 };
  • D) export * from 'module';
  1. What does export * from 'module'; do in TypeScript?
  • A) Re-exports everything from another module
  • B) Exports only functions from the module
  • C) Exports only classes from the module
  • D) Exports everything except functions
  1. Can TypeScript handle both require and import for modules?
  • A) Yes, TypeScript supports both
  • B) No, TypeScript only supports import
  • C) No, TypeScript only supports require
  • D) Yes, but only for external dependencies
  1. What is the purpose of the export = syntax in TypeScript?
  • A) To export a module’s single value using CommonJS-style exports
  • B) To allow default exports in TypeScript
  • C) To export only types in TypeScript
  • D) To re-export all named exports
  1. How does the import * as syntax work in TypeScript?
  • A) It imports all exports from a module as a single object
  • B) It imports a single export from a module
  • C) It imports only types from a module
  • D) It imports everything, including external dependencies
  1. What is a circular dependency in TypeScript?
  • A) When two or more modules depend on each other, creating a loop
  • B) When a module imports itself
  • C) When a module contains multiple functions
  • D) When a module has more than one export

Answer Key

QnoAnswer
1A) A way to import a module dynamically at runtime
2C) import('moduleName')
3B) To enable lazy loading of modules, reducing initial loading time
4B) By loading modules only when needed, reducing the initial load time
5B) import('./module').then(module => { ... });
6A) Yes, dynamic imports are asynchronous by nature
7A) By using a cache mechanism
8B) It enables code splitting, breaking up code into smaller chunks
9C) Both ECMAScript and CommonJS modules
10B) Code splitting
11A) To create shortcuts for module imports, making the code cleaner
12A) By using the paths property inside the compilerOptions object
13A) "paths": { "@utils/*": ["src/utils/*"] }
14B) It allows for cleaner, more concise imports
15A) Yes, path aliases can be used with dynamic imports
16A) TypeScript will throw a compile-time error indicating an unknown module
17A) Yes, by configuring a common tsconfig.json in a shared directory
18A) Yes, by configuring path aliases for node_modules
19A) It sets the base directory for resolving non-relative module imports
20A) Yes, path aliases can be used with third-party modules
21A) By using default and named imports/exports for better organization
22A) A default export exports a single value, while a named export exports multiple values
23A) import myModule from 'module';
24A) TypeScript will throw a compile-time error
25A) export { value1, value2 };
26A) Re-exports everything from another module
27A) Yes, TypeScript supports both
28A) To export a module’s single value using CommonJS-style exports
29A) It imports all exports from a module as a single object
30A) When two or more modules depend on each other, creating a loop

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