SJSU/Udacity CS046

Problem Set 5

  1. Loops
  2. Looping a String
  3. Product 1 to n
  4. Count Non-Vowels
  5. Do
  6. Average
  7. Blocks in a Loop
  8. Variable Blocks in a Loop
  9. Coin Toss
  10. Sum of Odd Digits
  11. Random Word Permutations
  12. Get Substrings

Loops

What does this code segment print?

for (int count = 0; count <= 5; count++)
{
   //do something
}
System.out.println(count);
  1. 5
  2. 6
  3. nothing, The code does not compile
  4. nothing. There is an infinite loop

What does this code segment print?

int count = 0;
for (count = 0; count <= 5; count++)
{
   //do something
}
System.out.println(count);
6

What does this code segment print?

int count = 0;
for (count = 5; count >=0; count--)
{
   System.out.print(count + " ");
}
System.out.println(count);
5 4 3 2 1 0 -1

What does this code segment print?

int count = 0;
for (count = 0; count <= 10; count = count + 2)
{
   System.out.print(count + " ");
}
System.out.println(count);
0 2 4 6 8 10 12

How many numbers does this loop print?

for (int i = 5; i >= 0; i--)
{
   System.out.println(i);
}
6

What is printed by this code fragment?

int n = 1;
int sum = 1;
do
{
   sum = sum + n;
   n++;
}
while (sum > 10 * n);
System.out.println(sum);
2

Problem on this page?

Your name:

Your email address:

Problem description:

To protect against spam robots, please answer this simple math problem:
× =