You need to develop a function that takes an array of numbers and returns a new array containing only the unique numbers. What approach would you use to filter out the duplicates?
- Using a for loop and checking uniqueness manually
- Using Array.prototype.filter() and a custom filter function
- Using Array.from(new Set(array))
- Using Array.prototype.reduce() with a custom accumulator function
To filter out duplicate numbers in an array, you can use Array.from(new Set(array)). This creates a Set from the array (which only keeps unique values) and then converts it back to an array. It's a concise and efficient approach for this task.
The else if statement is used in JavaScript for ________.
- conditional execution
- error handling
- multiple comparisons
- branching based on multiple conditions
The else if statement in JavaScript is used for branching based on multiple conditions. It allows you to check additional conditions if the previous if condition is false.
You are tasked with creating tests for a complex system with multiple interacting components. How would you decide which components to mock or stub to achieve a balance between test isolation and reliability?
- Mock all components to ensure complete isolation
- Stub only the most complex components
- Mock components that are external or slow
- Stub components that are stable and well-tested
When testing a complex system, it's essential to strike a balance between test isolation and reliability. Mocking all components can lead to over-fragmented tests and make maintenance difficult. Stubbing only the most complex components may not ensure adequate coverage. To achieve this balance, you should mock components that are external or slow, as these can introduce variability and slow down tests. Stubbing components that are stable and well-tested can help reduce unnecessary complexity and speed up test execution.
What does the process object in Node.js primarily provide information about?
- Operating system processes
- Web browser processes
- File I/O processes
- Database processes
The process object in Node.js primarily provides information about operating system processes. It allows you to interact with and control the Node.js process, such as accessing command line arguments, environment variables, and exiting the process. It is not related to web browser, file I/O, or database processes.
When designing systems with Non-Blocking I/O, careful consideration must be given to avoid ________, where multiple asynchronous operations are competing for resources.
- Callback Hell
- Deadlock
- Blocking I/O
- Synchronous Execution
When designing systems with Non-Blocking I/O, careful consideration must be given to avoid "Callback Hell," also known as "Callback Pyramid" or "Callback Spaghetti." This occurs when multiple asynchronous operations are nested deeply, making code difficult to read and maintain.
How can you implement template inheritance in Pug?
- extend layout.pug
- include layout.pug
- inherit layout.pug
- template layout.pug
In Pug, template inheritance is implemented using the extend keyword followed by the name of the layout file. This allows child templates to inherit the structure and content of the specified layout file. The other options (include, inherit, and template) are not used for template inheritance in Pug.
How can you optimize the performance of a SQL query that reads a large amount of data?
- Use indexes on columns frequently queried.
- Avoid joins and subqueries.
- Increase the page size of the database.
- Fetch all data at once to minimize network latency.
To optimize the performance of a SQL query reading large data, you should use indexes on columns frequently queried. Indexes improve data retrieval speed. The other options may not necessarily lead to performance improvements and can even degrade performance.
You are developing a Node.js application that should gracefully shut down when it receives a termination signal. How would you accomplish this using the process object?
- process.on('terminate', () => { /* Graceful shutdown code */ });
- process.kill('SIGTERM', process.pid);
- process.exit(0);
- process.handleTermination(() => { /* Graceful shutdown code */ });
To gracefully shut down a Node.js application upon receiving a termination signal, you should use process.on('terminate', () => { /* Graceful shutdown code */ });. This registers an event handler for the 'terminate' event. The other options either do not handle termination signals gracefully or are incorrect syntax.
You are tasked with implementing a secure authentication system for a web application. What considerations should you make to ensure the security of user credentials and session information?
- Store passwords in plain text
- Use a secure hashing algorithm for password storage
- Use HTTP for transmitting sensitive data
- Keep session tokens in local storage
To ensure the security of user credentials and session information, you should use a secure hashing algorithm (like bcrypt) to store passwords, not store them in plain text. Additionally, sensitive data should not be transmitted over HTTP, and session tokens should be stored securely (not in local storage, which is vulnerable to cross-site scripting attacks).
What considerations should be made when deciding between using a mock and a stub in a test case?
- The complexity of the test scenario
- The need for recording method calls
- The desire to control method behavior
- The size of the test data
When deciding between using a mock and a stub in a test case, considerations should include the complexity of the test scenario, as mocks are generally more complex than stubs. Recording method calls is a characteristic of mocks, and controlling method behavior is a characteristic of stubs. The size of the test data is typically not directly related to choosing between mocks and stubs.