Multiple Choice Question for Grand JS Quiz



(Chap # 11 - comparison operators )


(1) Which operator checks for both value and type equality in JavaScript?

A) ==
B) !=
C) ===
D) =

Answer: C) ===

Explanation: The === operator checks if the values are equal and also of the same data type.



(2) What is the result of this condition: 5 == "5"?

A) true
B) false
C) undefined
D) NaN

Answer: A) true

Explanation: The == operator compares only values, so it converts the string "5" to a number before comparing.



(3) What is the result of 5 === "5"?

A) true
B) false
C) undefined
D) NaN

Answer: B) false

Explanation: === checks both value and type, and a number is not strictly equal to a string.



(4) Which comparison operator checks for inequality in both value and type?

A) !=
B) <>
C) ==!
D) !==

Answer: D) !==

Explanation: The !== operator returns true if the operands are not equal in value or type.



(5) What will this expression return: 10 > 5?

A) true
B) false
C) NaN
D) undefined

Answer: A) true

Explanation: Since 10 is greater than 5, the condition is true.



(6) What will be the output of this code?

var a = 7;
var b = "7";
alert(a == b);


A) true
B) false
C) undefined
D) NaN

Answer: A) true

Explanation: == compares values and performs type coercion, so both are treated as 7.



(7) Which comparison will return false?

A) "10" == 10
B) null == undefined
C) 5 === "5"
D) false == 0

Answer: C) 5 === "5"

Explanation: 5 === "5" is false because the type (number vs string) is not the same.



(8) Which of the following expressions is true?

A) 5 != "5"
B) 5 !== "5"
C) "hello" == "Hello"
D) 10 < 5

Answer: B) 5 !== "5"

Explanation: !== checks both value and type; 5 !== "5" is true because one is a number and the other is a string.



(9) What does this code output?

var x = 3;
var y = 3;
alert(x === y);


A) true
B) false
C) undefined
D) Error

Answer: A) true

Explanation: Both are numbers and equal in value and type, so x === y is true.



(10) What will be the result of: "apple" > "banana"?

A) true
B) false
C) NaN
D) undefined

Answer: B) false

Explanation: JavaScript compares strings lexicographically; "a" comes before "b", so "apple" is less than "banana".



(Chap # 12 - if & else if Statements )


(1) What is the purpose of the else block in an if...else statement?

A) To execute code only when the if condition is false
B) To define the condition for the if statement
C) To perform a task repeatedly
D) To check multiple conditions simultaneously

Answer: A) To execute code only when the if condition is false

Explanation: The else block is executed when the condition in the if statement evaluates to false.



(2) What is the purpose of the else if statement?

A) To add another condition to check when the first if condition fails
B) To define the if condition
C) To terminate the loop
D) To check for equality

Answer: A) To add another condition to check when the first if condition fails

Explanation: else if allows you to check additional conditions if the first if condition is false.



(3) What will be the output of this code?

var age = 25;
if (age >= 18) {
  alert("Adult");
} else {
  alert("Minor");
}


A) Adult
B) Minor
C) undefined
D) Error

Answer: A) Adult

Explanation: Since age is 25, which is greater than or equal to 18, the first block executes, displaying Adult.



(4) What will be the output of the following code?

var num = 0;
if (num) {
  alert("Non-zero value");
} else {
  alert("Zero");
}


A) Non-zero value
B) Zero
C) undefined
D) Error

Answer: B) Zero

Explanation: In JavaScript, 0 is considered a falsy value, so the else block will execute and display Zero.



(5) Which of the following is the correct use of else if?

A) if (condition1) { } else if (condition2) { }
B) if (condition1) { } else if { }
C) if condition { } else if (condition) { }
D) if condition { } else { } else if { }

Answer: A) if (condition1) { } else if (condition2) { }

Explanation: else if follows an if statement to check for additional conditions if the previous condition is false.



(6) What will happen if no else block is provided in an if...else statement?

A) The program will always execute the if block
B) The if block will be executed if the condition is true, and nothing will happen if false
C) The program will throw an error
D) The code will enter an infinite loop

Answer: B) The if block will be executed if the condition is true, and nothing will happen if false

Explanation: If no else block is provided, the code inside the if block will execute when the condition is true, and nothing happens when it is false.



(7) Which of the following is true about else if statements?

A) There can be multiple else if statements following an if
B) You can use only one else if in a program
C) else if is used for looping
D) else if always executes the first block

Answer: A) There can be multiple else if statements following an if

Explanation: You can have multiple else if statements, allowing for checking several conditions in sequence.



(8) What will this code display?

var score = 85;
if (score >= 90) {
  alert("A");
} else if (score >= 80) {
  alert("B");
} else {
  alert("C");
}


A) A
B) B
C) C
D) undefined

Answer: B) B

Explanation: Since score is 85, the second condition (score >= 80) is true, so "B" is displayed.



(9) What will the following code output?

var x = -1;
if (x > 0) {
  alert("Positive");
} else if (x < 0) {
  alert("Negative");
} else {
  alert("Zero");
}


A) Positive
B) Negative
C) Zero
D) undefined

Answer: B) Negative

Explanation: Since x is -1, the second else if condition (x < 0) is true, so "Negative" is displayed.



(10) What is the output of the following code?

var day = 3;
if (day == 1) {
  alert("Monday");
} else if (day == 2) {
  alert("Tuesday");
} else if (day == 3) {
  alert("Wednesday");
} else {
  alert("Weekend");
}


A) Monday
B) Tuesday
C) Wednesday
D) Weekend

Answer: C) Wednesday

Explanation: Since day is 3, the third condition (day == 3) is true, and "Wednesday" is displayed.



(Chap # 13 - Testing sets of Conditions)


(1) What is the logical AND (&&) operator used for?

A) To combine two or more conditions, all of which must be true
B) To combine two or more conditions, at least one must be true
C) To compare two strings
D) To negate a condition

Answer: A) To combine two or more conditions, all of which must be true

Explanation: The logical AND (&&) operator ensures that all combined conditions must evaluate to true for the block to execute.



(2) What does the logical OR (||) operator do?

A) Returns true only if all conditions are true
B) Returns false if any condition is false
C) Returns true if at least one condition is true
D) Compares two values

Answer: C) Returns true if at least one condition is true

Explanation: The OR (||) operator returns true as long as at least one of the conditions evaluates to true.



(3) What will the following condition return?

(5 > 3 && 8 > 2)


A) true
B) false
C) undefined
D) NaN

Answer: A) true

Explanation: Both conditions are true, so the result of the AND operation is also true.



(4) What will this code output?

var age = 20;
var hasID = true;
if (age >= 18 && hasID) { alert("Allowed"); }
else { alert("Not allowed"); }


A) Allowed
B) Not allowed
C) Error
D) undefined

Answer: A) Allowed

Explanation: Both conditions (age >= 18 and hasID is true) are met, so "Allowed" is displayed.



(5) Which operator would you use to test if either of two conditions is true?

A) &&
B) !!
C) ==
D) ||

Answer: D) ||

Explanation: The logical OR operator (||) is used when any one of multiple conditions being true should trigger the code.



(6) What does this condition evaluate to?

(false || true && false)


A) true
B) false
C) undefined
D) Error

Answer: B) false

Explanation: AND (&&) is evaluated before OR (||), so it becomes (false || false), which results in false.



(7) What will be the output?

var username = "admin";
var password = "1234";
if (username === "admin" && password === "1234") { alert("Login successful"); }
else { alert("Login failed"); }


A) Login failed
B) Login successful
C) undefined
D) Error

Answer: B) Login successful

Explanation: Both conditions match the expected values, so the if block is executed.



(8) Which of the following is true for logical operators?

A) && has higher precedence than ||
B) || has higher precedence than &&
C) == has higher precedence than &&
D) != has the lowest precedence

Answer: A) && has higher precedence than ||

Explanation: In JavaScript, the logical AND (&&) is evaluated before the logical OR (||) in compound conditions.



(9) What happens if you combine && and || without parentheses?

A) JavaScript throws an error
B) Parentheses are always required
C) JavaScript uses precedence rules to evaluate the expression
D) All conditions are skipped

Answer: C) JavaScript uses precedence rules to evaluate the expression

Explanation: JavaScript follows operator precedence, evaluating && before || when parentheses are not used.



(10) What does the following code output?

var loggedIn = false;
var hasToken = true;
if (loggedIn || hasToken) { alert("Access granted"); }
else { alert("Access denied"); }


A) Access denied
B) Access granted
C) Error
D) undefined

Answer: B) Access granted

Explanation: Even though loggedIn is false, hasToken is true, making the OR condition true.



(Chap # 14 - if statements nested)


(1) What is a nested if statement?

A) An if statement placed inside a loop
B) An if statement placed inside another if statement
C) An if statement used with a switch
D) An if statement outside any function

Answer: B) An if statement placed inside another if statement

Explanation: A nested if is when one if statement exists within the block of another if statement to allow more precise condition checking.



(2) When does the inner if statement run in a nested if?

A) Always
B) Only if the outer if condition is false
C) Only if the outer if condition is true
D) If there's no else

Answer: C) Only if the outer if condition is true

Explanation: The inner if block will only execute if the outer if condition evaluates to true.



(3) What will this code display?

var user = "admin";
var pass = "123";
if (user === "admin") {
  if (pass === "123") { alert("Welcome"); }
}


A) Welcome
B) Nothing
C) Error
D) admin

Answer: A) Welcome

Explanation: Both the outer and inner if conditions are true, so the alert executes.



(4) Why might you use nested if statements?

A) To loop through conditions
B) To handle multiple variables in one condition
C) To create more specific decision branches
D) To replace switch statements

Answer: C) To create more specific decision branches

Explanation: Nested if statements let you make more granular checks within larger condition blocks.



(5) What will this code output?

var temp = 30;
if (temp > 20) {
  if (temp < 40) { alert("Comfortable"); }
}


A) Comfortable
B) Error
C) Nothing
D) 30

Answer: A) Comfortable

Explanation: Both conditions are true (30 > 20 and 30 < 40), so "Comfortable" is shown.



(6) What happens if the outer if condition is false in a nested if?

A) Inner if still runs
B) Inner if is ignored
C) Inner if throws an error
D) Else runs instead

Answer: B) Inner if is ignored

Explanation: The inner if is never evaluated unless the outer condition is true.



(7) Choose the correct structure of nested if statements.

A) if {} if () {}
B) if () {} else if () {}
C) if (condition1) { if (condition2) { } }
D) if (condition1) else (condition2)

Answer: C) if (condition1) { if (condition2) { } }

Explanation: This is the correct syntax for nesting one if statement inside another.



(8) What will this code output?

var a = 10;
if (a > 5) {
  if (a < 15) { alert("Between 5 and 15"); }
}


A) 10
B) Between 5 and 15
C) undefined
D) Error

Answer: B) Between 5 and 15

Explanation: Since a = 10, both conditions are satisfied and the alert displays the message.



(9) Can you use else inside a nested if block?

A) Yes
B) No
C) Only with switch
D) Only outside functions

Answer: A) Yes

Explanation: You can include else blocks inside nested if statements for additional logic.



(10) What is the best way to keep nested if statements readable?

A) Use semicolons everywhere
B) Avoid indentation
C) Use clear formatting and indentation
D) Use while loops

Answer: C) Use clear formatting and indentation

Explanation: Proper indentation helps maintain readability and structure when nesting if statements.



(Chap # 15 - Arrays)


(1) What is an array in JavaScript?

A) A variable that stores a single value
B) A function that loops
C) A special variable that can hold multiple values
D) A condition block

Answer: C) A special variable that can hold multiple values

Explanation: Arrays are used to store multiple values in a single variable, which can be accessed using indexes.



(2) How are array elements accessed in JavaScript?

A) Using parentheses
B) Using dot notation
C) Using square brackets with an index
D) Using curly braces

Answer: C) Using square brackets with an index

Explanation: Array elements are accessed using square brackets and the index number, starting from 0.



(3) What will this code output?

var colors = ["red", "blue", "green"];
alert(colors[1]);


A) red
B) blue
C) green
D) undefined

Answer: B) blue

Explanation: Array indexing starts from 0, so index 1 refers to "blue".



(4) What will happen if you access an array index that doesn’t exist?

A) Returns null
B) Throws an error
C) Returns undefined
D) Returns false

Answer: C) Returns undefined

Explanation: If the index does not exist in the array, JavaScript returns undefined.



(5) How do you find the number of elements in an array?

A) .count
B) .length
C) .size()
D) .total

Answer: B) .length

Explanation: The length property gives the number of elements in an array.



(6) What will this code output?

var fruits = ["apple", "banana"];
fruits[2] = "orange";
alert(fruits.length);


A) 2
B) 3
C) 1
D) Error

Answer: B) 3

Explanation: Adding an element at index 2 extends the array to 3 elements, even if index 2 was initially empty.



(7) What will this code output?

var items = [];
items[5] = "hello";
alert(items.length);


A) 1
B) 6
C) 5
D) Error

Answer: B) 6

Explanation: The array now has 6 positions (0 through 5), even though only index 5 has a value.



(8) Which method adds a new item to the end of an array?

A) push()
B) pop()
C) shift()
D) unshift()

Answer: A) push()

Explanation: push() adds a new element at the end of the array.



(9) What does the pop() method do?

A) Adds a new element at the start
B) Removes the first element
C) Removes the last element
D) Adds an element at the end

Answer: C) Removes the last element

Explanation: The pop() method removes the last element from an array and returns it.



(10) Which method adds an element at the beginning of an array?

A) shift()
B) pop()
C) unshift()
D) splice()

Answer: C) unshift()

Explanation: unshift() inserts a new element at the beginning of the array.



(Chap # 16 - Adding or removing elements from arrays)


(1) What is an array in JavaScript?

A) A variable that stores a single value
B) A function that loops
C) A special variable that can hold multiple values
D) A condition block

Answer: C) A special variable that can hold multiple values

Explanation: Arrays are used to store multiple values in a single variable, which can be accessed using indexes.



(2) How are array elements accessed in JavaScript?

A) Using parentheses
B) Using dot notation
C) Using square brackets with an index
D) Using curly braces

Answer: C) Using square brackets with an index

Explanation: Array elements are accessed using square brackets and the index number, starting from 0.



(3) What will this code output?

var colors = ["red", "blue", "green"];
alert(colors[1]);


A) red
B) blue
C) green
D) undefined

Answer: B) blue

Explanation: Array indexing starts from 0, so index 1 refers to "blue".



(4) What will happen if you access an array index that doesn’t exist?

A) Returns null
B) Throws an error
C) Returns undefined
D) Returns false

Answer: C) Returns undefined

Explanation: If the index does not exist in the array, JavaScript returns undefined.



(5) How do you find the number of elements in an array?

A) .count
B) .length
C) .size()
D) .total

Answer: B) .length

Explanation: The length property gives the number of elements in an array.



(6) What will this code output?

var fruits = ["apple", "banana"];
fruits[2] = "orange";
alert(fruits.length);


A) 2
B) 3
C) 1
D) Error

Answer: B) 3

Explanation: Adding an element at index 2 extends the array to 3 elements, even if index 2 was initially empty.



(7) What will this code output?

var items = [];
items[5] = "hello";
alert(items.length);


A) 1
B) 6
C) 5
D) Error

Answer: B) 6

Explanation: The array now has 6 positions (0 through 5), even though only index 5 has a value.



(8) Which method adds a new item to the end of an array?

A) push()
B) pop()
C) shift()
D) unshift()

Answer: A) push()

Explanation: push() adds a new element at the end of the array.



(9) What does the pop() method do?

A) Adds a new element at the start
B) Removes the first element
C) Removes the last element
D) Adds an element at the end

Answer: C) Removes the last element

Explanation: The pop() method removes the last element from an array and returns it.



(10) Which method adds an element at the beginning of an array?

A) shift()
B) pop()
C) unshift()
D) splice()

Answer: C) unshift()

Explanation: unshift() inserts a new element at the beginning of the array.



(Chap # 17 - Removing, inserting and extracting elements)


(1) Which array method is used to remove elements from a specific position?

A) slice()
B) splice()
C) push()
D) pop()

Answer: B) splice()

Explanation: splice() can remove, replace, or insert elements at a specific index in an array.



(2) What will this code output?

var arr = [1, 2, 3, 4];
arr.splice(1, 2);
alert(arr);


A) 1,4
B) 1,2
C) 2,3
D) 1,2,4

Answer: A) 1,4

Explanation: splice(1, 2) removes two elements starting from index 1, which are 2 and 3.



(3) What is the purpose of the slice() method?

A) To remove elements from the array
B) To insert new elements
C) To return a portion of the array
D) To sort the array

Answer: C) To return a portion of the array

Explanation: slice() returns a shallow copy of a portion of an array into a new array object without modifying the original array.



(4) What will this code return?

var fruits = ["apple", "banana", "cherry", "date"];
var result = fruits.slice(1, 3);
alert(result);


A) banana,cherry
B) banana,cherry,date
C) apple,banana
D) cherry,date

Answer: A) banana,cherry

Explanation: slice(1, 3) returns elements at index 1 and 2; index 3 is not included.



(5) Which of the following methods does NOT modify the original array?

A) splice()
B) push()
C) slice()
D) pop()

Answer: C) slice()

Explanation: slice() returns a new array and leaves the original array unchanged.



(6) What will be the result of the following?

var numbers = [1, 2, 3, 4];
numbers.splice(2, 0, 99);
alert(numbers);


A) 1,2,99,3,4
B) 1,2,3,99,4
C) 1,99,2,3,4
D) 99,1,2,3,4

Answer: A) 1,2,99,3,4

Explanation: splice(2, 0, 99) inserts 99 at index 2 without removing any element.



(7) What will the following code output?

var items = ["pen", "pencil", "eraser", "sharpener"];
var part = items.slice(0, 2);
alert(part);


A) pen,eraser
B) pencil,eraser
C) pen,pencil
D) sharpener,eraser

Answer: C) pen,pencil

Explanation: slice(0, 2) returns items from index 0 and 1 only.



(8) What will this code output?

var letters = ["a", "b", "c", "d"];
letters.splice(1, 1, "z");
alert(letters);


A) a,z,c,d
B) a,b,c,d,z
C) z,a,b,c,d
D) a,z,b,c,d

Answer: A) a,z,c,d

Explanation: splice(1,1,"z") removes "b" and replaces it with "z".



(9) What will this code return?

var data = [1, 2, 3, 4, 5];
var sliced = data.slice(2);
alert(sliced);


A) 1,2
B) 3,4,5
C) 2,3,4
D) 4,5

Answer: B) 3,4,5

Explanation: slice(2) returns elements starting from index 2 till the end.



(10) What happens when you use splice() with only one argument?

var test = ["x", "y", "z"];
test.splice(1);
alert(test);


A) x
B) y,z
C) x,y,z
D) x,z

Answer: A) x

Explanation: splice(1) removes all elements starting from index 1, so only "x" remains.



(Chap # 18 - For loops)


(1) What is the correct syntax of a for loop in JavaScript?

A) for (i <= 5; i++)
B) for (i = 0; i <= 5; i++)
C) for i = 0 to 5
D) loop (i = 0; i < 5; i++)

Answer: B) for (i = 0; i <= 5; i++)

Explanation: The correct syntax for a for loop includes initialization, condition, and increment separated by semicolons.



(2) What will the following code output?

for (var i = 0; i < 3; i++) {
alert(i);
}


A) 0 1 2
B) 1 2 3
C) 0 1 2 3
D) 1 2

Answer: A) 0 1 2

Explanation: The loop starts at 0 and runs while i is less than 3, so it alerts 0, 1, and 2.



(3) How many times will this loop run?

for (var i = 1; i <= 4; i++) {
console.log(i);
}


A) 5 times
B) 3 times
C) 4 times
D) Infinite times

Answer: C) 4 times

Explanation: It runs for i = 1, 2, 3, and 4 — a total of 4 times.



(4) What does the loop control variable do?

A) It determines if a loop is infinite
B) It performs calculations
C) It tracks how many times the loop has run
D) It stops the loop manually

Answer: C) It tracks how many times the loop has run

Explanation: The loop control variable is used to determine the number of iterations.



(5) What will be the value of i after this loop finishes?

for (var i = 0; i < 3; i++) {
console.log(i);
}


A) 2
B) 3
C) 0
D) 1

Answer: B) 3

Explanation: The loop runs until i becomes 3. After the last iteration (i=2), i is incremented to 3 and the condition fails.



(6) What will be the output of the following code?

var result = 0;
for (var i = 1; i <= 5; i++) {
result += i;
}
console.log(result);


A) 15
B) 5
C) 10
D) 0

Answer: A) 15

Explanation: The loop adds 1 + 2 + 3 + 4 + 5, resulting in 15.



(7) What will this loop print?

var i = 0;
for (i = 0; i < 2; i++) {
console.log(i * 2);
}


A) 0 2
B) 0 4
C) 2 4
D) 2 6

Answer: A) 0 2

Explanation: The loop prints i * 2 for i = 0 and i = 1, which are 0 and 2 respectively.



(8) Which of the following is the correct way to stop a for loop before it completes all iterations?

A) break
B) continue
C) return
D) exit

Answer: A) break

Explanation: The break statement is used to exit the loop prematurely.



(9) Which of the following will result in an infinite loop?

A) for (var i = 0; i < 5; i++) {}
B) for (var i = 5; i < 0; i++) {}
C) for (var i = 0; i < 5; i--) {}
D) for (var i = 0; i > 5; i++) {}

Answer: C) for (var i = 0; i < 5; i--) {}

Explanation: The loop decrements i, which makes it an infinite loop because the condition will always be true.



(10) What is the output of the following code?

for (var i = 0; i < 3; i++) {
if (i === 1) continue;
console.log(i);
}


A) 0 1 2
B) 0 2
C) 1 2
D) 0 1

Answer: B) 0 2

Explanation: The continue statement skips the iteration when i equals 1, so it only prints 0 and 2.



(Chap # 19 - for loops: flags, Booleans, array length, and breaks)


(1) What is the purpose of using a Boolean flag in a for loop?

A) To reset the loop
B) To toggle between even and odd numbers
C) To track whether a condition has been met
D) To change the loop counter

Answer: C) To track whether a condition has been met

Explanation: A Boolean flag is used to signal whether a specific condition occurred during the loop.



(2) What is the output of this code?

var found = false;
var list = [3, 7, 9, 5];
for (var i = 0; i < list.length; i++) {
if (list[i] === 9) {
found = true;
break;
}
}
console.log(found);


A) true
B) false
C) 9
D) undefined

Answer: A) true

Explanation: The loop finds the value 9 in the array and sets found to true before breaking.



(3) What does the `break` statement do in a loop?

A) Pauses the loop
B) Exits the loop completely
C) Skips one iteration
D) Repeats the last iteration

Answer: B) Exits the loop completely

Explanation: The break statement immediately exits the loop regardless of the loop condition.



(4) What is the output of this code?

var nums = [4, 8, 12];
for (var i = 0; i < nums.length; i++) {
console.log(nums[i]);
}


A) 0 1 2
B) 4 8 12
C) 3
D) undefined

Answer: B) 4 8 12

Explanation: The loop prints each element of the array using the array's length.



(5) Which method is used to get the number of elements in an array?

A) .count
B) .size()
C) .length
D) .elements()

Answer: C) .length

Explanation: The `.length` property returns the number of elements in an array.



(6) When using a Boolean flag, what is its typical initial value?

A) true
B) false
C) null
D) 0

Answer: B) false

Explanation: A Boolean flag usually starts as false, indicating the condition hasn’t been met yet.



(7) What does the following code output?

var found = false;
var arr = [2, 4, 6, 8];
for (var i = 0; i < arr.length; i++) {
if (arr[i] === 5) {
found = true;
break;
}
}
console.log(found);


A) true
B) false
C) 5
D) undefined

Answer: B) false

Explanation: 5 is not in the array, so found remains false throughout the loop.



(8) What is the output of this loop?

var flag = false;
for (var i = 0; i < 3; i++) {
if (i === 2) {
flag = true;
}
}
console.log(flag);


A) true
B) false
C) 2
D) undefined

Answer: A) true

Explanation: When i equals 2, the flag is set to true before the loop ends.



(9) Which keyword skips the current iteration in a loop but continues with the next one?

A) break
B) return
C) continue
D) skip

Answer: C) continue

Explanation: The continue statement skips the current iteration and moves to the next loop cycle.



(10) Which of the following describes a use-case for combining a loop with a Boolean flag?

A) Printing all values of an array
B) Creating an array
C) Searching for a value
D) Sorting numbers

Answer: C) Searching for a value

Explanation: Boolean flags are commonly used to indicate if a searched value was found within a loop.



(Chap # 20 - Nested for-loops)


(1) What is a nested for loop?

A) A loop inside an if statement
B) A loop that only runs once
C) A loop placed inside another loop
D) A loop that calls a function

Answer: C) A loop placed inside another loop

Explanation: A nested loop is when one loop runs inside the body of another loop, allowing iteration over multidimensional data.



(2) How many times will the inner loop run in this code?

for (var i = 0; i < 2; i++) {
for (var j = 0; j < 3; j++) {
console.log(i, j);
}
}


A) 2
B) 3
C) 5
D) 6

Answer: D) 6

Explanation: The outer loop runs 2 times and the inner loop runs 3 times for each outer iteration (2 * 3 = 6).



(3) What is printed by the following code?

for (var i = 1; i <= 2; i++) {
for (var j = 1; j <= 2; j++) {
document.write(i * j + " ");
}
}


A) 1 2 2 4
B) 1 2 3 4
C) 2 4 2 4
D) 1 1 2 2

Answer: A) 1 2 2 4

Explanation: The inner loop multiplies i and j. When i = 1 → 1*1, 1*2; When i = 2 → 2*1, 2*2.



(4) What is the primary use of nested loops in JavaScript?

A) Repeating a block infinitely
B) Checking browser compatibility
C) Iterating over multi-dimensional arrays
D) Performing HTTP requests

Answer: C) Iterating over multi-dimensional arrays

Explanation: Nested loops are ideal for handling arrays of arrays or tables where each row contains multiple values.



(5) What will be the final value of `sum` after this code?

var sum = 0;
for (var i = 0; i < 2; i++) {
for (var j = 0; j < 2; j++) {
sum += i + j;
}
}
console.log(sum);


A) 2
B) 4
C) 6
D) 8

Answer: C) 6

Explanation: i and j take values (0,0), (0,1), (1,0), and (1,1). Their sums: 0+0, 0+1, 1+0, 1+1 = 0+1+1+2 = 4.



(6) How do you access elements in a 2D array using nested loops?

A) array[i + j]
B) array[j][i]
C) array[i][j]
D) array(i, j)

Answer: C) array[i][j]

Explanation: 2D arrays are accessed using two indices — the row and the column — in the form array[i][j].



(7) Which loop runs first in a nested for loop?

A) The inner loop
B) The outer loop
C) They run at the same time
D) The one with the smaller counter

Answer: B) The outer loop

Explanation: The outer loop initiates the first iteration and calls the entire inner loop on each of its cycles.



(8) How many times will "Hi" be printed?

for (var i = 0; i < 3; i++) {
for (var j = 0; j < 2; j++) {
console.log("Hi");
}
}


A) 2
B) 3
C) 5
D) 6

Answer: D) 6

Explanation: Outer loop runs 3 times, inner loop 2 times per cycle → 3 * 2 = 6.



(9) What will this code display in the console?

for (var i = 0; i < 2; i++) {
for (var j = 0; j < 2; j++) {
console.log(i + "," + j);
}
}


A) 0,0 0,1 1,0 1,1
B) 0,1 1,2
C) 1,0 2,1
D) undefined

Answer: A) 0,0 0,1 1,0 1,1

Explanation: Each iteration logs the current i and j values, showing all combinations.



(10) What is a potential drawback of deeply nested loops?

A) They execute in parallel
B) They are faster
C) They reduce readability and performance
D) They automatically use recursion

Answer: C) They reduce readability and performance

Explanation: Deep nesting can make code hard to follow and may lead to inefficient execution.




Prepared by "Ismail Shah"