Chapter 5: Loops Self Test Questions Flashcards
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
Analyze the following code. Please select all that apply.
int count = 0;
while (count < 100) {
// Point A
System.out.println(“Welcome to Java!”);
count++;
// Point B
}
// Point C
A. count < 100 is always true at Point A
B. count < 100 is always true at Point B
C. count < 100 is always false at Point B
D. count < 100 is always true at Point C
E. count < 100 is always false at Point C
A & E
What will be displayed when the following code is executed?
int number = 6;
while (number > 0) {
number -= 3;
System.out.print(number + “ “);
}
3 0
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
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”);
} 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
Analyze the following statement:
double sum = 0;
for (double d = 0; d < 10;) {
d += 0.1;
sum += sum + d;
}
A. The program has a compile error because the adjustment is missing in the for loop.
B. The program has a compile error because the control variable in the for loop cannot be of the double type.
C. The program runs in an infinite loop because d < 10 would always be true.
D. The program compiles and runs fine.
The program compiles and runs fine.
Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + … + 99/100?
A:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum = i / (i + 1);
}
System.out.println(“Sum is “ + sum);
B:
double sum = 0;
for (int i = 1; i < 99; i++) {
sum += i / (i + 1);
}
System.out.println(“Sum is “ + sum);
C:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum += 1.0 * i / (i + 1);
}
System.out.println(“Sum is “ + sum);
D:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum += i / (i + 1.0);
}
System.out.println(“Sum is “ + sum);
E:
double sum = 0;
for (int i = 1; i < 99; i++) {
sum += i / (i + 1.0);
}
System.out.println(“Sum is “ + sum);
c & d
Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + … + 99/100?
A:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum = i / (i + 1);
}
System.out.println(“Sum is “ + sum);
B:
double sum = 0;
for (int i = 1; i < 99; i++) {
sum += i / (i + 1);
}
System.out.println(“Sum is “ + sum);
C:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum += 1.0 * i / (i + 1);
}
System.out.println(“Sum is “ + sum);
D:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum += i / (i + 1.0);
}
System.out.println(“Sum is “ + sum);
E:
double sum = 0;
for (int i = 1; i < 99; i++) {
sum += i / (i + 1.0);
}
System.out.println(“Sum is “ + sum);
c & d
The following loop displays _______________.
for (int i = 1; i <= 10; i++) {
System.out.print(i + “ “);
i++;
}
1 3 5 7 9
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
What is the output for y?
int y = 0;
for (int i = 0; i < 10; ++i) {
y += i;
}
System.out.println(y);
45
What is i after the following for loop?
int y = 0;
for (int i = 0; i < 10; ++i) {
y += i;
}
undefined
Is the following loop correct?
for ( ; ; );
yes