Unit 2 - Practice Quiz

INT219 59 Questions
0 Correct 0 Wrong 59 Left
0/59

1 How do you write a single-line comment in JavaScript?

JavaScript syntax and data types Easy
A. /* This is a comment */
B. <!-- This is a comment -->
C. // This is a comment
D. # This is a comment

2 Which of the following is NOT a primitive data type in JavaScript?

JavaScript syntax and data types Easy
A. Boolean
B. String
C. Number
D. Object

3 Which keyword is used to declare a variable that cannot be reassigned?

Variables and scope Easy
A. const
B. static
C. var
D. let

4 What is the scope of a variable declared with the let keyword inside a code block {}?

Variables and scope Easy
A. Function scope
B. Global scope
C. Block scope
D. No scope

5 What is the result of the expression 5 + '5' in JavaScript?

Operators and expressions Easy
A. '55'
B. undefined
C. '5+5'
D. 10

6 Which operator is used for strict equality (checks both value and type)?

Operators and expressions Easy
A. ===
B. !=
C. =
D. ==

7 Which statement is used to execute a block of code only if a specified condition is true?

Control flow statements Easy
A. while
B. if
C. switch
D. for

8 Which syntax is correct for declaring a basic named function in JavaScript?

Functions and arrow functions Easy
A. let myFunction = () => {}
B. def myFunction() {}
C. function myFunction() {}
D. myFunction = function() {}

9 How do you access the first element of an array named fruits?

Arrays and objects Easy
A. fruits[0]
B. fruits.get(0)
C. fruits[1]
D. fruits.first()

10 Which of the following correctly defines an empty object literal?

Arrays and objects Easy
A. ()
B. []
C. {}
D. new Object

11 Which JavaScript event is triggered when a user clicks an HTML element?

Basic event handling Easy
A. onmouseover
B. onclick
C. onchange
D. onsubmit

12 Which browser-specific function is used to display a simple message in a popup dialog box?

Introduction to browser interaction Easy
A. console.log()
B. prompt()
C. alert()
D. document.write()

13 What will typeof true return?

JavaScript syntax and data types Easy
A. "boolean"
B. "undefined"
C. "string"
D. "number"

14 Before ES6 (2015), which was the only keyword available for declaring variables in JavaScript?

Variables and scope Easy
A. const
B. var
C. let
D. variable

15 What does the modulus operator (%) do?

Operators and expressions Easy
A. It calculates the percentage of a number.
B. It performs exponentiation.
C. It compares two values for equality.
D. It returns the remainder of a division.

16 Which keyword is used to stop the execution of a loop or a switch statement?

Control flow statements Easy
A. continue
B. exit
C. return
D. break

17 What is the correct syntax for a simple arrow function that takes no arguments?

Functions and arrow functions Easy
A. function => console.log('Hello');
B. () -> console.log('Hello');
C. () => console.log('Hello');
D. => () console.log('Hello');

18 Which array method adds one or more elements to the end of an array and returns the new length?

Arrays and objects Easy
A. shift()
B. concat()
C. pop()
D. push()

19 How do you correctly start a for loop that counts from 0 to 4?

Control flow statements Easy
A. for (let i = 0 to 4)
B. loop (i = 0; i < 5; i++)
C. for (i = 0; i < 5)
D. for (let i = 0; i < 5; i++)

20 What is the modern, recommended way to attach an event handler to an HTML element using JavaScript?

Basic event handling Easy
A. element.addEventListener('click', myFunction);
B. element.onclick = myFunction;
C. element.attachEvent('onclick', myFunction);
D. <button onclick="myFunction()">

21 What will be logged to the console after running the following code snippet?

javascript
let a = '5';
let b = 3;

console.log(a + b);
console.log(a - b);

Operators and expressions Medium
A. 8 and 2
B. "53" and "2"
C. "53" and 2
D. NaN and 2

22 What is the output of the following code, which demonstrates a classic closure and scope issue with var?

javascript
for (var i = 0; i < 3; i++) {
setTimeout(() => {
console.log(i);
}, 10);
}

Variables and scope Medium
A. 3, 3, 3
B. 0, then 1, then 2 (with a delay)
C. undefined, undefined, undefined
D. 0, 1, 2

23 Consider the following object. What will person.greet() log to the console?

javascript
const person = {
name: 'Alice',
greet: () => {
console.log('Hello, ' + this.name);
}
};

person.greet();

Functions and arrow functions Medium
A. Hello, Alice
B. Hello,
C. Hello, undefined
D. A TypeError is thrown

24 What is the difference in the final state of the original array after running code A versus code B?

javascript
// Code A
const original = [1, 2, 3, 4, 5];
const newA = original.slice(1, 3);

// Code B
const original = [1, 2, 3, 4, 5];
const newB = original.splice(1, 3);

Arrays and objects Medium
A. The original array is mutated by splice but not by slice.
B. Both methods mutate the original array.
C. The original array is mutated by slice but not by splice.
D. Neither method mutates the original array.

25 What will be the final value of count after this loop completes?

javascript
let count = 0;
for (let i = 0; i < 5; i++) {
if (i % 2 === 0) {
continue;
}
count++;
}

Control flow statements Medium
A. 5
B. 2
C. 3
D. 0

26 What will the values of resultA and resultB be?

javascript
const value = 0;
const resultA = value || 'default';
const resultB = value ?? 'default';

Operators and expressions Medium
A. resultA is 0, resultB is 0
B. resultA is 0, resultB is 'default'
C. resultA is 'default', resultB is 0
D. resultA is 'default', resultB is 'default'

27 Imagine a <div> containing a <button>. Both have click event listeners attached. If a user clicks the button, in what order do the event handlers typically fire by default?

html
<div id="parent">
<button id="child">Click Me</button>
</div>

<script>
document.getElementById('parent').addEventListener('click', () => console.log('Parent clicked'));
document.getElementById('child').addEventListener('click', () => console.log('Child clicked'));
</script>

Basic event handling Medium
A. The order is random and not guaranteed
B. Child's handler first, then Parent's handler (Bubbling)
C. Parent's handler first, then Child's handler (Capturing)
D. Only the Child's handler fires

28 Which code snippet correctly creates a new <p> element with the text "Hello World" and appends it to a div with the id container?

html
<div id="container"></div>

Introduction to browser interaction Medium
A. javascript
const p = new Paragraph('Hello World');
document.getElementById('container').add(p);
B. javascript
const container = document.getElementById('container');
const p = document.create('p');
p.text = 'Hello World';
container.append(p);
C. javascript
const container = document.getElementById('container');
const p = document.createElement('p');
p.textContent = 'Hello World';
container.appendChild(p);
D. javascript
const container = document.querySelector('#container');
container.innerHTML = '<p>Hello World</p>';

29 What does the following code log to the console?

javascript
let x = 10;
function test() {
let x = 20;
console.log(x);
}
test();
console.log(x);

Variables and scope Medium
A. 20, then 10
B. 20, then 20
C. 10, then 10
D. 10, then 20

30 What is the result of typeof null in JavaScript?

JavaScript syntax and data types Medium
A. "object"
B. "undefined"
C. "void"
D. "null"

31 Given the array const letters = ['a', 'b', 'c', 'd'];, what will be the value of the rest variable after this destructuring assignment?

javascript
const [first, , third, ...rest] = letters;

Arrays and objects Medium
A. ['d']
B. [] (an empty array)
C. ['b', 'd']
D. 'd'

32 What is the output of this switch statement due to fall-through?

javascript
let level = 2;
let access = '';

switch (level) {
case 1:
access += 'User ';
case 2:
access += 'Editor ';
case 3:
access += 'Admin ';
break;
case 4:
access += 'SuperAdmin ';
}
console.log(access);

Control flow statements Medium
A. "Editor "
B. "User Editor Admin "
C. "" (empty string)
D. "Editor Admin "

33 What is logged to the console when the following function is called as calculate(5)?

javascript
const calculate = (x, y = x * 2) => {
return x + y;
}

Functions and arrow functions Medium
A. 5
B. NaN
C. 15
D. 10

34 What are the final values of x and y after this code runs, considering short-circuiting and the post-increment operator?

javascript
let x = 0;
let y = x++ || 10;

Operators and expressions Medium
A. x is 0, y is 10
B. x is 1, y is 1
C. x is 1, y is 0
D. x is 1, y is 10

35 What is the key difference between setting element.innerHTML and element.textContent?

javascript
const div = document.getElementById('myDiv');
const myString = '<strong>Hello</strong>';

// What happens differently here?
div.innerHTML = myString;
div.textContent = myString;

Introduction to browser interaction Medium
A. They are functionally identical for all strings.
B. textContent parses the string as HTML, while innerHTML inserts it as raw text.
C. innerHTML parses the string as HTML, while textContent inserts it as raw text.
D. innerHTML is faster but less secure; textContent is slower but more secure.

36 What is the primary purpose of event.preventDefault() when used inside an event handler for a form's submit event?

Basic event handling Medium
A. To stop the event from bubbling up to parent elements.
B. To trigger a custom validation function.
C. To stop the form from submitting and the page from reloading.
D. To delete the form data before it is sent to the server.

37 What is the content of obj after this code is executed?

javascript
const propName = 'status';
const value = 'active';

const obj = {
id: 123,
[propName]: value
};

Arrays and objects Medium
A. { id: 123, propName: 'status' }
B. { id: 123, status: 'active' }
C. { id: 123, 'status': value }
D. { id: 123, propName: 'active' }

38 What will be logged to the console?

javascript
console.log(message);
var message = 'Hello, Hoisting!';

Variables and scope Medium
A. Hello, Hoisting!
B. null
C. undefined
D. ReferenceError: message is not defined

39 Which of the following comparisons will result in true?

JavaScript syntax and data types Medium
A. false == 'false'
B. null == undefined
C. [] == ![]
D. '2' === 2

40 What is the output of the following Immediately Invoked Function Expression (IIFE)?

javascript
const result = (function(a, b) {
let sum = a + b;
return sum;
})(5, 10);

console.log(result);

Functions and arrow functions Medium
A. undefined
B. 15
C. A ReferenceError because sum is not defined globally
D. The function definition is logged

41 Consider the following JavaScript code snippet. What will be logged to the console?

javascript
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 10);
}

for (let j = 0; j < 3; j++) {
setTimeout(() => console.log(j), 10);
}

Variables and scope Hard
A. 3, 3, 3, and then 0, 1, 2
B. 0, 1, 2, and then 0, 1, 2
C. 0, 1, 2, and then 3, 3, 3
D. It will throw an error.

42 What is the result of the following expression in a modern JavaScript engine?

javascript
let x = 0;
let y = null;

const result = y || x ?? 'default';

Operators and expressions Hard
A. A SyntaxError is thrown.
B. 0
C. 'default'
D. null

43 Given the following code, what is the output of console.log(config.settings.retries) after the attemptChange function is called?

javascript
'use strict';

const config = {
id: 1,
settings: {
retries: 3,
timeout: 5000
}
};

Object.freeze(config);

function attemptChange(obj) {
try {
obj.id = 2; // This will fail silently in non-strict mode, but throw in strict mode
obj.settings.retries = 5; // This modification is on a nested object
} catch (e) {
console.log('Error caught');
}
}

attemptChange(config);
console.log(config.settings.retries);

Arrays and objects Hard
A. 3
B. The line console.log(config.settings.retries) is never reached due to an unhandled error.
C. undefined
D. 5

44 What is logged to the console when user.greet() is executed?

javascript
function User(name) {
this.name = name;
this.greet = () => {
const nestedGreet = () => {
console.log(this.name);
};
nestedGreet();
};
}

const user = new User('Alice');
user.greet();

Functions and arrow functions Hard
A. undefined
B. An empty string
C. A TypeError is thrown because this is undefined.
D. Alice

45 What will be the output of the following code?

javascript
let result = '';
outer_loop:
for (let i = 0; i < 3; i++) {
result += o${i};
for (let j = 0; j < 3; j++) {
if (i === 1 && j === 1) {
continue outer_loop;
}
result += i${j};
}
}
console.log(result);

Control flow statements Hard
A. o0i0i1i2o1i0o2i0i1i2
B. o0i0i1i2o1i0i1i2o2i0i1i2
C. o0i0i1i2o1i0o1i2o2i0i1i2
D. o0i0i1i2o1o2i0i1i2

46 What are the values of res1 and res2 after this code executes?

javascript
const res1 = {} + [];
const res2 = [] + {};

console.log(res1: {res2});

JavaScript syntax and data types Hard
A. res1: [object Object], res2: [object Object]
B. res1: NaN, res2: [object Object]
C. res1: 0, res2: [object Object]
D. res1: [object Object], res2: 0

47 Consider the following HTML and JavaScript. If a user clicks on the p element, in what order will the messages be logged to the console?

html
<div id="div1">
<div id="div2">
<p id="p_element">Click me</p>
</div>
</div>

javascript
const div1 = document.getElementById('div1');
const div2 = document.getElementById('div2');
const p = document.getElementById('p_element');

div1.addEventListener('click', () => console.log('div1 capture'), true);
div1.addEventListener('click', () => console.log('div1 bubble'));

div2.addEventListener('click', () => console.log('div2 bubble'));

p.addEventListener('click', () => console.log('p target'));

Basic event handling Hard
A. p target, div2 bubble, div1 bubble, div1 capture
B. div1 bubble, div2 bubble, p target, div1 capture
C. div1 capture, p target, div2 bubble, div1 bubble
D. div1 capture, div2 bubble, p target, div1 bubble

48 What is the output of this code?

javascript
function checkTDZ() {
console.log(a);
let a = 10;
}

try {
checkTDZ();
} catch (e) {
console.log(e.name);
}

Variables and scope Hard
A. TypeError
B. undefined
C. 10
D. ReferenceError

49 What does the following code log to the console?

javascript
const arr = [1, 2, 3];

arr.forEach((num, index) => {
console.log(num);
if (index === 0) {
arr.push(4);
arr.splice(1, 1);
}
});

Arrays and objects Hard
A. 1, 2, 3, 4
B. 1, 3, 4
C. 1, 2, 3
D. 1, 3

50 What is the result of the following code snippet?

javascript
const myObject = {
data: 'some data',
getData: function() {
const getArrow = () => arguments[0];
return getArrow('arg1', 'arg2');
}
};

console.log(myObject.getData('outer_arg1'));

Functions and arrow functions Hard
A. A ReferenceError because arguments is not defined in an arrow function.
B. arg1
C. outer_arg1
D. undefined

51 Suppose you have the following code. In what order will the messages be logged to the console?

javascript
console.log('A: Script Start');

setTimeout(() => {
console.log('B: setTimeout');
}, 0);

Promise.resolve().then(() => {
console.log('C: Promise.then 1');
}).then(() => {
console.log('D: Promise.then 2');
});

console.log('E: Script End');

Introduction to browser interaction Hard
A. A, C, D, E, B
B. A, E, B, C, D
C. A, E, C, D, B
D. A, B, C, D, E

52 What is the value of result in the following code?

javascript
let a = 1;
const arr = [2, 3, 4];
const result = arr[a] + (a += 1) + arr[a++];

Operators and expressions Hard
A. 11
B. The result is unpredictable due to unspecified evaluation order.
C. 9
D. 10

53 What is the output of this code?

javascript
const obj = Object.create(null);
obj.prop = 'exists';

let hasOwn = false;
try {
hasOwn = obj.hasOwnProperty('prop');
} catch(e) {
hasOwn = e.name;
}

const hasIn = 'prop' in obj;

console.log({hasIn});

Arrays and objects Hard
A. false, false
B. false, true
C. true, true
D. TypeError, true

54 What is the result of the expression 0.1 + 0.2 === 0.3 and why?

JavaScript syntax and data types Hard
A. false, because of JavaScript's implicit type coercion to strings.
B. false, because of floating-point inaccuracies inherent in the IEEE 754 standard.
C. true, because the mathematical addition is correct.
D. true, because modern JavaScript engines have fixed this historical bug.

55 What will be logged to the console?

javascript
const obj = { key: 'value' };

switch (obj) {
case { key: 'value' }:
console.log('Case 1');
break;
case obj:
console.log('Case 2');
break;
default:
console.log('Default');
}

Control flow statements Hard
A. Case 1
B. Case 2
C. A SyntaxError will be thrown.
D. Default

56 You have an async function as an event listener for a checkbox. What is a potential issue with the event.preventDefault() call in this code?

javascript
checkbox.addEventListener('click', async (event) => {
const userIsSure = await confirmAction(); // An async function that shows a dialog
if (!userIsSure) {
event.preventDefault();
}
});

Basic event handling Hard
A. The browser may have already performed the default action (ticking the checkbox) before preventDefault() is called.
B. The event object is garbage collected after the await and cannot be accessed.
C. event.preventDefault() will throw an error because events cannot be handled asynchronously.
D. There are no potential issues; this is the standard way to handle async validation in events.

57 What is the result of attempting to instantiate an arrow function with the new keyword?

javascript
const ArrowConstructor = () => {
this.value = 42;
};

let instance;
try {
instance = new ArrowConstructor();
} catch (e) {
instance = e.name;
}

console.log(instance);

Functions and arrow functions Hard
A. An object { value: 42 }
B. TypeError
C. ReferenceError
D. undefined

58 What will be logged by console.log(result)?

javascript
const data = {
id: 1,
user: 'admin',
details: {
lastLogin: new Date(),
roles: ['read', 'write']
},
getSummary: function() { return ${this.user}; },
temp: undefined
};

const result = JSON.parse(JSON.stringify(data));

console.log(typeof result.details.lastLogin);

Arrays and objects Hard
A. object
B. function
C. string
D. undefined

59 What is the output of the following code snippet?

javascript
let x = '10';
let y = 10;

console.log(x == y);
console.log(x === y);
console.log(y == [10]);
console.log('1,0' == [1,0]);

Operators and expressions Hard
A. true, false, false, true
B. true, true, true, true
C. true, false, true, false
D. true, false, true, true