Lec10: Do-While Loops Flashcards

1
Q

If the compiler encounters a statement that uses a variable before the variable is declared, an error will result.

A

True, this is known as a scope problem.

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

This type of loop will always be executed at least once.

A

Post-test loop (do-while loop)

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

Data-Validation: For error-checking to be truly useful, we need to allow the user to re-enter their input until they get it right. This requires repetition in general and specifically, a loop. Fill in the blank for the do-while condition so that the user stays inside the loop ONLY if they enter an invalid value. Notice that the blank will be repeated in BOTH the if statement and the while loop condition.

do{
System.out.print(“Enter an even integer: “);
number = keybd.nextInt();
if(___________________________)
System.out.println(“Bad input. Try again!”);
}while(____________________________);
System.out.println(“Thank you!”);

A

number % 2 != 0

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

How many times will the following do-while loop be executed?

int x = 11;
do
{
x += 20;
} while (x > 100);

A

1

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

What will be the value of x after the following code is executed?

int x = 10;
do
{
x *= 20;
}while (x > = 200);

A

Infinite loop

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

How many times will the following do-while loop be executed?

int x = 11;
do
{
x += 20;
}while (x <= 100);

A

5

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

This type of loop is ideal in situations where you always want the loop to iterate at least once.

A

do-while loop

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

The do-while loop must be terminated with a semicolon.

A

True

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

Which of the following code fragments will NOT print out 53 asterisks on one line?

(a)int count = 0; (b) int count = 0;
do{ do{
System.out.print(“”); System.out.print(“”);
}while (++count < 53); }while (count++ < 53);
(c)int count = 1; (d) int count = 1;
while(count++ <= 53) while(count++ < 54)
{ System.out.print(“”);
System.out.print(“
”);
}

A

(b)

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

Data-Validation: For error-checking to be truly useful, we need to allow the user to re-enter their input until they get it right. This requires repetition in general and specifically, a loop. Fill in the blank for the while condition so that the user stays inside the loop ONLY if they enter an invalid value. Suppose we need the user to enter an integer between 1 and 5. Choose the correct condition for the while loop.

System.out.print(“Enter an integer from 1-5: “);
number = keybd.nextInt();
while(____________________________)
{
System.out.println(“Bad input. Try again!”);
System.out.print(“Enter an integer from 1-5:”);
number = keybd.nextInt();
}
System.out.println(“Thank you!”);

A

number < 1 || number > 5

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

What is the output of the following Java code?

int num = 10;
while(num > 10)
num = num - 2;
System.out.println(num);

A

10

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

What is the output of the following Java code?

int x = 1;
do{
System.out.print(x + “ “);
} while( x > 0);
System.out.println();

A. 1
B. 1 0 -1
C. 1 0
D. None of these

A

D. None of these.

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

Suppose sum and num are int variables, and the input captured from the keyboard (in the order shown) from the code below is :

18 25 61 6 -1

What is the output of the following code? (Assume that console is the name of a Scanner object named initialized to the standard input device.)

sum = 0;
System.out.print(“Enter a number: “);
num = console.nextInt( );
while( num != -1)
{
sum =sum + num;
System.out.print(“Enter a number: “);
num = console.nextInt( );
}
System.out.println(sum);

A

110

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

What is the output of the following Java code?

count = 1;
num = 25;
while( count < 25)
{
num = num - 1;
count ++;
}
System.out.println(count + “ “ + num);

A

25 1

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

What is the output of the following program segment?

int x = 14;
int y = 60;
while( (y - x) % 3 != 0)
{
System.out.print(y + “ “);
y = y - 5;
}
System.out.println( );

A

60 55

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

Consider the following code fragment:

int count = 0, a = 10 ;
int x = < some value>;
while( count < 100 && (x * x * x - a) > 0.01)
{
x = (1.0 / 3.0) * (2 * x + a) / (x * x);
count++ ;
}

Which of the following conditions must be true after this code has been successfully executed? (Hint: Do not analyze the code too deeply - instead, look at the while loop condition. Think of DeMorgan’s Laws.)

A

count >= 100 || (x * x * x - a) <= 0.01

17
Q

Two truths and a lie: Identify the false statement.

A. When the statements in a loop body must execute at least one time, it is convenient to use a do…while loop.
B. When the body of a do…while loop contains a single statement, you do not need to use curly braces to block the statement.
C. The do…while loop checks the value of the loop control variable at the top of the loop prior to loop execution.

A

The do-while loop checks the value of the loop control variable at the top of the loop prior to the loop execution.