1/12
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
What output is produced by the following code?
int count = 0;
while (count < 5)
{
System.out.println(count);
count++;
}
System.out.println("count after loop = " + count);
0
1
2
3
4
count after loop = 5
Can the body of a while loop execute zero times? Can the body of a do-while loop execute zero times?
while loop : Yes
do-while loop : No, a do-while must execute at least once.
What output is produced by the following code?
int count = 0;
do
{
System.out.println(count);
count++;
} while (count < 0);
System.out.println("count after loop = " + count);
0
count after loop = 1
Revise the following code so that it uses a while loop instead of a do-while loop:
Scanner keyboard = new Scanner(System.in);
int number;
do
{
System.out.println("Enter a whole number:");
number = keyboard.nextInt();
System.out.println("You entered " + number);
} while (number > 0);
System.out.println("number after loop = " + number);
Scanner keyboard = new Scanner(System.in);
int 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);
What output is produced by the following code?
int count = 0;
while (count < 5)
{
System.out.println(count);
count;
}
System.out.println("count after loop = " + count);
It's a infinite loop.
Imagen 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 while loop after these statements that ensures that population is positive. If the user enters a population that is either negative or zero, ask the user to enter a non-negative value.
while (population < 0){
System.out.println("Enter a non-negative value");
System.out.print("Enter the population of the city: ");
int population = keyboard.nextInt( );
}
What output is produced by the following code?
for (int n = 1; n
1
2
3
4
What output is produced by the following code?
int n;
for (n = 1; n > 4; n++)
System.out.println(n);
Nothing happens.
What output is produced by the following code?
for (int n = 4; n > 0; n--)
System.out.println(n);
4
3
2
1
What output is produced by the following code?
for (int n = 4; n > 0; n--);
System.out.println(n);
(This is not the same as the previous question. Look carefully.)
n will change to 0, but will not display anything
What output is produced by the following code?
for (double test = 0; test < 3; test = test + 0.5)
System.out.println(test);
0.0
0.5
1.0
1.5
2.0
2.5
Write a for statement that displays the even numbers 2, 4, 6, 8, and 10.
Each number should appear on a separate line. Declare all the variables you use.
for(int n = 2; n < 11; n += 2)
System.out.println(n);
What output is produced by the following code?
for (int count = 0; count
0
0
1
0
1
2