Chapter 5 Flashcards

1
Q

How many times will the following code print “Welcome to Java”?
int count = 0;
while (count++ < 10) {
System.out.println(“Welcome to Java”);
}

A

10

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

To add 0.01 + 0.02 + … + 1.00, what order should you use to add the numbers to get better accuracy?

A

add 0.01, 0.02, …, 1.00 in this order to a sum variable whose initial value is 0.

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

Which of the following loops prints “Welcome to Java” 10 times?
A:
for (int count = 1; count <= 10; count++) {
System.out.println(“Welcome to Java”);
}
B:
for (int count = 0; count < 10; count++) {
System.out.println(“Welcome to Java”);
}
C:
for (int count = 1; count < 10; count++) {
System.out.println(“Welcome to Java”);
}
D:
for (int count = 0; count <= 10; count++) {
System.out.println(“Welcome to Java”);
}

A

AB

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
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;
}

(II):
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
5
Q

Is the following loop correct?

for ( ; ; );

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
What is the output for y?
int y = 0;
for (int i = 0; i < 10; ++i) {
     y += i;
}
System.out.println(y);
A

45

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

How many times will the following code print “Welcome to Java”?
int count = 0;
do {
System.out.println(“Welcome to Java”);
} while (count++ < 10);

A

11

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

How many times will the following code print “Welcome to Java”?
int count = 0;
do {
System.out.println(“Welcome to Java”);
count++;
} while (count < 10);

A

10

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

What is the value in count after the following loop is executed?
int count = 0;
do {
System.out.println(“Welcome to Java”);
} while (count++ < 9);
System.out.println(count);

A

10

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

The following loop displays _______________.

for (int i = 1; i <= 10; i++) {
System.out.print(i + “ “);
i++;
}

A

1 3 5 7 9

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

How many times is the println statement executed?
for (int i = 0; i < 10; i++)
for (int j = 0; j < i; j++)
System.out.println(i * j)

A

45

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

How many times will the following code print “Welcome to Java”?
int count = 0;
do {
System.out.println(“Welcome to Java”);
} while (++count < 10);

A

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
What is the number of iterations in the following loop?
for (int i = 1; i < n; i++) {
     // iteration
}
A

n - 1

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

How many times will the following code print “Welcome to Java”?
int count = 0;
while (count < 10) {
System.out.println(“Welcome to Java”);
count++;
}

A

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
What is the output of the following code?
int x = 0;
while (x < 4) {
     x = x + 1;
}
System.out.println("x is " + x);
A

x is 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
What will be displayed when the following code is executed?
int number = 6;
while (number > 0) {
     number -= 3;
     System.out.print(number + " ");
}
A

3 0

17
Q
What is y after the following for loop statement is executed?
int y = 0;
for (int i = 0; i < 10; ++i) {
     y += 1;
}
A

10

18
Q
What is the output after the following loop terminates?
int number = 25;
int i;
boolean isPrime = true;
for (i = 2; i < number; i++) {
     if (number % i == 0) {
          isPrime = false;
          break;
     }
}
System.out.println("i is " + i + " isPrime is " + isPrime)
A

i is 5 isPrime is false

19
Q

How many times is the println statement executed?
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
System.out.println(i * j);

A

100

20
Q
What is the value of balance after the following code is executed?
int balance = 10;
while (balance >= 1) {
     if (balance < 9)
          break;
     balance = balance - 9;
}
A

1