CARVIEW |
Modules and Namespaces in TypeScript Quiz
This quiz explores Modules and Namespaces in TypeScript, including topics such as TypeScript modules, default and named exports, and legacy namespaces. It provides insights into how to organize and structure TypeScript code for better maintainability and scalability.
Question 1
What is the primary purpose of modules in TypeScript?
To create a single global scope for all code.
To split TypeScript files into smaller pieces without affecting scope.
To organize code into reusable and isolated units.
To replace classes and interfaces entirely.
Question 2
Which keyword is used to define a module in TypeScript?
module
namespace
export
Modules do not require a specific keyword.
Question 3
Which of the following is a correct way to use a default export?
// In file: user.ts
export default class User {
name: string;
constructor(name: string) {
this.name = name;
}
}
// In another file
import User from "./user";
const user = new User("Pranjal");
Default exports must always use the import * syntax
Default exports require curly braces during import
This is the correct way to define and import a default export.
Default exports do not support classes.
Question 4
How do named exports differ from default exports?
Named exports can export only functions, while default exports can export any entity
Named exports must be imported with the exact names, whereas default exports can be renamed
Named exports are used for legacy namespaces, while default exports are for modern modules
Named exports cannot coexist with default exports in the same module.
Question 5
What is the primary difference between namespaces and modules in TypeScript?
Namespaces are used to group related variables and functions within a single file, while modules organize code across files.
Modules are a feature of JavaScript, while namespaces are exclusive to TypeScript.
Namespaces enforce strict type checking, whereas modules do not.
Modules are legacy features, while namespaces are modern alternatives.
Question 6
Which of the following syntax defines a namespace?
namespace Utilities {
export function log(message: string): void {
console.log(message);
}
}
The namespace keyword creates a legacy namespace.
namespace is used for defining modern modules.
This is invalid TypeScript syntax.
The log function is only accessible globally.
Question 7
How do you use a function from a namespace in TypeScript?
namespace MathUtilities {
export function square(num: number): number {
return num * num;
}
}
// Usage
const result = MathUtilities.square(5);
Functions in namespaces cannot be accessed outside the file.
Functions in namespaces must be explicitly exported to be accessed.
Functions in namespaces are automatically added to the global scope
Functions in namespaces can only be accessed using import.
Question 8
Why are namespaces considered legacy in TypeScript?
They do not support ES6 syntax.
Modules have largely replaced namespaces for organizing code.
Namespaces cannot handle asynchronous code.
TypeScript no longer supports namespaces.
Question 9
Can a module and a namespace coexist in TypeScript?
No, they are mutually exclusive.
Yes, but namespaces must be imported explicitly.
Yes, namespaces can exist within a module.
No, TypeScript only supports modules.
Question 10
Which of the following demonstrates the combination of modules and namespaces?
export namespace Shapes {
export class Circle {
constructor(public radius: number) {}
}
}
import { Shapes } from "./shapes";
const circle = new Shapes.Circle(10);
Modules and namespaces cannot be combined.
This code demonstrates how namespaces can be exported as part of a module.
The Shapes namespace must be defined in a separate module.
Namespaces cannot be used in ES6 module syntax.
There are 10 questions to complete.