J | | SELF-TEST QUESTIONS
1. What output is produced by the following code?
tnt count = 0;
while (count < 5)
1
System. out.printIn(count) ;
count++;
t
System.out.printIn("count after loop =
+ count);
2. Can the body of a white loop execute zero times? Can the body of a
do-wiiTe loop execute zero times?
3. What output is produced by the following code?
int count
do
{
system. out.printin(count) ;
count++;
} while (count < 0);
System.out.printIn("count after loop =
* + count);
4. Revise the following code so that it uses a whte loop instead of a do-
white loop:
Scanner keyboard = ney Scanner(Systen.in) ;
int number;
do
{
System.out.printIn("Enter a whole nunber:");
number = keyboard.nextint( ;
SyStem.out.printin("You entered ” + number);
} while (number > 0);
System.out.printIn("number after loop = " + number);
5. What output is produced by the following code?
int count =
while (count < 5)
t
system. out printin(count) ;
count;
+
System.out.printIn¢"count after loop = " + count);
6. Imagine a program that reads the population of a city using the following
statements:
System.out.print("Enter the population of the city:
int population = keyboard.nextInt( );
Write a whe loop after these statements that ensures that population is
positive. If the user enters 2 population that is either negative or zero, ask
the user to enter a nonnegative value.