













































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














































Which two syntax examples correctly initialize a value to the variable strLang? A. let strLang = 'javascript'; B. const strLang = 'java' + 'script'; C. let strLang = javascript; D. str strLang = 'javascript'; ✔ANSWER: A. let strLang = 'javascript'; B. const strLang = 'java' + 'script'; Additional Info: Note that C doesn't have a value that has single quotes around the word 'javascript' and for D, 'str' is not a proper identifier Which statement sorts the following number array so it is in ascending order? const arr = [7, 3, 400, 10]; A. arr.sort(); B. arr.sort((a, b) => a - b); C. arr.sort((a, b) => a < b); D. arr.sort((a, b) => b - a); ✔ANSWER: B. arr.sort((a, b) => a - b); Additional Info: A. The sort() function sorts values as strings, which will sort this as [10, 3, 400, 7]. C. The compare function expects a positive, negative, or 0 to be returned. D. The compare function will actually reverse the order of the numbers. Which statement sorts the following string array so it is in descending order? const arr = ["Banana", "Orange", "Apple", "Mango"]; A. arr.sort(); B. arr.sort((a, b) => a - b); C. arr.reverse(); D. arr.sort((a, b) => b - a); ✔ANSWER:
C. arr.reverse(); Additional Info: A. The sort() function sorts values as strings in ASCENDING order: ["Apple", "Banana", "Mango", "Orange"]; B,D. Expect a number array. What are the different data types used in JavaScript? ✔ANSWER: There are 8 basic data types in JavaScript:
A developer wants to use a module called DatePrettyPrint. This module exports one default function called printDate(). How can a developer import and use the printDate() function? A. import printDate() from '/path/DatePrettyPrint.js'; printDate(); B. import printDate from '/path/DatePrettyPrint.js'; printDate(); C. import DatePrettyPrint from '/path/DatePrettyPrint.js'; DatePrettyPrint.printDate(); D. import printDate from '/path/DatePrettyPrint.js'; DatePrettyPrint.printDate(); ✔ANSWER: B. import printDate from '/path/DatePrettyPrint.js'; printDate(); Additional Info: Once imported by name, you must call the imported method directly by name. You must call the methods directly or use the * to import all methods. When using the const identifier to create an instance of an object, are the properties changeable? ✔ANSWER: Yes. An object declared as const can be modified. The const fixes the value of the object, but NOT its contents. A developer is not able to use a variable outside a function that is defined inside a function. What could be causing this? ✔ANSWER: Variables defined INSIDE a function cannot be accessed from anywhere OUTSIDE the function, because the variable is defined only in the scope of the function. If an application manipulates the browser history using the History API, which event should a developer use to detect when the browser's native back or forward button is clicked? A. popstate B. navigate
C. pushstate D. change ✔ANSWER: A. Popstate, A popstate event is dispatched to the window each time the active history entry changes between two history entries for the same document. Additional Info: The navigate() method loads a specified URL. The pushstate() method pushes the given data onto the session history stack with the specified title. The change event is fired for changes to ,
What will the output be in the console for the following: let s_prim = 'foo' let s_obj = new String(s_prim) console.log(typeof s_prim) console.log(typeof s_obj) ✔ANSWER: String Object What will the output be in the console for the following: let s1 = '2 + 2' let s2 = new String('2 + 2') console.log(eval(s1)) console.log(eval(s2)) console.log(eval(s2.valueOf())) ✔ANSWER: 4 "2 + 2" 4 What will the output be in the console for the following: const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.'; const regex = /[A-Z]/g; const found = paragraph.match(regex); console.log(found); ✔ANSWER: ["T","I"] Additional Info: If the g flag is used, all matches will be returned. If the g flag is NOT used, then only the first match will be returned. What will the output be in the console for the following: const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.'; const regex = /[A-Z]/; const found = paragraph.match(regex); console.log(found); ✔ANSWER:
Additional Info: If the g flag is used, all matches will be returned. If the g flag is NOT used, then only the first match will be returned. What will the output be in the console for the following: [2, 5, 9].forEach(logArrayElements); function logArrayElements(element, index, array) { console.log('a[' + index + '] = ' + element); } ✔ANSWER: a[0] = 2, a[1] = 5, a[2] = 9 What is the difference between for ... of and for ... in while iterating over an array of elements? ✔for ... of iterates via elements while for ... in uses an index through the array. What will the output be in the console for the following: const arr = [1, 4, 9, 16]; const mapA = arr.map(x => x * 2); console.log(mapA); ✔ANSWER: [2,8,18,32] What will the output be in the console for the following: const arr = [1, 4, 9, 16]; const mapA = arr.filter(x => x % 2); console.log(mapA); ✔ANSWER: [4,16] What will the output be in the console for the following: const arr = [1, 4, 9, 16]; console.log(arr.slice(1,3)); ✔ANSWER: [4,9] What will the output be in the console for the following: const arr = [1, 4, 9, 16]; const reducer = (acc, curr) => acc + curr; console.log(arr.reduce(reducer)); ✔ANSWER: 30
names : ['Sue', 'Joe'], showMessage: function() { this.names.forEach(function(name) { console.log(this.hello + ' ' + name); }); } } message.showMessage(); ✔ANSWER: undefined Sue undefined Joe Additional Info: Referencing the 'this' keyword inside the nested function just refers to the scope in which the object was invoked, which in this case is global, meaning the variable hello does not exist. What will the output be in the console for the following: let message = { hello : 'Hello', names : ['Sue', 'Joe'], showMessage: function() { let self = this; this.names.forEach(function(name) { console.log(self.hello + ' ' + name); }); } } message.showMessage(); ✔ANSWER: Hello Sue Hello Joe Additional Info: The self variable references what is known as the "lexical scope," because it was defined within the showMessage function. What will the output be in the console for the following: let message = { hello : 'Hello', names : ['Sue', 'Joe'], showMessage: function() {
this.names.forEach(name => { console.log(this.hello + ' ' + name); }); } } message.showMessage(); ✔ANSWER: Hello Sue Hello Joe Additional Info: The arrow function that ES6 introduced has the lexical scope built in. What will the output be in the console for the following: function showContact (firstName, lastName, ...titles) { console.log(firstName + ' ' + lastName + ', ' + titles[0] + ' and ' + titles[1]); } showContact('Sue', 'Johnson', 'Developer', 'Architect'); ✔ANSWER: Sue Johnson, Developer and Architect Additional Info: ... allows you to take an unknown number of parameters in that space What will the output be in the console for the following: let array1 = ['one', 'two']; let array2 = ['three', 'four']; array1.push(...array2); console.log(...array1); ✔ANSWER: "one" "two" "three" "four" Additional Info: using ... also acts as a spread and will pull elements one at a time through. When creating a class in JavaScript, what is autogenerated? A. prototype B. constructor C. function D. extension ✔ANSWER:
Which of the following is FALSE about Modules? A. Modules always execute in strict mode, which means that variables need to be declared. B. They only get executed once, which is right when they are loaded. C. Import statements get hoisted, which means that all dependencies will execute right when the module is loaded. D. Modules must include a constructor to be exported. ✔ANSWER: D. Modules must include a constructor to be exported. Refer to this code: const p1 = new Promise((resolve, reject) => { setTimeout(() => { resolve('P1 Resolved'); }, 1500); }); const p2 = (data) => new Promise((resolve, reject) => { setTimeout(() => resolve('${data}, P2 Resolved'), 1500, data); }); Which two answers correctly execute p1 and p2? A. p1.then((data) => p2(data)).then(result => result); B. async function getResult() { const data = await p1; return await p2(data); } getResult(); C. p1().then(function() { p2().then(function(result) { return result; }); }); D. async function getResult() { const data = p1; const result = p2(data); } await getResult(); ✔ANSWER: A + B Additional Info: A. The method promise.then() is used to associate further action with a promise that becomes settled.
B. Using await inside the function causes both to execute. C. The syntax is wrong for calling p1 and p2. D. The await is outside the function calling the data. Refer to this code: const getId = new Promise((resolve, reject) => { setTimeout(() => resolve(15), 1500); }); const getBook = bookId => new Promise((resolve, reject) => { setTimeout(() => resolve('${bookId}:JavaScript Algorithms'), 1500); }); getId.then(id => getBook(id)).then(data => data); What is the correct code to replace the last line with an async/await function? A. async function getBookInfo() { const Id = await getId; const result = await getBook(id); } getBookInfo(); B. async function getBookInfo() { const Id = await getId; const result = await getBook(id); } await getBookInfo(); C. await function getBookInfo() { const Id = getId; const result = getBook(id); } async getBookInfo(); D. async function getBookInfo() { const Id = getId; const result = getBook(id); } await getBookInfo(); ✔ANSWER: A. Additional Info: The await expression is always inside an async function.
Refer to the code below: const https = require('https'); const server = https.createServer((req, res) => { // Line 3: code goes here let reqData = JSON.parse(chunk); console.log(reqData);}); res.end('OK');}); server.listen(8000); Which code does the developer need to add to line 03 to receive incoming request data? A. req.on('data', (chunk) => { B. req.get('data', (chunk) => { C. req.data((chunk) => { D. req.on('get', (chunk) => { ✔ANSWER A. req.on('data', (chunk) => { Additional Info req.get is used with http requests. req.data is what is getting passed in, not a method. req.on('get', ... the 'get' argument is not passed in so can not be used this way. What are the most important items in your npm package.json file if you plan to publish a package? ✔The name and version. Together they form a unique identifier for the package. When it comes to frameworks, what is one of the main benefits of using popular frameworks? ✔One of the main benefits is a strong community supporting the framework. This usually results in good documentation and resources. When using async, what type of object is always returned? A. Null B. String C. Promise D. Map ✔ANSWER: C. Promise
What is the output of the following: for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 1); } for (let i = 0; i < 3; i++) { setTimeout(() => console.log(i), 1); } A: 0 1 2 and 0 1 2 B: 0 1 2 and 3 3 3 C: 3 3 3 and 0 1 2 ✔ANSWER: C: 3 3 3 and 0 1 2 Additional Info: In the first loop, the var i value is GLOBAL, which means the value is set OUTSIDE of the scope of the loop. So the var i value is incremented independent of the setTimeout function firing. let and const are block-scoped and follow the increment accordingly. What's the output? const shape = { radius: 10, diameter() { return this.radius * 2; }, perimeter: () => 2 * Math.PI * this.radius, }; console.log(shape.diameter()); console.log(shape.perimeter()); A: 20 and 62. B: 20 and NaN C: 20 and 63 D: NaN and 63 ✔ANSWER: B: 20 and NaN Additional Info:
let d; d = c; c.greeting = 'Hello'; console.log(d.greeting); A: Hello B: Hey! C: undefined D: ReferenceError E: TypeError ✔ANSWER: A. Hello Additional Info: In JavaScript, all objects interact by reference when setting them equal to each other. First, variable c holds a value to an object. Later, we assign d with the same reference that c has to the object. When you change one object, you change all of them. What's the output? let a = 3; let b = new Number(3); let c = 3; console.log(a == b); console.log(a === b); console.log(b === c); A: true false true B: false false true C: true false false D: false true true ✔ANSWER: C: true false false Additional Info: new Number() is a built-in function constructor. Although it looks like a number, it's not really a number: it has a bunch of extra features and is an object.
When we use the == operator, it only checks whether it has the same value. They both have the value of 3, so it returns true. However, when we use the === operator, both value and type should be the same. It's not: new Number() is not a number, it's an object. Both return false. What's the output? class Chameleon { static colorChange(newColor) { this.newColor = newColor; return this.newColor; } constructor({ newColor = 'green' } = {}) { this.newColor = newColor; } } const freddie = new Chameleon({ newColor: 'purple' }); console.log(freddie.colorChange('orange')); A: orange B: purple C: green D: TypeError ✔ANSWER: D. TypeError Additional Info: The colorChange function is static. Static methods are designed to live only on the constructor in which they are created, and cannot be passed down to any children. Since freddie is a child, the function is not passed down, and not available on the freddie instance: a TypeError is thrown. What's the output? let greeting; greetign = {}; // Typo!