Chapter 4 - Loops Flashcards

1
Q

What are the 3 kinds of loops?

A

while, do-while, and for loops

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

What is the loop body?

A

The loop body is the statements inside the loop that are executed every iteration

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

What is the difference between a while loop and a do-while loop?

A

A while loop checks the loop-continuation-condition before entering the loop body, while a do-while loop checks it after the first iteration

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

When are while and do-while loops generally used?

A

When the number of iterations is not predetermined

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

When is a for loop generally used?

A

When the loop body needs to be executed a certain number of times

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

What is the syntax of the while loop?

A

while (loop-continuation-condition) {
Statement(s);
}

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

What is the syntax of the do-while loop?

A

do {
Statement(s);
} while (loop-continuation-condition)

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

What is the syntax of the for loop?

A

for (initial-action; loop-continuation-condition; action-after-each-iteration) {
Statement(s);
}

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

Analyze the following statement:

double sum = 0;
for (double d = 0; d

A

D. The program compiles and runs fine.

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

Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + … + 99/100?

A:
double sum = 0;
for (int i = 1; i

A

E. CD

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

The following loop displays _____.

for (int i = 1; i );
i++;
}

A. 1 2 3 4 5 6 7 8 9
B. 1 2 3 4 5 6 7 8 9 10
C. 1 2 3 4 5
D. 1 3 5 7 9
E. 2 4 6 8 10
A

D. 1 3 5 7 9

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

Do the following two statements in (I) and (II) result in the same value in sum?

(I):
for (int i = 0; i<10; i++) {
sum += i;
}

A

Yes

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

What is i after the following for loop?

int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}

A. 9
B. 10
C. 11
D. undefined

A

D. undefined

Explanation: The scope of i is inside the loop. After the loop, i is not defined.

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

Given the following four patterns,

Pattern A
1
1  2
1  2  3
1  2  3  4
1  2  3  4  5
1  2  3  4  5  6
Pattern B 
1  2  3  4  5  6
1  2  3  4  5
1  2  3  4
1  2  3
1  2
1
Pattern C 
              1
           2 1
        3 2 1
     4 3 2 1
  5 4 3 2 1
6 5 4 3 2 1 
Pattern D
1 2 3 4 5 6
   1 2 3 4 5 
      1 2 3 4
         1 2 3
            1 2 
               1
Which of the pattern is produced by the following code?
    for (int i = 1; i = 1; j--)
        System.out.print(j );
      System.out.println();
    }
A

C

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

Analyze the following fragment:

double sum = 0;
double d = 0;
while (d != 10.0) {
  d += 0.1;
  sum += sum + d;
}

A. The program does not compile because sum and d are declared double, but assigned with integer value 0.
B. The program never stops because d is always 0.1 inside the loop.
C. The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
D. After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + … + 1.9

A

C. The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.

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

Analyze the following code:

public class Test {  
  public static void main (String args[]) {
    int i = 0;
    for (i = 0; i
A

C and D are correct.
Explanation: This is a logic error. System.out.println(i + 4) is not a part the for loop because the for loop ends with the last semicolon at for (i=0; i

17
Q

What will be displayed after the following loop terminates?

int number = 25;
int i;

boolean isPrime = true;
for (i = 2; i

A

D. i is 6 isPrime is false