Answer: C) ===
Explanation: The === operator checks if the values are equal and also of the same data type.
Answer: A) true
Explanation: The == operator compares only values, so it converts the string "5" to a number before comparing.
Answer: B) false
Explanation: === checks both value and type, and a number is not strictly equal to a string.
Answer: D) !==
Explanation: The !== operator returns true if the operands are not equal in value or type.
Answer: A) true
Explanation: Since 10 is greater than 5, the condition is true.
Answer: A) true
Explanation: == compares values and performs type coercion, so both are treated as 7.
Answer: C) 5 === "5"
Explanation: 5 === "5" is false because the type (number vs string) is not the same.
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.
Answer: A) true
Explanation: Both are numbers and equal in value and type, so x === y is true.
Answer: B) false
Explanation: JavaScript compares strings lexicographically; "a" comes before "b", so "apple" is less than "banana".
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.
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.
var age = 25;
if (age >= 18) {
alert("Adult");
} else {
alert("Minor");
}
Answer: A) Adult
Explanation: Since age is 25, which is greater than or equal to 18, the first block executes, displaying Adult.
var num = 0;
if (num) {
alert("Non-zero value");
} else {
alert("Zero");
}
Answer: B) Zero
Explanation: In JavaScript, 0 is considered a falsy value, so the else block will execute and display Zero.
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.
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.
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.
var score = 85;
if (score >= 90) {
alert("A");
} else if (score >= 80) {
alert("B");
} else {
alert("C");
}
Answer: B) B
Explanation: Since score is 85, the second condition (score >= 80) is true, so "B" is displayed.
var x = -1;
if (x > 0) {
alert("Positive");
} else if (x < 0) {
alert("Negative");
} else {
alert("Zero");
}
Answer: B) Negative
Explanation: Since x is -1, the second else if condition (x < 0) is true, so "Negative" is displayed.
var day = 3;
if (day == 1) {
alert("Monday");
} else if (day == 2) {
alert("Tuesday");
} else if (day == 3) {
alert("Wednesday");
} else {
alert("Weekend");
}
Answer: C) Wednesday
Explanation: Since day is 3, the third condition (day == 3) is true, and "Wednesday" is displayed.
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.
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.
Answer: A) true
Explanation: Both conditions are true, so the result of the AND operation is also true.
Answer: A) Allowed
Explanation: Both conditions (age >= 18 and hasID is true) are met, so "Allowed" is displayed.
Answer: D) ||
Explanation: The logical OR operator (||) is used when any one of multiple conditions being true should trigger the code.
Answer: B) false
Explanation: AND (&&) is evaluated before OR (||), so it becomes (false || false), which results in false.
Answer: B) Login successful
Explanation: Both conditions match the expected values, so the if block is executed.
Answer: A) && has higher precedence than ||
Explanation: In JavaScript, the logical AND (&&) is evaluated before the logical OR (||) in compound conditions.
Answer: C) JavaScript uses precedence rules to evaluate the expression
Explanation: JavaScript follows operator precedence, evaluating && before || when parentheses are not used.
Answer: B) Access granted
Explanation: Even though loggedIn is false, hasToken is true, making the OR condition true.
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.
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.
Answer: A) Welcome
Explanation: Both the outer and inner if conditions are true, so the alert executes.
Answer: C) To create more specific decision branches
Explanation: Nested if statements let you make more granular checks within larger condition blocks.
Answer: A) Comfortable
Explanation: Both conditions are true (30 > 20 and 30 < 40), so "Comfortable" is shown.
Answer: B) Inner if is ignored
Explanation: The inner if is never evaluated unless the outer condition is true.
Answer: C) if (condition1) { if (condition2) { } }
Explanation: This is the correct syntax for nesting one if statement inside another.
Answer: B) Between 5 and 15
Explanation: Since a = 10, both conditions are satisfied and the alert displays the message.
Answer: A) Yes
Explanation: You can include else blocks inside nested if statements for additional logic.
Answer: C) Use clear formatting and indentation
Explanation: Proper indentation helps maintain readability and structure when nesting if statements.
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.
Answer: C) Using square brackets with an index
Explanation: Array elements are accessed using square brackets and the index number, starting from 0.
Answer: B) blue
Explanation: Array indexing starts from 0, so index 1 refers to "blue".
Answer: C) Returns undefined
Explanation: If the index does not exist in the array, JavaScript returns undefined.
Answer: B) .length
Explanation: The length property gives the number of elements in an array.
Answer: B) 3
Explanation: Adding an element at index 2 extends the array to 3 elements, even if index 2 was initially empty.
Answer: B) 6
Explanation: The array now has 6 positions (0 through 5), even though only index 5 has a value.
Answer: A) push()
Explanation: push() adds a new element at the end of the array.
Answer: C) Removes the last element
Explanation: The pop() method removes the last element from an array and returns it.
Answer: C) unshift()
Explanation: unshift() inserts a new element at the beginning of the array.
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.
Answer: C) Using square brackets with an index
Explanation: Array elements are accessed using square brackets and the index number, starting from 0.
Answer: B) blue
Explanation: Array indexing starts from 0, so index 1 refers to "blue".
Answer: C) Returns undefined
Explanation: If the index does not exist in the array, JavaScript returns undefined.
Answer: B) .length
Explanation: The length property gives the number of elements in an array.
Answer: B) 3
Explanation: Adding an element at index 2 extends the array to 3 elements, even if index 2 was initially empty.
Answer: B) 6
Explanation: The array now has 6 positions (0 through 5), even though only index 5 has a value.
Answer: A) push()
Explanation: push() adds a new element at the end of the array.
Answer: C) Removes the last element
Explanation: The pop() method removes the last element from an array and returns it.
Answer: C) unshift()
Explanation: unshift() inserts a new element at the beginning of the array.
Answer: B) splice()
Explanation: splice() can remove, replace, or insert elements at a specific index in an array.
Answer: A) 1,4
Explanation: splice(1, 2) removes two elements starting from index 1, which are 2 and 3.
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.
Answer: A) banana,cherry
Explanation: slice(1, 3) returns elements at index 1 and 2; index 3 is not included.
Answer: C) slice()
Explanation: slice() returns a new array and leaves the original array unchanged.
Answer: A) 1,2,99,3,4
Explanation: splice(2, 0, 99) inserts 99 at index 2 without removing any element.
Answer: C) pen,pencil
Explanation: slice(0, 2) returns items from index 0 and 1 only.
Answer: A) a,z,c,d
Explanation: splice(1,1,"z") removes "b" and replaces it with "z".
Answer: B) 3,4,5
Explanation: slice(2) returns elements starting from index 2 till the end.
Answer: A) x
Explanation: splice(1) removes all elements starting from index 1, so only "x" remains.
Answer: B) for (i = 0; i <= 5; i++)
Explanation: The correct syntax for a for loop includes initialization, condition, and increment separated by semicolons.
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.
Answer: C) 4 times
Explanation: It runs for i = 1, 2, 3, and 4 — a total of 4 times.
Answer: C) It tracks how many times the loop has run
Explanation: The loop control variable is used to determine the number of iterations.
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.
Answer: A) 15
Explanation: The loop adds 1 + 2 + 3 + 4 + 5, resulting in 15.
Answer: A) 0 2
Explanation: The loop prints i * 2 for i = 0 and i = 1, which are 0 and 2 respectively.
Answer: A) break
Explanation: The break statement is used to exit the loop prematurely.
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.
Answer: B) 0 2
Explanation: The continue statement skips the iteration when i equals 1, so it only prints 0 and 2.
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.
Answer: A) true
Explanation: The loop finds the value 9 in the array and sets found to true before breaking.
Answer: B) Exits the loop completely
Explanation: The break statement immediately exits the loop regardless of the loop condition.
Answer: B) 4 8 12
Explanation: The loop prints each element of the array using the array's length.
Answer: C) .length
Explanation: The `.length` property returns the number of elements in an array.
Answer: B) false
Explanation: A Boolean flag usually starts as false, indicating the condition hasn’t been met yet.
Answer: B) false
Explanation: 5 is not in the array, so found remains false throughout the loop.
Answer: A) true
Explanation: When i equals 2, the flag is set to true before the loop ends.
Answer: C) continue
Explanation: The continue statement skips the current iteration and moves to the next loop cycle.
Answer: C) Searching for a value
Explanation: Boolean flags are commonly used to indicate if a searched value was found within a loop.
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.
Answer: D) 6
Explanation: The outer loop runs 2 times and the inner loop runs 3 times for each outer iteration (2 * 3 = 6).
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.
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.
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.
Answer: C) array[i][j]
Explanation: 2D arrays are accessed using two indices — the row and the column — in the form array[i][j].
Answer: B) The outer loop
Explanation: The outer loop initiates the first iteration and calls the entire inner loop on each of its cycles.
Answer: D) 6
Explanation: Outer loop runs 3 times, inner loop 2 times per cycle → 3 * 2 = 6.
Answer: A) 0,0 0,1 1,0 1,1
Explanation: Each iteration logs the current i and j values, showing all combinations.
Answer: C) They reduce readability and performance
Explanation: Deep nesting can make code hard to follow and may lead to inefficient execution.