CS110 Practice Quiz #7

for statement




  Choose the best alternative:
  1. The for loop is called a fixed repetition loop because the loop is repeated a fixed number of times.
  2. The loop counter in a for loop is altered after the loop statements are executed in a given iteration.
  3. The for loop is known as a post-test loop.
  4. In the for statement below, i is called a(n) .
    for (int i = 1; i < 5; i++)
        System.out.println('W');
  5. The for loop will always execute at least once. (A one-trip loop.)
    .
  6. What output is produced by the segment of code shown below:
    for (int i = 1; i < 5; i++)
        System.out.println('A');
  7. What output is produced by the segment of code shown below:
    for (int i = 1; i <= 5; i++)
        System.out.println('W');
  8. The segment of code below will result in .
    for (int i = 1; i <= 3;)
        System.out.println('A');
  9. What output is produced by the segment of code shown below:
    for (int x = 3; x >= -1; x--)
        System.out.println(x);
  10. What output is produced by the segment of code shown below:
    for (char c = 'a'; c <= 'd'; c++)
        System.out.println(c);
  11. The output produced by the segment of code shown below is .
    for (int i = 0; i >= 3; i++ )
        System.out.println(i);
  12. What output is produced by the segment of code shown below:
    for (int i = 1; i <= 10; i += 2)
        System.out.println(i);
  13. What output is produced by the segment of code shown below:
    for (int i = 1; i <= 10; i ++)
    {   System.out.println(i);
        i++;
    }




Answers

  1. true
  2. true
  3. false. The for loop is a pre-test loop.
  4. loop control variable
  5. false. The for loop is a zero-trip loop. The do-while loop is a one-trip loop.
  6. AAAA
  7. WWWWW
  8. An infinite loop.
  9. 3210-1
  10. abcd
  11. nothing - the loop statements never execute.
  12. 13579
  13. 13579 NOTE: The loop counter also incremented inside the loop.