Unit 5 Flashcards

1
Q

What is JavaScript primarily used for in web development?
A. Styling pages
B. Structuring content
C. Adding interactivity and dynamic content
D. Creating database schemas

A

Answer: C

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

Which of the following tags is used to write JavaScript code in HTML?
A. <.js.>
B. <.javascript.>
C. <.script.>
D. <.code.>

A

Answer: C

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

Where is the best place to place a

 tag for performance?
A. In the <head> tag
B. Right after the opening <html>
C. At the end of the <body>
D. Inside a <style> block</style>
A

Answer: C

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

Which attribute allows you to defer loading of JavaScript until the page finishes parsing?
A. type=”module”
B. defer
C. delay
D. buffer

A

Answer: B

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

What is a JavaScript module?
A. A compressed CSS file
B. A reusable HTML block
C. A file using import and export to organize code
D. A built-in browser object

A

Answer: C

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

What is the difference between default and named exports in JavaScript modules?
A. Default exports can only export arrays
B. Named exports are slower
C. Default exports allow one main export; named allows multiple
D. There is no difference

A

Answer: C

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

Which of the following is a correct arrow function syntax for adding two numbers?
A. let add = (a, b) => { return a + b; };
B. function add(a, b) => a + b;
C. add(a, b) = a + b =>
D. let add = function => (a, b) { return a + b; }

A

Answer: A

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

What keyword refers to the current object context in traditional JS functions?
A. that
B. self
C. this
D. context

A

Answer: C

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

What is the key difference in this between arrow functions and regular functions?
A. Arrow functions create a new this context
B. Arrow functions use the surrounding scope’s this
C. Regular functions don’t support this
D. Arrow functions always return undefined

A

Answer: B

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

What does the following code return?
((name) => ‘Hi ‘ + name)(‘Jess’);
```
A. "Hi"
B. "name"
C. "Hi Jess"
D. undefined

A

Answer: C

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

What will this arrow function return?

js
((name) => { return 'Hi ' + name })('Jess');

A. undefined
B. 'Hi Jess'
C. Hi name
D. A syntax error
A

Answer: B

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

What does users.map(user => user.age) return if users is an array of user objects?
A. A single user’s age
B. A list of names
C. A new array of all user ages
D. A new object with age as a key

A

Answer: C

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

What does the reduce() method do?
A. Multiplies all elements
B. Converts a string to lowercase
C. Combines all values in an array into a single value
D. Removes the first item in an array

A

Answer: C

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

What is a callback function?
A. A function called after a variable is defined
B. A variable passed into a method
C. A function passed to another function to be executed later
D. A hidden JavaScript file

A

Answer: C

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

Which of the following is an example of a higher-order function?
A. parseInt()
B. alert()
C. map()
D. toString()

A

Answer: C

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

What will this code output?
const obj = {
value: 42,
getValue: () => this.value
};
console.log(obj.getValue());
~~~
A. 42
B. undefined
C. this
D. Throws an error

17
Q

What does the following code return?

```js
const arr = [[1, 2], [3, 4], [5, 6]];
const flat = arr.reduce((a, b) => a.concat(b), []);
console.log(flat);
~~~

A. [[1, 2], [3, 4], [5, 6]]
B. undefined
C. [1, 2, 3, 4, 5, 6]
D. [1, 3, 5]

18
Q

Why is the following arrow function problematic?

```js
Array.prototype.first = () => this[0];
const arr = [10, 20, 30];
console.log(arr.first());
~~~

A. It throws a syntax error
B. Arrow functions can’t access array methods
C. this refers to the wrong context inside arrow functions
D. Arrow functions can’t return values

19
Q

In the DOM, what is the difference between a method and a property?
A. Properties are always strings, methods are numbers
B. A method performs actions; a property stores values
C. Methods can’t be used with objects
D. There is no difference

20
Q

What does document.querySelector() return?
A. All elements matching a selector
B. A live NodeList
C. The first element matching a CSS selector
D. A string of class names

21
Q

Which of these returns a list of all elements matching a class name?
A. getElementById()
B. querySelector()
C. getElementsByClassName()
D. querySelectorAll(“#id”)

22
Q

Which property lets you change the contents of an HTML element, including child tags?
A. .textContent
B. .innerText
C. .innerHTML
D. .value

23
Q

What is the difference between .textContent and .innerHTML?
A. They both do the same thing
B. .textContent includes HTML tags
C. .innerHTML escapes special characters
D. .textContent sets text only, ignoring HTML tags

24
Q

What method would you use to add a new class to an element’s class list?
A. .classList.add()
B. .addClass()
C. .class()
D. .style.addClass()

25
Q

What does the following code do?
element.setAttribute(‘id’, ‘newId’);
~~~
A. Adds a new class
B. Deletes an element
C. Sets or changes the element’s id
D. Renames the element

26
Q

Which method allows you to insert a new DOM element into a page?
A. appendChild()
B. removeChild()
C. replaceHTML()
D. appendStyle()

27
Q

What is the purpose of addEventListener() in JavaScript?
A. Attaches styles to elements
B. Creates a form field
C. Registers a function to run when a specific event occurs
D. Prevents form submission

28
Q

Which event type is triggered when a text field changes?
A. keyup
B. change
C. input
D. click

29
Q

Which of the following is not a valid DOM event?
A. onresize
B. onlaunch
C. onclick
D. onmouseover

30
Q

What does the following event listener do?

js
button.addEventListener('click', () => alert('Clicked!'));

A. Submits a form
B. Logs button ID
C. Shows a popup when the button is clicked
D. Changes button color
31
Q

Why is using addEventListener() preferred over inline onclick="..."?
A. Inline JS loads faster
B. addEventListener() separates structure and logic
C. Inline JS doesn’t work in modern browsers
D. It’s the only way to handle events

32
Q

Which code ensures focus moves to the next input box after typing 2 characters?
A. if (input.length == 2) { next.focus(); }
B. input.value.length == 2 => focus(next);
C. input.addEvent('focus', nextBox);
D. focus(input).when(full)

33
Q

In the Message Printer lab, how is the hidden output box shown after input?
A. display = true
B. output.style.display = block;
C. outputBox.hidden = false;
D. show(outputBox)