CS 4A- Chapter 5 – Loops

Your page rank:

Total word count: 930
Pages: 3

Calculate the Price

- -
275 words
Looking for Expert Opinion?
Let us have a look at your work and suggest how to improve it!
Get a Consultant

Analyze the following code.

double sum = 0;
for (double d = 0; d < 10; sum += sum + d) {
d += 0.1;
}

The program compiles and runs fine.

What is the number of iterations in the following loop:

for (int i = 1; i < n; i++) {
// iteration
}

n – 1

Analyze the following statement:

double sum = 0;
for (double d = 0; d<10;) {
d += 0.1;
sum += sum + d;
}

The program compiles and runs fine.

The elements inside the for loop control are separated using semicolons instead of commas. True/False

True

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

What is the output of the following fragment?

for (int i = 0; i < 15; i++) {
if (i % 4 == 1)
System.out.print(i + " ");
}

1 5 9 13

What is y after the following for loop statement is executed?

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

10

What is the output for y?

int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}
System.out.println(y);

45

A continue statement can be used only in a loop. True/False

True

A break statement can be used only in a loop. True/False

False

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);

6

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–;
}

I & IV

A variable declared in the for loop control can be used after the loop exits. True/False

False

What is i after the following for loop?

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

Undefined

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

You can always convert a for loop to a while loop. True/False

True

How many times will the following code print "Welcome to Java"?

int count = 0;
while (count++ < 10) {
System.out.println("Welcome to Java");
}

10

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)

45

Suppose cond1 is a Boolean expression. When will this while condition be true?

while ( !cond1) …

If cond1 is false

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");

x = 0

What is the output of the following code:

for ( ; false ; )
System.out.println("Welcome to Java");

Does not print anything

You can always convert a while loop to a for loop.

True

What is 1.0 + 1.0 + 1.0 == 3.0?

There is no guarantee that 1.0 + 1.0 + 1.0 == 3.0 is true.

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 fragment:

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

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

Which of the following expression yields an integer between 0 and 100, inclusive?

(int)(Math.random() * 101)

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 of balance after the following code is executed?

int balance = 10;

while (balance >= 1) {
if (balance < 9) break;
balance = balance – 9;
}

1

Analyze the following code.

int count = 0;
while (count < 100) {
// Point A
System.out.println("Welcome to Java!");
count++;
// Point B
}

// Point C

– count < 100 is always true at Point A – count < 100 is always false at Point C

What balance after the following code is executed?

int balance = 10;

while (balance &gt;= 1) {
if (balance &lt; 9) continue;
balance = balance – 9;
}

The loop does not end

How many times will the following code print "Welcome to Java"?

int count = 0;
do {
System.out.println("Welcome to Java");
} while (++count &lt; 10);

10

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

True

What is the number of iterations in the following loop:

for (int i = 1; i &lt;= n; i++) {
// iteration
}

n

How many times will the following code print "Welcome to Java"?

int count = 0;
do {
System.out.println("Welcome to Java");
} while (count++ &lt; 10);

11

You can always write a program without using break or continue in a loop. True/False

True

Share This
Flashcard

More flashcards like this

NCLEX 10000 Integumentary Disorders

When assessing a client with partial-thickness burns over 60% of the body, which finding should the nurse report immediately? a) ...

Read more

NCLEX 300-NEURO

A client with amyotrophic lateral sclerosis (ALS) tells the nurse, "Sometimes I feel so frustrated. I can’t do anything without ...

Read more

NASM Flashcards

Which of the following is the process of getting oxygen from the environment to the tissues of the body? Diffusion ...

Read more

Unfinished tasks keep piling up?

Let us complete them for you. Quickly and professionally.

Check Price

Successful message
sending