Given the code below:
Setcurrent URL ();
console.log(‘The current URL is: ‘ +url );
function setCurrentUrl() {
Url = window.location.href:
What happens when the code executes?
A. The url variable has local scope and line 02 throws an error.
B. The url variable has global scope and line 02 executes correctly.
C. The url variable has global scope and line 02 throws an error.
D. The url variable has local scope and line 02 executes correctly.
Explanation:
url is implicitly declared without let/const/var, making it a global variable.
Line 2 accesses the global url after setCurrentUrl() assigns window.location.href to it.
A developer is required to write a function that calculates the sum of elements in an array but is getting undefined every time the code is executed. The developer needs to find what is missing in the code below.
Const sumFunction = arr => {
Return arr.reduce((result, current) => {
//
Result += current;
//
), 10);
);
Which option makes the code work as expected?
A. Replace line 02 with return arr.map(( result, current) => (
B. Replace line 04 with result = result +current;
C. Replace line 03 with if(arr.length == 0 ) ( return 0; )
D. Replace line 05 with return result;
Explanation:
The callback passed to reduce must return the updated accumulator on each iteration. In the original code, you increment result but never return it, so the next iteration sees undefined. By adding return result; at line 05, each accumulated value is correctly passed forward, and the final sum is computed as expected.
Developer creates a new web server that uses Node.js. It imports a server library that uses events and callbacks for handling server functionality. The server library is imported with require and is made available to the code by a variable named server. The developer wants to log any issues that the server has while booting up. Given the code and the information the developer has, which code logs an error at boost with an event?
A. Server.catch ((server) => {
console.log(‘ERROR’, error);
});
B. Server.error ((server) => {
console.log(‘ERROR’, error);
});
C. Server.on (‘error’, (error) => {
console.log(‘ERROR’, error);
});
D. Try{
server.start();
} catch(error) {
console.log(‘ERROR’, error);
Explanation:
Node.js event emitters expose an error event that you listen for via server.on('error', handler). This callback is invoked whenever the server library emits an error during startup or operation. Neither .catch nor .error are valid event-registration methods on an EventEmitter, and a simple try/catch won’t catch asynchronous errors emitted later.
Given the following code:
Let x =(‘15’ + 10)*2;
What is the value of a?
A. 3020
B. 1520
C. 50
D. 35
Explanation:
In JavaScript, adding a number to a string concatenates: '15' + 10 yields '1510'. Multiplying that string by 2 coerces it to the number 1510, resulting in 3020. Thus the expression evaluates to 3020.
A developer creates an object where its properties should be immutable and prevent properties from being added or modified. Which method should be used to execute this business requirement?
A. Object.const()
B. Object.eval()
C. Object.lock()
D. Object.freeze()
Explanation:
Object.freeze(obj) makes an object immutable: it prevents adding, deleting, or modifying properties on that object. Neither Object.const, Object.lock, nor Object.eval exist in JavaScript.
Given the following code:
document.body.addEventListener(‘ click ’, (event) => {
if (/* CODE REPLACEMENT HERE */) {
console.log(‘button clicked!’);
)
});
Which replacement for the conditional statement on line 02 allows a developer to correctly determine that a button on page is clicked?
A. Event.clicked
B. e.nodeTarget ==this
C. event.target.nodeName == ‘BUTTON’
D. button.addEventListener(‘click’)
Explanation:
Within a delegated click handler on document.body, event.target.nodeName === 'BUTTON' reliably detects when the actual element clicked is a <button>. event.target refers to the deepest element clicked, and nodeName returns its tag name in uppercase.
Refer to the expression below:
Let x = (‘1’ + 2) == (6 * 2);
How should this expression be modified to ensure that evaluates to false?
A. Let x = (‘1’ + ‘ 2’) == ( 6 * 2);
B. Let x = (‘1’ + 2) == ( 6 * 2);
C. Let x = (1 + 2) == ( ‘6’ / 2);
D. Let x = (1 + 2 ) == ( 6 / 2);
Explanation:
By concatenating with a space—('1' + ' 2') yields '1 2', which cannot convert to a valid number, so the comparison '1 2' == 12 becomes NaN == 12, which is false. All other options still coerce to matching numeric values and evaluate to true.
A developer wants to set up a secure web server with Node.js. The developer creates a directory locally called app-server, and the first file is app-server/index.js Without using any third-party libraries, what should the developer add to index.js to create the secure web server?
A. const https =require(‘https’);
B. const server =require(‘secure-server’);
C. const tls = require(‘tls’);
D. const http =require(‘http’);
Explanation:
To spin up an HTTPS server in Node.js without third-party modules, you must use the built-in https package:
const https = require('https');
// …load TLS key/cert…
https.createServer(options, handler).listen(443);
Neither http, tls, nor a fictional secure-server module provides the full HTTPS interface by itself.
A test has a dependency on database. query. During the test, the dependency is replaced with an object called database with the method, Calculator query, that returns an array. The developer does not need to verify how many times the method has been called. Which two test approaches describe the requirement?
(Choose 2 answers)
A. White box
B. Stubbing
C. Black box
D. Substitution
Explanation:
Replacing the database.query dependency with a stand-in object that returns a fixed array without inspecting its internal behavior or verifying invocation counts is characteristic of stubbing and black-box testing. Stubbing provides a lightweight test double that returns predetermined data. Black-box testing focuses on inputs and outputs, ignoring the internal implementation and call frequency. White-box testing would require awareness of internal logic and verifying interactions, which is not needed here. Substitution is not a formal testing term in mainstream methodologies. Therefore, the appropriate approaches are stubbing to replace dependencies and black-box testing to validate behavior purely by the returned data.
A Developer wrote the following code to test a sum3 function that takes in an array of numbers and returns the sum of the first three number in the array, The test passes:
Let res = sum2([1, 2, 3 ]) ;
console.assert(res === 6 );
Res = sum3([ 1, 2, 3, 4]);
console.assert(res=== 6);
A different developer made changes to the behavior of sum3 to instead sum all of the numbers present in the array. The test passes:
Which two results occur when running the test on the updated sum3 function?
(Choose 2 answers)
A. The line 02 assertion passes.
B. The line 02 assertion fails
C. The line 05 assertion passes.
D. The line 05 assertion fails.
Explanation:
With the updated sum3 that sums all elements, calling sum3([1,2,3]) still returns 6, so the first console.assert(res === 6) passes. However, sum3([1,2,3,4]) now returns 10, not 6, so the second assertion fails. Thus, line 02 succeeds (A), and line 05 fails (D). This highlights the importance of writing tests that capture intended behavior changes: when functionality broadens from summing only the first three items to summing the entire array, existing tests may no longer validate correct behavior for all inputs.
Refer to the code below:
01 const exec = (item, delay) =>{
02 new Promise(resolve => setTimeout( () => resolve(item), delay)),
03 async function runParallel() {
04 Const (result1, result2, result3) = await Promise.all{
05 [exec (‘x’, ‘100’) , exec(‘y’, 500), exec(‘z’, ‘100’)]
06 );
07 return `parallel is done: $(result1) $(result2)$(result3)`;
08 }
}
}
Which two statements correctly execute the runParallel () function?
(Choose 2 answers)
A. Async runParallel () .then(data);
B. runParallel ( ). done(function(data){
return data;
});
C. runParallel () .then(data);
D. runParallel () .then(function(data)
return data
Explanation:
Because runParallel() is an async function, it returns a Promise. You consume its result via the Promise API’s .then(...) handler. Options C and D correctly call runParallel() and attach a .then callback to process its resolved value. Option A incorrectly prefixes with Async, which isn’t valid syntax for invocation. Option B uses .done(...), a non-standard Promise method (found in some libraries but not native JavaScript). Always use .then(...) or await to handle async function results in modern JavaScript.
A developer has the following array of student test grades:
Let arr = [ 7, 8, 5, 8, 9 ];
The Teacher wants to double each score and then see an array of the students who scored more than 15 points. How should the developer implement the request?
A. Let arr1 = arr.filter(( val) => ( return val > 15 )) .map (( num) => ( return num *2 ))
B. Let arr1 = arr.mapBy (( num) => ( return num *2 )) .filterBy (( val ) => return val > 15 )) ;
C. Let arr1 = arr.map((num) => num*2). Filter (( val) => val > 15);
D. Let arr1 = arr.map((num) => ( num *2)).filterBy((val) => ( val >15 ));
Explanation:
You first double each grade using Array.prototype.map(), producing [14,16,10,16,18]. Then you filter scores above 15 with Array.prototype.filter(), yielding [16,16,18]. This chaining ensures the mapping happens before filtering. Option A reverses the order (filter then map), B and D use non-standard methods (mapBy/filterBy), which don’t exist in vanilla JavaScript. Option C correctly leverages built-in map and filter methods in the proper sequence to meet the teacher’s requirements.
Page 2 out of 19 Pages |
Previous |