Unit 4: JavaScript Fundamentals - Practice Quiz

CSE326 — Internet Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What is JavaScript primarily used for in web development?

Introduction to JavaScript Easy
A. Adding interactivity to web pages
B. Defining the structure of web pages
C. Managing files on a web server
D. Styling the appearance of web pages

2 Which HTML element is used to include JavaScript code in a web page?

Introduction to JavaScript Easy
A. <script>
B. <meta>
C. <link>
D. <style>

3 Which keyword declares a block-scoped variable that can be reassigned?

Variables and Constants Easy
A. define
B. let
C. const
D. static

4 Which keyword declares a block-scoped binding that cannot be reassigned?

Variables and Constants Easy
A. var
B. value
C. let
D. const

5 What is the data type of the value "JavaScript"?

Data Types Easy
A. Boolean
B. String
C. Number
D. Undefined

6 Which JavaScript value represents an intentionally empty or absent value?

Data Types Easy
A. 0
B. undefined
C. null
D. false

7 Which operator checks both value and data type for equality?

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

8 What is the result of the expression 10 % 3?

Operators and Expressions Easy
A. 1
B. 3
C. 3.33
D. 0

9 Which property returns the number of characters in a string named text?

Strings and String Methods Easy
A. text.count
B. text.length
C. text.size
D. text.width

10 Which method converts the string name to uppercase?

Strings and String Methods Easy
A. name.upperCase()
B. name.toUpperCase()
C. name.makeUpper()
D. name.toLowerCase()

11 Which pair contains the two Boolean values in JavaScript?

Numbers and Boolean Values Easy
A. 1 and 0
B. true and false
C. on and off
D. yes and no

12 What does Number("25") return?

Numbers and Boolean Values Easy
A. The number 25
B. The value undefined
C. The Boolean true
D. The string "25"

13 Which statement runs a block of code only when a specified condition is true?

Conditional Statements Easy
A. if statement
B. return statement
C. break statement
D. for statement

14 Which keyword provides an alternative block when an if condition is false?

Conditional Statements Easy
A. else
B. case
C. then
D. default

15 Which keyword is commonly used to declare a JavaScript function?

Functions Easy
A. define
B. method
C. procedure
D. function

16 Which keyword sends a value back from a function?

Functions Easy
A. output
B. yield
C. break
D. return

17 Where can a variable declared inside a function normally be accessed?

Scope Easy
A. Only inside HTML
B. Only in another function
C. Anywhere in the program
D. Inside that function

18 Which method adds an element to the end of an array?

Arrays and Array Operations Easy
A. slice()
B. pop()
C. shift()
D. push()

19 Which index accesses the first element of a JavaScript array named items?

Arrays and Array Operations Easy
A. items[first]
B. items[0]
C. items[1]
D. items[-1]

20 How can the name property of an object called student be accessed using dot notation?

Objects and Object Manipulation Easy
A. student->name
B. student(name)
C. student.name
D. student::name

21 What happens when a browser encounters <script defer src="app.js"></script> in the document <head>?

Introduction to JavaScript Medium
A. The script runs only after all images have loaded
B. The script downloads in parallel and runs after HTML parsing
C. The script blocks HTML parsing until execution finishes
D. The script downloads only after HTML parsing finishes

22 Given const user = { name: "Ana" };, which statement executes without causing an error?

Variables and Constants Medium
A. user = { name: "Ben" };
B. const user = { name: "Ben" };
C. user.name = "Ben";
D. user = null;

23 What is printed by console.log(typeof null, Array.isArray([]));?

Data Types Medium
A. null true
B. object false
C. object true
D. undefined false

24 What is the output of console.log(0 ?? 5, 0 || 5);?

Operators and Expressions Medium
A. 0 5
B. 5 0
C. 0 0
D. 5 5

25 What value is produced by the expression 2 + 3 * 4 ** 2?

Operators and Expressions Medium
A. 196
B. 50
C. 66
D. 80

26 What does "JavaScript".slice(4, 10) return?

Strings and String Methods Medium
A. "Scrip"
B. "cript"
C. "Script"
D. "JavaS"

27 What is printed by let text = " web "; text.trim().toUpperCase(); console.log(text.length);?

Strings and String Methods Medium
A. 5
B. 4
C. 0
D. 3

28 What is printed by console.log(Number.isNaN("hello"), isNaN("hello"));?

Numbers and Boolean Values Medium
A. true false
B. false true
C. false false
D. true true

29 What is the value of [0, 1, "", "JS", null].filter(Boolean).length?

Numbers and Boolean Values Medium
A. 1
B. 4
C. 2
D. 3

30 What is printed by const age = 20; const hasID = false; if (age >= 18 && hasID) { console.log("Enter"); } else if (age >= 18) { console.log("ID required"); } else { console.log("Too young"); }?

Conditional Statements Medium
A. ID required
B. Enter
C. Nothing is printed
D. Too young

31 What is printed by const value = "2"; switch (value) { case 2: console.log("number"); break; case "2": console.log("string"); break; default: console.log("other"); }?

Conditional Statements Medium
A. number
B. number string
C. string
D. other

32 What is returned by function total(price, tax = price * 0.1) { return price + tax; } total(200);?

Functions Medium
A. 200
B. 210
C. NaN
D. 220

33 What is the result of function twice(fn, value) { return fn(fn(value)); } twice(x => x + 3, 4);?

Functions Medium
A. 7
B. 10
C. 11
D. 14

34 What is printed by let count = 10; { let count = 20; console.log(count); } console.log(count);?

Scope Medium
A. 20 followed by 10
B. 10 followed by 10
C. 20 followed by 20
D. 10 followed by 20

35 What is returned by function makeFunctions() { const result = []; for (let i = 0; i < 3; i++) result.push(() => i); return result; } makeFunctions().map(fn => fn());?

Scope Medium
A. [3, 3, 3]
B. [0, 0, 0]
C. [1, 2, 3]
D. [0, 1, 2]

36 After const values = [1, 2, 3, 4]; const removed = values.splice(1, 2, 9);, what are values and removed?

Arrays and Array Operations Medium
A. values is [1, 2, 9, 4]; removed is [3]
B. values is [1, 9, 3, 4]; removed is [2]
C. values is [1, 4]; removed is [2, 3, 9]
D. values is [1, 9, 4]; removed is [2, 3]

37 What is the result of [1, 2, 3, 4].filter(n => n % 2 === 0).map(n => n * n)?

Arrays and Array Operations Medium
A. [1, 4, 9, 16]
B. [2, 4]
C. [4, 16]
D. [1, 9]

38 What value is returned by [2, 3, 4].reduce((product, n) => product * n, 1)?

Arrays and Array Operations Medium
A. 14
B. 24
C. 10
D. 20

39 What is printed by const original = { info: { score: 1 } }; const copy = { ...original }; copy.info.score = 5; console.log(original.info.score);?

Objects and Object Manipulation Medium
A. A TypeError occurs
B. 1
C. 5
D. undefined

40 What is printed by const item = { name: "Pen", price: 10 }; const { name: label, stock = 0 } = item; console.log(label, stock);?

Objects and Object Manipulation Medium
A. Pen undefined
B. Pen 0
C. name 0
D. label 10

41 What is printed by the following JavaScript code?

JAVASCRIPT
console.log("A");
Promise.resolve().then(() => console.log("B"));
queueMicrotask(() => console.log("C"));
setTimeout(() => console.log("E"), 0);
console.log("D");

Introduction to JavaScript Hard
A. A D C B E
B. A D E B C
C. A B C D E
D. A D B C E

42 What happens when this code executes?

JAVASCRIPT
let x = 1;

function test() {
  console.log(x);
  let x = 2;
}

test();

Variables and Constants Hard
A. It prints 1.
B. It prints 2.
C. It prints undefined.
D. It throws a ReferenceError.

43 In an ordinary non-strict script, what is printed?

JAVASCRIPT
const config = Object.freeze({
  nested: { value: 1 }
});

config.nested.value = 2;
config.nested = { value: 3 };
console.log(config.nested.value);

Variables and Constants Hard
A. 3
B. 2
C. 1
D. undefined

44 What is the value of result?

JAVASCRIPT
const value = Object.create(null);
const result = [
  typeof value,
  value instanceof Object,
  Object.getPrototypeOf(value)
];

Data Types Hard
A. ["object", false, Object.prototype]
B. ["null", false, null]
C. ["object", false, null]
D. ["object", true, null]

45 What are the final values of x and y?

JAVASCRIPT
let x = 0;
const y = x++ || ++x;

Operators and Expressions Hard
A. x is 2 and y is 1.
B. x is 1 and y is 0.
C. x is 1 and y is 1.
D. x is 2 and y is 2.

46 What happens when an ECMAScript parser processes this statement?

JAVASCRIPT
const result = null ?? false || true;

Operators and Expressions Hard
A. result becomes false.
B. result becomes true.
C. result becomes null.
D. A SyntaxError occurs.

47 What is the value of result?

JAVASCRIPT
const text = "A😀B";
const result = [
  text.length,
  text.slice(1, 3),
  Array.from(text).length
];

Strings and String Methods Hard
A. [3, "😀", 4]
B. [3, "😀", 3]
C. [4, "😀B", 3]
D. [4, "😀", 3]

48 What does the following expression return?

JAVASCRIPT
"aba".replace(/a/g, (match, offset) => String(offset))

Strings and String Methods Hard
A. "ab2"
B. "0b2"
C. "0ba"
D. "1b3"

49 What is the result of this comparison?

JAVASCRIPT
Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2

Numbers and Boolean Values Hard
A. true
B. undefined
C. A RangeError
D. false

50 What is the value of result?

JAVASCRIPT
const result = [
  Object.is(NaN, NaN),
  Object.is(0, -0),
  Boolean(new Boolean(false))
];

Numbers and Boolean Values Hard
A. [false, false, true]
B. [false, true, false]
C. [true, false, true]
D. [true, true, false]

51 What is the final value of result?

JAVASCRIPT
let result = "";

switch (2) {
  case 1:
    result += "A";
  case 2:
    result += "B";
  default:
    result += "C";
  case 3:
    result += "D";
}

Conditional Statements Hard
A. "ABCD"
B. "BC"
C. "BCD"
D. "BD"

52 What is the value of result after this code runs?

JAVASCRIPT
let a = 0;
let b = 1;
let result;

if (a = b && 0 || 2) {
  result = [a, "T"];
} else {
  result = [a, "F"];
}

Conditional Statements Hard
A. [1, "T"]
B. [2, "F"]
C. [2, "T"]
D. [0, "F"]

53 What happens when f() is called?

JAVASCRIPT
let x = 10;

function f(a = x, x = 20) {
  return [a, x];
}

f();

Functions Hard
A. It returns [undefined, 20].
B. It throws a ReferenceError.
C. It returns [10, 20].
D. It returns [20, 20].

54 What does the final expression return?

JAVASCRIPT
const obj = {
  x: 5,
  make() {
    return () => this.x;
  }
};

const fn = obj.make();
const other = { x: 9, fn };
other.fn();

Functions Hard
A. undefined
B. A TypeError
C. 5
D. 9

55 What string is produced?

JAVASCRIPT
const callbacks = [];

for (var i = 0; i < 3; i++) {
  let j = i;
  callbacks.push(() => `${i}:${j}`);
}

callbacks.map(fn => fn()).join(",");

Scope Hard
A. "3:3,3:3,3:3"
B. "0:3,1:3,2:3"
C. "3:0,3:1,3:2"
D. "0:0,1:1,2:2"

56 What is the value of result?

JAVASCRIPT
const source = [1, , 3];
const mapped = source.map(x => x * 2);
const result = [
  mapped.length,
  0 in mapped,
  1 in mapped,
  2 in mapped
];

Arrays and Array Operations Hard
A. [3, false, false, true]
B. [3, true, true, true]
C. [2, true, false, true]
D. [3, true, false, true]

57 After this code runs, what are removed and values?

JAVASCRIPT
const values = [1, 2, 3, 4];
const removed = values.splice(1, 2, 9, 8, 7);

Arrays and Array Operations Hard
A. removed = [2, 3, 4], values = [1, 9, 8, 7]
B. removed = [9, 8], values = [1, 7, 3, 4]
C. removed = [2, 3], values = [1, 9, 8, 7]
D. removed = [2, 3], values = [1, 9, 8, 7, 4]

58 What is the value of result?

JAVASCRIPT
const symbol = Symbol("1");
const object = {
  [1]: "first",
  "1": "second",
  [symbol]: "third"
};

const result = [
  Object.keys(object),
  object[1],
  Object.getOwnPropertySymbols(object).length
];

Objects and Object Manipulation Hard
A. [["1"], "second", 1]
B. [["1", symbol], "second", 1]
C. [["1", "1"], "first", 1]
D. [["1"], "first", 0]

59 What is the value of result?

JAVASCRIPT
const source = {
  get x() {
    return 2;
  }
};

const target = { ...source };
const descriptor = Object.getOwnPropertyDescriptor(target, "x");
const result = [target.x, typeof descriptor.get, descriptor.writable];

Objects and Object Manipulation Hard
A. [2, "undefined", true]
B. [2, "function", false]
C. [2, "function", true]
D. [undefined, "undefined", true]

60 What is the value of result?

JAVASCRIPT
const prototype = { x: 1 };
const object = Object.create(prototype);
object.x = 2;
delete object.x;

const result = [
  "x" in object,
  Object.hasOwn(object, "x"),
  object.x
];

Objects and Object Manipulation Hard
A. [false, false, undefined]
B. [true, true, 2]
C. [false, true, 1]
D. [true, false, 1]