Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Consider the following pseudocode that simulates rolling a die 5 times: Do th

ID: 3555149 • Letter: 1

Question

1. Consider the following pseudocode that simulates rolling a die 5 times:

Do the following 5 times:
Roll the die.
Add value to total.

Which statement corresponds to rolling the die?

A. die = generator.nextInt(7) - 1;

B. die = generator.nextInt(6) + 1;

C. die = generator.nextInt(5) + 1;

D. die = generator.nextInt(7);

2. A(n) ___ loop is appropriate for a definite number of iterations.

A. while

B. for

C. infinite

D. nested

3. Assume that generator has been initialized to an instance of class Random. Which of the following calls returns a random integer between 0 and n inclusive?

A. generator.nextInt(n + 1)

B. generator.nextInt(n + 1) - 1

C. generator.nextInt(n)

D. generator.nextInt(n) + 1

4. Insert a statement to terminate this loop when the end of input is reached.
boolean done = false;
while (!done)
{
String input = in.next();
if (input.equalsIgnoreCase("Q"))
__________
else
{
double x = Double.parseDouble(input);
data.add(x);
}
}

A. done = true;

B. done = "Yes";

C. done = "Q";

D. done == true;

5. Consider the following pseudocode for removing dashes from a phoneNumber:

For each position of the phone number starting at the beginning
If the character is not a dash
Append the character at that position to the phone number without dashes.

Which is the correct if condition for the pseudocode?

A. if (phoneNumber.charAt(i) == '-')

B. if (phoneNumber.charAt(i).equals('-'))

C. if (!phoneNumber.charAt(i).equals('-'))

D. if (phoneNumber.charAt(i) != '-')

6. Insert the missing statement in the following investment loop code fragment.
int years = 20;
while (years > 0)
{
__________
double interest = balance * rate / 100;
balance = balance + interest;
}

A. years--;

B. years == years - 1;

C. years == years + 1;

D. years++;

7. Consider the following pseudocode for constructing the reverse of an input string:

For each position of the string starting at the end working backwards
Append the character at that position to the reverse of the string.

Which is the correct loop body for this pseudocode, assuming that reverse has been declared and initialized to the empty string?

A. reverse = reverse + input.stringAt(i);

B. reverse = input.charAt(i) + reverse;

C. reverse = reverse + input.charAt(i);

D. reverse = input.charAt(i);

A. die = generator.nextInt(7) - 1;

B. die = generator.nextInt(6) + 1;

C. die = generator.nextInt(5) + 1;

D. die = generator.nextInt(7);

2. A(n) ___ loop is appropriate for a definite number of iterations.

A. while

B. for

C. infinite

D. nested

3. Assume that generator has been initialized to an instance of class Random. Which of the following calls returns a random integer between 0 and n inclusive?

A. generator.nextInt(n + 1)

B. generator.nextInt(n + 1) - 1

C. generator.nextInt(n)

D. generator.nextInt(n) + 1

4. Insert a statement to terminate this loop when the end of input is reached.
boolean done = false;
while (!done)
{
String input = in.next();
if (input.equalsIgnoreCase("Q"))
__________
else
{
double x = Double.parseDouble(input);
data.add(x);
}
}

A. done = true;

B. done = "Yes";

C. done = "Q";

D. done == true;

5. Consider the following pseudocode for removing dashes from a phoneNumber:

For each position of the phone number starting at the beginning
If the character is not a dash
Append the character at that position to the phone number without dashes.

Which is the correct if condition for the pseudocode?

A. if (phoneNumber.charAt(i) == '-')

B. if (phoneNumber.charAt(i).equals('-'))

C. if (!phoneNumber.charAt(i).equals('-'))

D. if (phoneNumber.charAt(i) != '-')

6. Insert the missing statement in the following investment loop code fragment.
int years = 20;
while (years > 0)
{
__________
double interest = balance * rate / 100;
balance = balance + interest;
}

A. years--;

B. years == years - 1;

C. years == years + 1;

D. years++;

7. Consider the following pseudocode for constructing the reverse of an input string:

For each position of the string starting at the end working backwards
Append the character at that position to the reverse of the string.

Which is the correct loop body for this pseudocode, assuming that reverse has been declared and initialized to the empty string?

A. reverse = reverse + input.stringAt(i);

B. reverse = input.charAt(i) + reverse;

C. reverse = reverse + input.charAt(i);

D. reverse = input.charAt(i);

Explanation / Answer

1. B
2. B
3. A
4. A
5. D
6. A
7. C