MCQs on Modules and Namespaces | TypeScript

Master TypeScript’s modules and namespaces with these 30 multiple-choice questions. Dive into import/export syntax, default exports, aliases, and namespaces for effective, scalable code organization and development.


Topic 1: Import/Export Syntax (10 Questions)

  1. How do you import a specific member from a module in TypeScript?
    A) import { member } from 'module';
    B) import 'module' { member };
    C) require { member } from 'module';
    D) import { member } => 'module';
  2. Which syntax is correct for exporting a variable in TypeScript?
    A) export default variable;
    B) export variable;
    C) module.exports = variable;
    D) export variable from 'module';
  3. What is the correct syntax to export multiple members from a TypeScript module?
    A) export { member1, member2 };
    B) export [member1, member2];
    C) export module { member1, member2 };
    D) export {member1, member2} from 'module';
  4. How can you import an entire module in TypeScript?
    A) import * from 'module';
    B) import 'module';
    C) import module from 'module';
    D) import { * } from 'module';
  5. Which of the following is the correct way to import a module using a specific alias?
    A) import * as alias from 'module';
    B) import alias from 'module';
    C) import 'module' as alias;
    D) import alias { * } from 'module';
  6. How do you import a module’s default export in TypeScript?
    A) import { default } from 'module';
    B) import 'module' default;
    C) import default from 'module';
    D) import * from 'module';
  7. Can you import both default and named exports from the same module in TypeScript?
    A) Yes, using import default, { named1, named2 } from 'module';
    B) No, only one can be imported at a time
    C) Yes, using import { default, named1, named2 } from 'module';
    D) No, you need separate import statements
  8. How would you export a function in TypeScript?
    A) function myFunction() {}
    B) export function myFunction() {}
    C) export default function myFunction() {}
    D) Both B and C
  9. Which syntax is correct for importing a function from another module?
    A) import functionName from 'module';
    B) import { functionName } from 'module';
    C) import functionName() from 'module';
    D) import 'functionName' from 'module';
  10. What happens if a module is not found during import in TypeScript?
    A) An error is thrown
    B) The program continues running with a warning
    C) TypeScript ignores the missing module
    D) The module is created automatically

Topic 2: Default Exports (5 Questions)

  1. What is the purpose of a default export in TypeScript?
    A) To export multiple variables
    B) To allow a module to export a single entity
    C) To export a type definition
    D) To make a module private
  2. How would you export a class as the default export in TypeScript?
    A) export default class MyClass {}
    B) default export class MyClass {}
    C) export class MyClass {}
    D) module.exports = class MyClass {};
  3. Which of the following is the correct syntax for importing a default export?
    A) import default from 'module';
    B) import { default } from 'module';
    C) import * as default from 'module';
    D) import MyClass from 'module';
  4. Can a module have both default and named exports?
    A) Yes, a module can have both
    B) No, a module can only have a default export
    C) Yes, but it is not recommended
    D) No, a module cannot have any export
  5. How can you rename a default export during import?
    A) import { alias as default } from 'module';
    B) import alias from 'module';
    C) import default as alias from 'module';
    D) import { default as alias } from 'module';

Topic 3: Aliases for Imports (5 Questions)

  1. What is the syntax to import an alias for a named export in TypeScript?
    A) import { originalName as alias } from 'module';
    B) import { alias } from 'module';
    C) import alias from 'module';
    D) import 'module' as alias;
  2. How do you import multiple members and rename them using aliases?
    A) import { member1 as alias1, member2 as alias2 } from 'module';
    B) import { member1, member2 as alias2 } from 'module';
    C) import alias1 from 'member1'; import alias2 from 'member2';
    D) import { alias1, alias2 from 'module';
  3. What will happen if you import the same member multiple times with different aliases?
    A) Error, duplicates not allowed
    B) It will work fine, with both aliases available
    C) The last alias will overwrite the earlier one
    D) TypeScript will ignore the duplicate
  4. Which of the following is the correct way to rename a default import?
    A) import { default as alias } from 'module';
    B) import alias from 'module';
    C) import alias as default from 'module';
    D) import { alias } from 'module';
  5. Can you use an alias for a default export in TypeScript?
    A) Yes, by using the as keyword
    B) No, aliases only work for named exports
    C) Yes, but only for functions
    D) No, default exports cannot be renamed

Topic 4: Namespaces and Their Use Cases (10 Questions)

  1. What is the main purpose of a namespace in TypeScript?
    A) To group related code and avoid naming conflicts
    B) To export modules
    C) To encapsulate private methods
    D) To declare variables globally
  2. How do you declare a namespace in TypeScript?
    A) namespace MyNamespace {}
    B) module MyNamespace {}
    C) declare namespace MyNamespace {}
    D) type MyNamespace = {};
  3. How do you access a member of a namespace?
    A) namespace.member
    B) namespace->member
    C) namespace.member()
    D) namespace:member;
  4. Which of the following is a correct use of namespaces in TypeScript?
    A) Grouping related functions and types
    B) Creating multiple global variables
    C) Avoiding the use of modules
    D) Reducing the use of functions
  5. Can namespaces in TypeScript be merged?
    A) Yes, namespaces can be merged using the same name
    B) No, namespaces are independent of each other
    C) Yes, but only when declared in different files
    D) No, merging namespaces is not possible
  6. What is the advantage of using namespaces over modules in TypeScript?
    A) Easier to organize code in larger projects
    B) Can avoid the overhead of importing/exporting
    C) More flexibility with the file system
    D) No real difference
  7. How do you define a function within a namespace?
    A) namespace MyNamespace { function myFunction() {} }
    B) module MyNamespace { function myFunction() {} }
    C) namespace MyNamespace = { function myFunction() {} }
    D) function namespace.myFunction() {}
  8. Can you use both modules and namespaces in the same TypeScript file?
    A) Yes, but they must be used separately
    B) No, only one can be used per file
    C) Yes, they are compatible
    D) No, namespaces are deprecated in favor of modules
  9. How do you reference a class inside a namespace?
    A) namespace.ClassName
    B) namespace->ClassName
    C) namespace.ClassName()
    D) namespace.ClassName[]
  10. What happens if a namespace and a module have the same name?
    A) TypeScript will merge both into one
    B) TypeScript will give an error due to the conflict
    C) The module will override the namespace
    D) The namespace will override the module

Answer Key

QnoAnswer
1A) import { member } from 'module';
2A) export default variable;
3A) export { member1, member2 };
4B) import 'module';
5A) import * as alias from 'module';
6C) import default from 'module';
7A) Yes, using import default, { named1, named2 } from 'module';
8D) Both B and C
9B) import { functionName } from 'module';
10A) Error is thrown
11B) To allow a module to export a single entity
12A) export default class MyClass {}
13D) import MyClass from 'module';
14A) Yes, a module can have both
15B) import alias from 'module';
16A) import { originalName as alias } from 'module';
17A) import { member1 as alias1, member2 as alias2 } from 'module';
18B) It will work fine, with both aliases available
19B) import alias from 'module';
20B) No, aliases only work for named exports
21A) To group related code and avoid naming conflicts
22A) namespace MyNamespace {}
23A) namespace.member
24A) Grouping related functions and types
25A) Yes, namespaces can be merged using the same name
26B) Can avoid the overhead of importing/exporting
27A) namespace MyNamespace { function myFunction() {} }
28A) Yes, but they must be used separately
29A) namespace.ClassName
30B) TypeScript will give an error due to the conflict

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