CS 4A: Chapter 5 – Loops – Flashcards
Unlock all answers in this set
Unlock answersquestion
Analyze the following code.
double sum = 0;
for (double d = 0; d < 10; sum += sum + d) {
d += 0.1;
}
answer
The program compiles and runs fine.
question
What is the number of iterations in the following loop:
for (int i = 1; i < n; i++) {
// iteration
}
answer
n - 1
question
Analyze the following statement:
double sum = 0;
for (double d = 0; d<10;) {
d += 0.1;
sum += sum + d;
}
answer
The program compiles and runs fine.
question
The elements inside the for loop control are separated using semicolons instead of commas. True/False
answer
True
question
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);
answer
10
question
What is the output of the following fragment?
for (int i = 0; i < 15; i++) {
if (i % 4 == 1)
System.out.print(i + " ");
}
answer
1 5 9 13
question
What is y after the following for loop statement is executed?
int y = 0;
for (int i = 0; i < 10; ++i) {
y += 1;
}
answer
10
question
What is the output for y?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}
System.out.println(y);
answer
45
question
A continue statement can be used only in a loop. True/False
answer
True
question
A break statement can be used only in a loop. True/False
answer
False
question
What is sum after the following loop terminates?
int sum = 0;
int item = 0;
do {
item++;
sum += item;
if (sum > 4) break;
}
while (item < 5);
answer
6
question
Which of the following loops produces the following output? (choose all that apply)
1 2 3 4 1 2 3 1 2 1
(I)
for (int i = 5; i > 0; i--) {
for (int j = 1; j < i; j++)
System.out.print(j + " ");
System.out.println();
}
(II)
for (int i = 1; i < 5; i++) {
for (int j = 1; j < i; j++)
System.out.print(j + " ");
System.out.println();
}
(III)
int i = 0;
while (i < 5) {
for (int j = 1; j < i; j++)
System.out.print(j + " ");
System.out.println();
i++;
}
(IV)
int i = 5;
while (i > 0) {
for (int j = 1; j < i; j++)
System.out.print(j + " ");
System.out.println();
i--;
}
answer
I & IV
question
A variable declared in the for loop control can be used after the loop exits. True/False
answer
False
question
What is i after the following for loop?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}
answer
Undefined
question
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;
}
answer
Yes
question
Is the following loop correct?
for (; ; );
answer
Yes
question
You can always convert a for loop to a while loop. True/False
answer
True
question
How many times will the following code print "Welcome to Java"?
int count = 0;
while (count++ < 10) {
System.out.println("Welcome to Java");
}
answer
10
question
How many times are the following loops executed?
for (int i = 0; i < 10; i++)
for (int j = 0; j < i; j++)
System.out.println(i * j)
answer
45
question
Suppose cond1 is a Boolean expression. When will this while condition be true?
while ( !cond1) ...
answer
If cond1 is false
question
Assume x is 0. What is the output of the following statement?
if (x > 0)
System.out.println("x is greater than 0");
else if (x < 0)
System.out.println("x is less than 0");
else
System.out.println("x equals 0");
answer
x = 0
question
What is the output of the following code:
for ( ; false ; )
System.out.println("Welcome to Java");
answer
Does not print anything
question
You can always convert a while loop to a for loop.
answer
True
question
What is 1.0 + 1.0 + 1.0 == 3.0?
answer
There is no guarantee that 1.0 + 1.0 + 1.0 == 3.0 is true.
question
How many times will the following code print "Welcome to Java"?
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
}
answer
10
question
Analyze the following fragment:
double sum = 0;
double d = 0;
while (d != 10.0) {
d += 0.1;
sum += sum + d;
}
answer
The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
question
Which of the following expression yields an integer between 0 and 100, inclusive?
answer
(int)(Math.random() * 101)
question
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);
answer
10
question
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;
}
answer
1
question
Analyze the following code.
int count = 0;
while (count < 100) {
// Point A
System.out.println("Welcome to Java!");
count++;
// Point B
}
// Point C
answer
- count < 100 is always true at Point A
- count < 100 is always false at Point C
question
What balance after the following code is executed?
int balance = 10;
while (balance >= 1) {
if (balance < 9) continue;
balance = balance - 9;
}
answer
The loop does not end
question
How many times will the following code print "Welcome to Java"?
int count = 0;
do {
System.out.println("Welcome to Java");
} while (++count < 10);
answer
10
question
The while loop and the do loop are equivalent in their expressive power; in other words, you can rewrite a while loop using a do loop, and vice versa. True/False
answer
True
question
What is the number of iterations in the following loop:
for (int i = 1; i <= n; i++) {
// iteration
}
answer
n
question
How many times will the following code print "Welcome to Java"?
int count = 0;
do {
System.out.println("Welcome to Java");
} while (count++ < 10);
answer
11
question
You can always write a program without using break or continue in a loop. True/False
answer
True