Chapter 5 Flashcards
How many times will the following code print “Welcome to Java”?
int count = 0;
while (count++ < 10) {
System.out.println(“Welcome to Java”);
}
10
To add 0.01 + 0.02 + … + 1.00, what order should you use to add the numbers to get better accuracy?
add 0.01, 0.02, …, 1.00 in this order to a sum variable whose initial value is 0.
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”);
}
AB
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;
}
Yes
Is the following loop correct?
for ( ; ; );
Yes
What is the output for y? int y = 0; for (int i = 0; i < 10; ++i) { y += i; } System.out.println(y);
45
How many times will the following code print “Welcome to Java”?
int count = 0;
do {
System.out.println(“Welcome to Java”);
} while (count++ < 10);
11
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);
10
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);
10
The following loop displays _______________.
for (int i = 1; i <= 10; i++) {
System.out.print(i + “ “);
i++;
}
1 3 5 7 9
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)
45
How many times will the following code print “Welcome to Java”?
int count = 0;
do {
System.out.println(“Welcome to Java”);
} while (++count < 10);
10
What is the number of iterations in the following loop? for (int i = 1; i < n; i++) { // iteration }
n - 1
How many times will the following code print “Welcome to Java”?
int count = 0;
while (count < 10) {
System.out.println(“Welcome to Java”);
count++;
}
10
What is the output of the following code? int x = 0; while (x < 4) { x = x + 1; } System.out.println("x is " + x);
x is 4