All Exams  >   Software Development  >   JavaScript for Web Development  >   All Questions

All questions of Loops in JS for Software Development Exam

What will be the output of the following code?
let obj = { a: 1, b: 2, c: 3 };
for (let key in obj) {
  console.log(key);
}
  • a)
    a b c
  • b)
    1 2 3
  • c)
    undefined undefined undefined
  • d)
    The output cannot be determined
Correct answer is option 'A'. Can you explain this answer?

Mansi Singh answered
Explanation:
The code provided creates an object named "obj" with three properties: "a", "b", and "c", each assigned a numerical value. The code then uses a for...in loop to iterate over each key in the object and print it to the console.

Code Execution:
1. The object "obj" is created with the properties a, b, and c.
2. The for...in loop iterates over each key in the object.
3. In the first iteration, the key "a" is printed to the console.
4. In the second iteration, the key "b" is printed to the console.
5. In the third iteration, the key "c" is printed to the console.
6. The loop completes and the code execution ends.

Output:
The output of the code will be:
a
b
c

Explanation:
The for...in loop iterates over each key in the object in an arbitrary order. In this case, the keys "a", "b", and "c" are printed to the console in that order. The values of the properties are not printed, only the keys themselves.
1 Crore+ students have signed up on EduRev. Have you? Download the App

Which loop statement is most appropriate when the number of iterations is known in advance?
  • a)
    for loop
  • b)
    while loop
  • c)
    do...while loop
  • d)
    switch loop
Correct answer is option 'A'. Can you explain this answer?

Codebreakers answered
The for loop is most appropriate when the number of iterations is known in advance because it allows you to initialize a loop variable, specify a termination condition, and update the loop variable all within the loop statement.

Which loop in JavaScript is used to iterate over the elements of an array?
  • a)
    while loop
  • b)
    for loop
  • c)
    do while loop
  • d)
    for...of loop
Correct answer is option 'D'. Can you explain this answer?

Mahi Ahuja answered
The 'for...of' loop in JavaScript is used to iterate over the elements of an array.

In JavaScript, loops are used to execute a block of code repeatedly until a certain condition is met. The 'for...of' loop is specifically designed for iterating over the elements of an array or any other iterable object in a concise and readable manner.

How does the 'for...of' loop work?
The 'for...of' loop provides an easy and straightforward way to iterate over the elements of an array without the need for manual indexing or tracking the length of the array. It follows the syntax:

```
for (variable of iterable) {
// code to be executed for each element
}
```

Key points about the 'for...of' loop:
- The 'variable' represents a new variable that will be assigned the value of each element in the 'iterable' object on each iteration.
- The 'iterable' object can be an array, a string, or any other iterable object in JavaScript.
- On each iteration, the 'variable' takes the value of the next element in the 'iterable' object until all the elements have been iterated.
- The loop automatically handles the iteration process and does not require manual indexing or specifying the length of the array.
- The 'for...of' loop can only be used for forward iteration and does not provide access to the index of each element in the array.
- The loop terminates when all the elements in the 'iterable' object have been processed.

Example:
Let's consider an example to demonstrate the usage of the 'for...of' loop:

```javascript
const numbers = [1, 2, 3, 4, 5];

for (const num of numbers) {
console.log(num);
}
```

In this example, the 'for...of' loop is used to iterate over each element in the 'numbers' array. On each iteration, the 'num' variable takes the value of the next element in the array, and it is logged to the console. The output will be:

```
1
2
3
4
5
```

The 'for...of' loop simplifies the process of iterating over the elements of an array and provides a more readable and concise syntax compared to other types of loops in JavaScript.

Which loop statement is most appropriate when the condition for termination is checked at the end of the loop?
  • a)
    for loop
  • b)
    while loop
  • c)
    do...while loop
  • d)
    switch loop
Correct answer is option 'C'. Can you explain this answer?

Codebreakers answered
The do...while loop is most appropriate when the condition for termination is checked at the end of the loop because it guarantees that the loop block will be executed at least once before checking the termination condition.

Which loop in JavaScript is guaranteed to execute the code block at least once?
  • a)
    for loop
  • b)
    while loop
  • c)
    do...while loop
  • d)
    switch loop
Correct answer is option 'C'. Can you explain this answer?

The do...while loop in JavaScript is similar to the while loop, but it guarantees that the code block will be executed at least once, even if the condition is initially false.

What will be the output of the following code?
let i = 0;
while (i < 5) {
  console.log(i);
  i += 2;
}
  • a)
    0 1 2 3 4
  • b)
    0 2 4
  • c)
    1 3 5
  • d)
    0 1 2 3
Correct answer is option 'B'. Can you explain this answer?

The code uses a while loop to repeatedly output the value of 'i' until it becomes equal to or greater than 5. The initial value of 'i' is 0, and in each iteration, it is incremented by 2. Therefore, the output will be 0, 2, and 4.

What will be the output of the following code?
for (let i = 0; i < 5; i++) {
  if (i === 3) {
    continue;
  }
  console.log(i);
}
  • a)
    0 1 2 3 4
  • b)
    0 1 2 4
  • c)
    0 1 2 3
  • d)
    1 2 3 4
Correct answer is option 'B'. Can you explain this answer?

The code uses a for loop to iterate from 0 to 4. The 'continue' statement is encountered when 'i' is equal to 3, which skips the current iteration. Therefore, the number 3 is omitted from the output.

What happens if the update expression is omitted in a for loop?
  • a)
    The loop will execute indefinitely.
  • b)
    The loop will only execute once.
  • c)
    The loop will not execute at all.
  • d)
    An error will occur.
Correct answer is option 'A'. Can you explain this answer?

CodeNation answered
If the update expression is omitted in a for loop, the loop will continue to execute indefinitely because there is no mechanism to change the loop variable and satisfy the termination condition.

What will be the output of the following code?
let x = 3;
for (let i = 0; i < x; i++) {
  console.log(i);
  x++;
}
  • a)
    0 1 2
  • b)
    0 1 2 3
  • c)
    0 1 2 3 4
  • d)
    Infinite loop
Correct answer is option 'D'. Can you explain this answer?

Yogesh Dwivedi answered
The code uses a for loop to iterate from 0 to 'x - 1', where 'x' is initially set to 3. However, within the loop, the value of 'x' is incremented by 1 in each iteration. This causes the termination condition 'i < x' to always be true, leading to an infinite loop.

What will be the output of the following code?
let i = 0;
do {
  console.log(i);
  i++;
} while (i < 3);
  • a)
    0 1 2
  • b)
    1 2 3
  • c)
    0 1
  • d)
    0 1 2 3
Correct answer is option 'A'. Can you explain this answer?

Tanuja Mishra answered
The code uses a do...while loop to output the value of 'i' until it becomes equal to or greater than 3. The initial value of 'i' is 0, and in each iteration, it is incremented by 1. The loop continues until 'i' is no longer less than 3. Therefore, the output will be 0, 1, and 2.

What will be the output of the following code?
let x = 5;
for (let i = 0; i < x; i++) {
  if (i === 3) {
    break;
  }
  console.log(i);
}
  • a)
    0 1 2
  • b)
    0 1 2 3
  • c)
    0 1 2 3 4
  • d)
    1 2 3
Correct answer is option 'A'. Can you explain this answer?

Tanuja Mishra answered
The code uses a for loop to iterate from 0 to 'x - 1', where 'x' is initially set to 5. When 'i' becomes equal to 3, the 'break' statement is encountered, which terminates the loop immediately. Therefore, the output will be 0, 1, and 2.

Chapter doubts & questions for Loops in JS - JavaScript for Web Development 2024 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Loops in JS - JavaScript for Web Development in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

Top Courses Software Development

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev