For loop Flashcards

1
Q

What is the syntax for a basic for loop in TypeScript?

A
for (initialization; condition; increment) { // code to execute }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True or False: The initialization section of a for loop is optional in TypeScript.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Fill in the blank: In a for loop, the _____ section is executed after each iteration.

A

increment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What keyword can be used to declare the loop variable in a TypeScript for loop?

A

let or const

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

In TypeScript, what is the purpose of the condition in a for loop?

A

To determine whether to continue executing the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the syntax for a “for …of loop” in TypeScript?

A
let arr = [10, 20, 30, 40];

for (var val of arr) {
  console.log(val); // prints values: 10, 20, 30, 40
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of the for…of loop in TypeScript?

A

The for…of loop is used to iterate over iterable objects such as arrays, strings, and other collections.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

True or False: The for…of loop can be used to iterate over the keys of an object.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Fill in the blank: The syntax for a for…of loop is ‘for (const ____ of iterable) { }’.

A

element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which of the following can be used with a for…of loop?
A) Array

B) Object

C) String

A

A and C (Array and String)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write a short code snippet using a for…of loop to print each element of an array.

A
const arr = [1, 2, 3]; for (const num of arr) { console.log(num); }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the for…in loop iterate over in TypeScript?

A

The for…in loop iterates over the enumerable properties of an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

True or False: The for…in loop can be used to iterate over arrays in TypeScript.

A

True, but it’s not recommended due to potential unexpected results.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Fill in the blank: The syntax for a for…in loop is: for (____ in object) { }

A

variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which of the following is a correct example of a for…in loop?
A)for (let key in obj) { console.log(key); }

B) for (obj as key) { console.log(key); }

C) for (let val of obj) { console.log(val); }

A

A) for (let key in obj) { console.log(key); }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Short Answer: What is a potential drawback of using a for…in loop for iterating over arrays?

A

It may iterate over properties that are not part of the array, leading to unexpected behavior.