# 1 what is the purpose of the following algorithm assuming s references a string bool 5346790

1. What is the purpose of the following algorithm, assuming s references a String?
boolean valid = (s != null);
int i = 0;
char ch;
while (valid && i
{
ch = s.charAt(i);
valid = Character.isDigit(ch);
i++;
}

 A. verifies that the string contains all digits

B. finds the first digit in the string

C. counts the number of digits in the string

D. finds the last digit in the string

2. What output is generated by this for loop?
String str = “”;
for (int i = 0; i
{
System.out.println(str.charAt(i) + “-“);
}

A. —

B. No output is generated.

C. —

D. –

3. Consider the following pseudocode for simulating counting players onto teams numbered 1 to 4:

For each player:
Count off which team.

Which statement correctly constructs the loop for each player?

A. for (int p = 1; p

B. for (int p = 0; p

C. for (int p = 0; p

D. for (int p = 1; p

4. Which for loop header has symmetric bounds?

A. Which for loop header has symmetric bounds?

B. for (i = 10; i

C. for (i = a; i

D. for (i = 0; i

5.Consider the following pseudocode for simulating counting players onto teams numbered 1 to 4:

For each player:
Count off which team.

Which statement correctly simulates counting the player onto a team, assuming p is the number of the player?

A. team = (p % 4) != 0 ? (p % 4) : 4;

B. team = (p mod 4) != 0 ? (p mod 4) : 4;

C. team = (p % 4);

D. team = (p mod 4);

6. Which single-step command steps inside method calls?

A. continue

B. step into

C. breakpoint

D. step over

7. Complete the for loop header for this algorithm that counts the number of digits in a string.
int digits = 0;
for (____________________)
{
char ch = str.charAt(i);
if (Character.isDigit(ch))
{
digits++;
}
}

A. int i = 1; i

B. int i = 0; i

C. int i = 0; i

D. int i = 1; i

8. What is the purpose of the following algorithm?
boolean valid = false;
int input;
while (!valid)
{
System.out.print(“Please enter a number between 1 and 10: “);
input = in.nextInt();
if (0
else { System.out.println(“Invalid input.”); }
}

A. comparing adjacent values

B. finding the first match

C. counting matches

D. prompting until a match is found

9. What output does this for loop generate?
for (int i = 1; i
{
System.out.print(i + ” “);
}
System.out.println(i + 1);

A. 1 2 3 4 5

B. The output is infinite.

C. None because the code does not compile.

D. 1 2 3 4 5 6

10. What output does this while loop generate?
j = 1;
while (j > 0)
{
System.out.print(j + “, “);
j++;
}

A. No output is generated.

B. 1, 2, 3, 4, 5, 6,

C. 1, 2, 3, 4, 5,

D. The output is infinite.

11.

In the following code segment, the value d is set to a random integer between ____.

Random generator = new Random();
int d = generator.nextInt(100);

A. 0 and 99

B. 1 and 100

C. 1 and 99

D. 0 and 100

12. When a debugger executes a program, the execution is suspended whenever a __________ is reached.

A. break

B. breakpoint

C. continue

D. single-step

13. How many iterations does the following for loop execute?
for(x = 0; x

A. 21

B. 19

C. 20

D. 0

14. What is the purpose of the following algorithm?
boolean found = false;
int i = 0;
while (!found && i
{
ch = str.charAt(i);
if (ch == &#39;-&#39;) { found = true; }
else { i++; }
}

A. finding the first match

B. prompting until a match is found

C. counting matches

D. comparing adjacent values

15. Which loop is preferred when a variable runs from a starting to an ending value with a constant increment or decrement?

A. infinite

B. for

C. nested

D. while