Table of contents | |
Multiple Choice Questions (MCQs) | |
High Order Thinking Questions (HOTS) | |
Fill in the Blanks | |
True/False |
Q.1. Which loop statement is used to execute a block of code repeatedly as long as a specified condition is true?
(a) for loop
(b) while loop
(c) do-while loop
(d) if-else loop
Ans. (b)
Q.2. The three components of a for loop are:
(a) initialization, condition, and update
(b) condition, increment, and decrement
(c) initialization, condition, and decrement
(d) initialization, increment, and condition
Ans. (a)
Q.3. What is the output of the following for loop?
for (var i = 0; i < 5; i++) {
console.log(i);
}
(a) 0 1 2 3 4
(b) 1 2 3 4 5
(c) 5 4 3 2 1
(d) 4 3 2 1 0
Ans. (a)
Q.4. Which loop is best suited for looping through the elements of an array?
(a) for loop
(b) while loop
(c) do-while loop
(d) foreach loop
Ans. (a)
Q.5. Which loop is used to iterate over the properties of an object?
(a) for loop
(b) while loop
(c) do-while loop
(d) for...in loop
Ans. (d)
Q.1. Write a for loop that prints the squares of numbers from 1 to 10.
for (var i = 1; i <= 10; i++) {
console.log(i * i);
}
Q.2. Explain the concept of "iteration" in the context of loops.
Iteration refers to the process of repeatedly executing a block of code until a specific condition is met. In the context of loops, it involves executing the loop body multiple times, often with different values or data, until the loop's termination condition is false.
Q.3. How can you terminate a loop before its normal end?
A loop can be terminated before its normal end by using the 'break' statement. When encountered within a loop, 'break' immediately exits the loop, and control is transferred to the next statement outside the loop.
Q.4. What is the difference between the 'break' and 'continue' statements in JavaScript?
The 'break' statement is used to exit a loop entirely. When encountered, it terminates the innermost loop and transfers control to the next statement outside the loop. On the other hand, the 'continue' statement is used to skip the current iteration of a loop and continue with the next iteration.
Q.5. Write a program that prints the Fibonacci sequence using a for loop.
var n = 10;
var fib = [0, 1];
for (var i = 2; i <= n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
console.log(fib);
1. The ________ statement is used to jump out of a loop and continue executing the next statement outside the loop.
'break'
2. The ________ statement is used to skip the current iteration of a loop and continue with the next iteration.
'continue'
3. The ________ loop is executed at least once, even if the condition is false.
'do-while'
4. In JavaScript, arrays are zero-________.
'based'
5. A(n) ________ is a sequence of properties where each property has a key and a value.
'object'
1. In a for loop, the initialization expression is executed only once at the beginning.
True
2. A for loop can have multiple update expressions separated by commas.
True
3. The break statement can only be used within a loop or a switch statement.
True
4. The condition of a loop is checked after each iteration.
True
5. A while loop is guaranteed to execute at least once, even if the condition is initially false.
False
Hands-On Questions
Q.1. Write a JavaScript program that uses a nested for loop to print the following pattern:
*
* *
* * *
* * * *
* * * * *
for (var i = 1; i <= 5; i++) {
var row = '';
for (var j = 1; j <= i; j++) {
row += '* ';
}
console.log(row);
}
Q.2. Iteration refers to the process of repeatedly executing a block of code until a specific condition is met. In the context of loops, it involves executing the loop body multiple times, often with different values or data, until the loop's termination condition is false.
function sumArray(arr) {
var sum = 0;
for (var i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
var numbers = [1, 2, 3, 4, 5];
console.log(sumArray(numbers));
Q.3. A loop can be terminated before its normal end by using the break statement. When encountered within a loop, break immediately exits the loop, and control is transferred to the next statement outside the loop.
var person = {
name: 'John',
age: 30,
city: 'New York'
};
for (var key in person) {
console.log(key + ': ' + person[key]);
}
Q.4. The break statement is used to exit a loop entirely. When encountered, it terminates the innermost loop and transfers control to the next statement outside the loop. On the other hand, the continue statement is used to skip the current iteration of a loop and continue with the next iteration.
var num = 5;
for (var i = 1; i <= 10; i++) {
console.log(num + ' * ' + i + ' = ' + num * i);
}
Q.5. var n = 10;
var fib = [0, 1];
for (var i = 2; i <= n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
console.log(fib);
var n = 6;
var factorial = 1;
var i = 1;
while (i <= n) {
factorial *= i;
i++;
}
console.log(factorial);
51 videos|28 docs|12 tests
|
|
Explore Courses for Software Development exam
|