JS MIDTERM

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Select the statement that is false regarding the "this" keyword in JavaScript functions.

Arrow functions have predictable this behaviors and are preferred in object methods and event handler functions.

Concatenation

Binding multiple strings together using the + string operator

what instruction can be used to terminate a loop

Break

what statement skips the current iteration and resumes with the next iteration in the loop

Continue

Declaration

Creating a new variable

What would be the result of this expression? console.log(['Toyota', 'Dodge', 'Mazda', 'Ford'][1])

Dodge

Select the three true answers regarding JavaScript callbacks. The remaining two are false.

------Callbacks are used on certain array iteration methods, such as .forEach() and .reduce(). -------A callback function can run after another function has finished -------A callback is a function passed as an argument to another function

Check off the two statements that are true regarding JavaScript arrays.

-----A best practice for arrays is to ensure that each value is of the same data type (e.g., all numbers, all strings). -----Arrays can contain primitive data types as well as complex types, such as objects and nested arrays.

Select the three things that object destructuring makes possible.

-----Apply custom names to variables. -----Apply default values, which are applied when they are missing from the source object. -----Maintain a smaller number of locally scoped variables, reducing the chance of name collisions.

Select the three statements that are true regarding objects and functions.

----Invoking a function while passing its arguments in an order different from the expected order of parameters will likely produce a bug. -----Objects can be passed as arguments to a function. -----You can use dot or bracket notation to "unpack" an object that has been assigned to a function parameter.

Select the two statements that are true regarding arguments given to a function.

----JS ignores incoming arguments in excess of the function's defined parameters. -----You can designate a default value for a function parameter (e.g., function testMe(x = 2){ return x}), which will apply in cases where the function call is missing that argument.

Below are a series of statements regarding variable scoping. Select the two statements that are false.

-Scopes apply solely to functions. The scope is constrained to the space between the curly braces ({ instructions }) of a function. -All variables can be accessed within any function, conditional statement, or scope, so long as those variables are declared in the same JS file.

Creates a shallow copy of a portion of a given array, limited to just the elements from the given array that pass a conditional test

.filter()

Returns the first element in the provided array that satisfies the provided testing function

.find()

Executes a provided function (a callback) once for every element in the array

.forEach()

Creates a new array populated with the results of calling a provided function on every element in the calling array

.map()

Executes a user-supplied "reducer" callback function on each element of the array to produce a single value

.reduce()

What would be the console output of the expression below? (Hint: consider the difference between prefix and postfix.) let x = 1; console.log(x++);

1

Strings and array elements have no key names, so they are accessed by their index position. Complete the following expression by providing the index number to get the capital "S" in the word below. const myString = "JavaScript";console.log(myString[?]); Provide the missing property to get the 2nd to last letter. console.log(myString[myString.Correct answer?-2])

4 and length

Data type

A term identifying the kind of value held in memory; limits the operations that can be done on it

[?] hide detail and give us the ability to talk about problems at a higher level. This principle is based on the observation that as a code base grows in size and complexity, programmers are more often confused, and bugs tend to be more common.

Abstractions

Template literal

An easy way to interpolate a variable or expression into a string

An object literal is an object that you create by typing the literal syntax. For example: const myDog = { name : "Lily", breed : "Terrier", sex : "female", age : 6 } Which one of the following statements is true regarding inheritance for object literals?

const yourDog = myDog;

The object below holds a series of objects representing each Marvel Universe movie released through 2015. You need only the movies based on Iron Man. Select the code example that correctly uses object destructuring to retrieve those objects and assign each as unique variables. const marvelObj = { mv01 : { title: "Iron Man", year: 2008 }, mv02 : { title: "The Incredible Hulk", year: 2008 }, mv03 : { title: "Iron Man 2", year: 2010 }, mv04 : { title: "Thor", year: 2011 }, mv06 : { title: "Captain America: The First Avenger", year: 2011 }, mv07 : { title: "The Avengers", year: 2012 }, mv08 : { title: "Iron Man 3", year: 2013 }, mv09 : { title: "Thor: The Dark World", year: 2013 }, mv10 : { title: "Captain America: The Winter Soldier", year: 2014 }, mv11 : { title: "Guardians of the Galaxy", year: 2014 }, mv12 : { title: "Avengers: Age of Ultron", year: 2015 }, mv13 : { title: "Ant-Man", year: 2015 } }

const { mv01, mv03, mv08 } = marvelObj;

Closures consist of the function itself combined with its outer state at the time of function [?]

creation

Use codepen.io to create an array named babyNames. Include at least three names in this array. Then provide a code line that console logs only the second name.

et babyNames = [Bri"", "Robert", "Aj"]; console.log(babyNames[1]);

JavaScript array methods are object properties containing a [?] defenition

function

JavaScript array methods are object properties containing a [?] definition

function

The myCars function below prints the names of cars, but we can never know how many arguments (car names) will be sent to the function. Use the rest parameter to accept an indefinite number of arguments. Then update the function body to print the third and fourth car names in the array. Paste your updated code below. function myCars(car1, car2) { console.log("car1", car1); console.log("car2", car2); console.log("car3"); console.log("car4"); } myCars("Tesla", "Dodge", "Mercury", "Subaru", "Toyota", "Mini", "Pontiac");

function myCars(car1, car2, ...rest) { console.log("car3", rest[2]); console.log("car4", rest[3]); } myCars("Tesla", "Dodge", "Mercury", "Subaru", "Toyota", "Mini", "Pontiac");

Which of the following patterns reflects a proper (vanilla) JavaScript function declaration?

function mySum(a, b){ const answer = a + b; return answer; };

The array below represents top cross-country race times, ordered from fastest to slowest. Select the code example that correctly uses destructuring to retrieve only the podium winning times, assigning the variable names of "first," "second," and "third." const timesArr = ["15:05:22","15:50:03","16:32:50","16:45:41","17:01:58","17:33:52","17:50:29","17:53:33","18:09:50","18:20:23"];

let [ first, second, third ] = timesArr;

Given the following object, select the statement that would yield the number of Cy Young Award wins by Clayton Kershaw. let cyYoungWinners = { clemens: { name: 'Roger Clemens', wins: [1986, 1987, 1991, 1997, 1998, 2001, 2004],}, johnson: { name: 'Randy Johnson', wins: [1995, 1999, 2000, 2001, 2002] }, kershaw: { name: 'Clayton Kershaw', wins: [2011, 2013, 2014] },

let claytonWins = cyYoungWinners["kershaw"].wins.length

When we assign a function as the value to one of our keys, we call that function a [?]

method

Given the following object literal, which line of code would successfully change the occupation property? let person = { fname: "Abraham", lname: "Lincoln", occupation : "senator", age: 52 }

person.occupation = "president";

Consider this small program again. What does this last line of code produce? console.log(result); let x = 1; const parentFunction = function(){ let myValue = 2; console.log(x); console.log(myValue); const childFunction = function(){ console.log(x += 5); console.log(myValue += 1); } return childFunction;}const result = parentFunction();result();console.log(result); // What does this produce?

It prints the childFunction function definition to the console.

Which of the following statements best describes JavaScript?

JavaScript is highly versatile and flexible, favoring usability over strict convention.

A primitive data type is not an object and hence it has neither properties nor methods. List at least three of the six main primitive (or simple) data types.

Null, undefind, number

Select the answer that reflects a proper use of the rest parameter.

const myFunc = (arg2, arg3, ...moreNums) => { console.log(moreNums[0]); // more code here }

Primitive data type

A classification of simple data types, characterized by a lack of object characteristics

Variable

A named container for a value

JS uses the concept of binding to hold onto things (values, objects, functions) for the lifecycle of the program (or until they are no longer needed). what is the the act of binding (or attaching) the variable name to its data.

Assignment

let

Declares a block-scoped local variable

var

Declares a function-scoped or globally-scoped variable

Starts by executing the body instruction, then applies the conditional test to determine whether to repeat the body instruction. This loop type has no built-in counter initialization or incrementation.

Do While Loop

The JS interpreter will evaluate each line of the function body, including lines that occur after the return statement.

False

true or false A closure is synonymous with lexical scope.

False

Provides "built-in" support for counter initialization, a conditional test, and counter incrementation. The conditional test is applied before the body instruction is executed.

For Loop

Assignment

Giving a value to a variable

Bindings that are created outside of any and all function or blocked {} statements

Global variables

Interpolation

Injecting a variable directly into a string

const

Is a read-only value that can't be changed through reassignment and can't be redeclared.

The scope environment within which a function is created

Lexical scope

Function parameters and variables declared inside the function body

Local variables

A scope that exists within another scope

Nested scope

Which of the following statements is true regarding the concept of "state" in a JS program?

State refers loosely to the contents of a program's memory at a given time. State can affect program behavior.

Which statement(s) correctly describe the result of the following code? let a = false;let b = "";console.log(a == b); // Case 1console.log(a === b); // Case 2

The === operator tests for equality in both type and value. An empty string is not a true boolean type. Thus, a===b returns false. and The == operator tests for equality in value only. An empty string seems "falsey," so JS treats it as the boolean false. Thus, a==b returns true.

When JavaScript's interpreter expects but does not get a boolean value (true/false), then it will try to intelligently coerce the result to a true or false value. Select the four expressions that will evaluate as true.

console.log(Boolean("Are we having fun yet?")) console.log(Boolean(1 - 2)) console.log(Boolean("string".length)) console.log(Boolean(undefined))

You have a variety of techniques for appending items to an array. Which of the following array expressions result in mutation of the original array (myArray1)? let myArray1 = ["Yorkshire Terrier", "Greyhound", "Poodle", "Great Dane"]; let myArray2 = ["Chihuahua", ...myArray1]; let myArray3 = myArray1.push("German Shepherd"); console.log(myArray1);

The myArray3 statement mutates myArray1, and myArray2 does not.

Which statement best describes the order in which the JavaScript interpreter evaluates code.

Top to bottom, except where function and var hoisting, conditional statements, and loops dictate otherwise.

True or False, The term ES6 generally refers to the current implementation of the modern JavaScript standard.

True

True or false A JS array can be described as a set of like elements with numerical indexing (no string-based key-value pairs).

True

True or false Functions that operate on other functions, either by taking them as arguments or by returning them, are called higher-order functions.

True

The code below uses bracket notation to pass each element of the numbers array to the mySum function. Rewrite the function invocation by using the spread operator. This means you will not have to type out a reference to each individual array element. Paste your code below. const mySum = (a, b, c, d, e) => { return a + b + c + d + e; } const nums = [5, 3, 9, 10, 15]; // modify the following line only console.log(mySum(nums[0], nums[1], nums[2], nums[3], nums[4]));

console.log(mySum(...nums));

Which of the following statements is true regarding call stack execution?

When the current function is finished, the interpreter takes it off the call stack and then resumes execution at the place in the code where the function call was made.

Starts with a conditional test and repeats the body instruction until the test returns false. This loop type has no built-in counter initialization or incrementation.

While Loop

The [?] object holds all parameter values for regular functions (Note: It does not apply to arrow functions). One drawback, however, is that it is a non-standard array, so we cannot directly access array methods like .slice(). For these reasons, the ...rest parameter is often a better alternative.

arguments

true or false Pure functions are not closures.

true

Select the statement that is true regarding this small program. let x = 1; const parentFunction = function(){ let myValue = 2; console.log(x); console.log(myValue); const childFunction = function(){ console.log(x += 5); console.log(myValue += 1); } return childFunction; } const result = parentFunction(); result();

x is a global variable that can be read and changed by any function, whereas myValue is a private variable that can be read and changed only within its closure.


Kaugnay na mga set ng pag-aaral

Global Business - FVC1 (All of the Practice Quizzes in MindTap), FVC1 Global Business V2 Pre-Assessment, WGU Global Business FVC1 V2 and D080 Module 1-4

View Set

EMT Chapter 9: Patient Assessment Part 1

View Set

Chapter 10: Muscle Tissue and Muscle Organization

View Set

Short stories test -combined set (final)

View Set

Group Health and Blanket Insurance

View Set

Ch 33 Obstetrics and Neonatal Care

View Set