Written Exam Flashcards

1
Q

Examine the two code examples below. Explain, with particular reference to line 3 of each example, why Example 1 logs the same values for arr1 and arr2, but Example 2 logs different values.

A

Both examples, the variables declared on lines 1 and 2 both reference the same array. Line 1 the array [1, 2, 3] is assigned to the variable arr1. On line 2, arr2 is declared and assigned the same value as arr1, which is a reference pointing to the array. On line 3 of the first example, we mutate the array with the push method, pushing the number 4 to it. So on lines 5 and 6, when the log method is called on the console object, arr1 and arr2 are logged to console revealing they are in fact the same array.

But on line 3 of example 2, we reassigned arr1 to the return the concat method with the argument [4]. This returns a new array, a new reffence to a different array.

So when arr1 and arr2 are logged to console, we see they are different arrays.

This example demonstrates that difference between primitive and object values. Object “values” are just references to the objects in the heap. Whereas primitive values are stored directly with the variable on the stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Examine the code example below. Explain why line 7 logs 5, but line 8 raises a ReferenceError.

A

On line 1 the global variable num1 is declared and initialized with the number 5. Line 7 then invokes the log method on the console object using the variable num1 as an argument, logging the number 5 to console.

Despite the variable declaration on line 4, that variable is not accessible outside that block defined between line 3 and 5. On line 4 the variable num2 is declared and initialized to the number 10, but it has local scope and is only accessible within that block (even though the if conditionnal statement with the conditional (num1 > 4) on line 3 evaluates to true and the block executes. num2 still only has local scope). On line 8 when num2 is attempted to be accessed, it is not available and throws a reference error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the following code log? Explain how you came up with your answer.

A

The code above will log the number 23 to the console.

On line 1, the global variable score is declared and initialized with the value 23. Lines 3 to 6 we have a function declaration for the function updateScore with the parameter points. Within that function on line 4 we declare the variable score and on line 5 we have the addition assignment operator with the points parameter. This function doesn’t return anything and will always evaluate to undefined.

on line 8 the updateScore function is invoked with the argument 5, which looks like it might add 5 to the global score variable. But it doesn’t!

So on line 9 when the log method is invoked on the console object with the score argument. It reveals the same value that score was initialized with.

This demonstrates variable shadowing. Namely that variables declared within an inner scope shadow variables with the same name in the outer scope (score).

If the let keyword were omitted, the inner score would refer to the global score and it would reassigned that variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Examine the following code carefully. Which variable names are in the global scope, and which are local to the scope inside the add function? Explain how you came up with your answer, making sure to name all of the variables in each of the two scopes.

A

Globally scoped variables: num1, num2, num3, add.

We know num1, num2, num3 have global scope because they were declared outside any curly braces (blocks or functions). The function add also has global scope because it was declared outside any blocks or functions.

Variables locally scoped to the add function: num1, num2, total.

Despite the parameters num1 and num2 having the same names as the global variables, they are distinct and prevent access to the outer scoped variables of the same name. They shadow the global variables of the same name. The variable total is declared within the function body and has local scope there as well.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The timesTwo function is successfully invoked on line 3, but attempting to invoke the timesThree function on line 4 raises an error. Explain why the timesTwo invocation is successful, but the invocation of timesThree is not. Be precise about why these two functions have such different behavior.

A

On lines 3 and 4 we see the functions timesTwo and timesThree invoked before they are defined.

This is fine for the function timesTwo declared on lines 6 to 8. Because function declarations are “hoisted”. Javascript moves these declarations to the top and evaluates their definition before the first line of code is run.

This is not the case or function expressions or arrow function definitions like the timesThree function defined on line 10. On line 4 this function is attempted to be accessed before it is defined and it throws a reference error.

This demonstrates the concept of hoisting, where function declarations (where function is the first keyword on the line) have their definition moved to the top. So they can be invoked on lines earlier than they are declared.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The code on line 8 logs the number 10. Explain in detail why that is the case.

A

On line 1 the global variable bottles is declared and initilized to the number 10. within the function declaration for decrementBottles the parameter bottles will shadow the global variable bottles When when this function is invoked (even if it took the global bottles variable as an argument), it doesn’t effect the global bottles variable So on line 8 it logs the same bottles value it was initialized with

This again demonstrates the concept of variable shadowing. Namely that inner scope variables can shadow outer scope variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Identify all of the variables, primitive values, and objects in the following code:

A

Variables:

letters
numbers
num
FOUR
double
num (the num parameter within the function double is a distinct locally scoped variable from the other globally scoped num)
Primitive Values: The elements within the letters array. Strings:

“a”
“b”
“c”.
The elements within the numbers array. Numbers:

3
4.5
5
The number 4 assigned to the FOUR variable (the result of the Math.floor(numbers[1]) expression)

The number 2 within the arrow function definition on line 5:

on line 6, the expression letters[0] evaluates to the primitive “a”
on line 4, the expression numbers[1] evaluates to the primitive 4.5
on line 6, the function double(letters[0]) returns the primitive NaN

on line 6, the 0 within letters[0]

on line 4, the 1 within numbers[1]

Objects:

the letters array object [“a”, “b”, “c”]
the numbers array object [3, 4.5, 5]
the function assigned the the double variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain why line 8 below logs A sentence is a set of words put together with meaning. rather than Asentenceisasetofwordsputtogetherwithmeaning.. What does this demonstrate about what happens on line 2?

A

When a primitive is passed into a function as an argument, it is the value only that is passed into it. If this string is changed, it will not effect the original variable holding the value. So when the string assigned to sentence is passed as an argument into the squash function invocation on line 6, the sentence variable is unaffected. So when it is logged on line 8, it is the same value it was initialized with. This demonstrates the concept of pass by value, that primitives passed into functions only pass the value. Not a reference to the variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The following code is similar to that from the previous question. What does it log to the console and why?

A

In this case, it will log to console the string “Asentenceisasetofwordsputtogetherwithmeaning”.

This is becasue we are no longer passing a primitive into the function. We are passing a reference to an object. So when we make changes to that object within the function, the changes remain outside the local scope of the function.

The function squash declared on lines 1 to 3 takes the parameter array. On line 2 within the function body, the first element of the array parameter is reassigned with the return from the method replaceAll called on it.

On line 5, the variable sentence is declared and initliazed with an array with 1 element “A sentence is a set of words put together with meaning.”

On line 6 when the function squash is invoked with the array sentence as an argument, the first element of the sentence array is reassigned with the return of the same element with the replaceAll method invoked on it, returning “Asentenceisasetofwordsputtogetherwithmeaning”.

The array is mutated within the function body on line 2. Where the element at array[0] is reassigned with the return from array[0].replaceAll(“ “, “”), a copy of the string but with all the “ “ replace with “”.

This demonstrates the difference of pass by reference. When objects are passed as arguments into functions, they pass references to the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

In the following code, the first two lines log false, while the third logs true. Shouldn’t it be the other way around? Why does the code behave the way it does?

A

This demonstrates implicit coercion. Also that the “value” returned by any given object literal, is a reference to a unique object. Despite [] and [] appearing the same, they represent unique objects. Unique object references. Whether strict comparison or loose comparison, they are different references. So on lines 1 and 2, they evaluate to false.

On line 3 however, when an array is compared to a string (using the non-strict/loose equality operator), the array is implicitly coerced into a string. An empty array will be coerced into an empty string. so it’s really comparing “” == “” which evaluates to true

This demonstrates implicit coercion. In this case, an array into a string when the array is compared to a string using the non-strict equality operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly