Answers to Self-Test Questions

  1. 1.

    0
    1
    2
    3
    4
    count after loop = 5
  2. 2. Yes, the body of a while loop can execute zero times. No, the body of a do-while loop must execute at least once.

  3. 3.

    0
    count after loop = 1
  4. 4.

    Scanner keyboard = new Scanner(System.in); 
    int number;
    System.out.println("Enter a whole number:”);
    number = keyboard.nextInt();
    System.out.println("You entered " + number); 
    while (number > 0)
    {
         System.out.println("Enter a whole number:”);
         number = keyboard.nextInt();
         System.out.println("You entered " + number);
    }
    System.out.println("number after loop = " + number);
  5. 5. This is an infinite loop. The println statement after the loop will never be executed. The output begins

    0
    −1
    −2
    −3
    .
    .
    .
  6. 6.

    while (population <= 0) { System.out.println("Population ...

Get Java: An Introduction to Problem Solving and Programming, 8th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.