Which JavaScript expression uses the rest operator?
- function myFunction(a, b, ...rest)
- const [x, y, ...rest] = arr;
- const {x, y, ...rest} = obj;
- const rest = [a, b, ...c];
The rest operator (...) is used in function parameters to collect all remaining arguments into an array. In the example function myFunction(a, b, ...rest), the ...rest collects any additional arguments passed to the function into an array named rest.
How can you make the properties of an object immutable in JavaScript?
- Using Object.freeze()
- Using Object.preventExtensions()
- Using Object.seal()
- Using Object.makeImmutable()
To make the properties of an object immutable in JavaScript, you can use the Object.freeze() method. This method prevents any changes to the object's properties, making them read-only. The other options, Object.preventExtensions() and Object.seal(), allow some level of modification but not full immutability, and Object.makeImmutable() is not a valid method in JavaScript.
When creating a multidimensional array in JavaScript, each element of the main array is ______.
- an object
- a reference
- a function
- a prototype
When creating a multidimensional array in JavaScript, each element of the main array is a reference to another array or value. This allows for the creation of nested arrays. Changes made to the referenced arrays will affect the main array.
How can you execute a block of code multiple times, as long as a specified condition is true, in JavaScript?
- for loop
- while loop
- if statement
- switch statement
In JavaScript, you can use a while loop to execute a block of code repeatedly as long as a specified condition is true. The for loop is used for iterating over a sequence, and if and switch statements are conditional constructs, not loops.
You are developing a Node.js application where you need to perform a specific action immediately after the current operation completes. How would you use the process object to schedule this action?
- process.scheduleImmediate(() => { /* Action code */ });
- process.nextTick(() => { /* Action code */ });
- process.setImmediate(() => { /* Action code */ });
- process.waitForNext(() => { /* Action code */ });
To schedule a specific action immediately after the current operation completes, you should use process.setImmediate(() => { /* Action code */ });. This ensures that the action is placed in the event queue and executed as soon as possible after the current operation. The other options do not serve this purpose correctly.
What type of files are generally served as static files in Express.js?
- HTML files
- Dynamic server scripts
- Configuration files
- Images, CSS, JavaScript
In Express.js, static files typically include images, CSS files, and JavaScript files. These files do not change dynamically and can be served directly to clients. HTML files are often dynamically generated, and configuration files are not typically served as static files.
Which of the following is a commonly used configuration file for ESLint?
- .eslintrc.json
- .eslint-config
- .eslint-settings
- .lintfile
The commonly used configuration file for ESLint is .eslintrc.json. This file allows you to specify ESLint rules, plugins, and other configuration options for your project.
Which of the following HTTP methods is typically used for reading data in CRUD operations?
- GET
- POST
- PUT
- DELETE
The GET HTTP method is used for reading data in CRUD (Create, Read, Update, Delete) operations. It retrieves data from the server without making any modifications.
What considerations need to be made for evolving schemas in distributed databases to ensure data consistency?
- Distributed databases should never have evolving schemas
- Schema changes should be applied simultaneously to all nodes
- Schema changes should be applied sequentially with backward compatibility
- Schema changes should be applied only to a single node
When evolving schemas in distributed databases, it's important to apply changes sequentially with backward compatibility. This means making changes in a way that allows both old and new versions of the schema to coexist temporarily, ensuring data consistency during the transition. Applying changes simultaneously or only to a single node can lead to data inconsistencies.
You are building a RESTful API with Express.js that will be consumed by various client applications, including mobile and web. How would you design the authentication and authorization mechanisms to ensure maximum security and flexibility?
- Implement a single hard-coded API key for all clients
- Use JWT (JSON Web Tokens) for authentication and fine-grained middleware for authorization
- Allow anonymous access to all endpoints
- Rely solely on client-side security mechanisms
To ensure maximum security and flexibility in a RESTful API, you should use JWT for authentication, as it allows for stateless authentication and can be used across different client types. Fine-grained middleware can be used for authorization, controlling access to specific endpoints. The other options are insecure or impractical approaches.