Java An Introduction to Problem Solving and Programming Chapter 4.1 (SELF-TEST QUESTIONS)

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/12

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 9:43 PM on 9/15/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

13 Terms

1
New cards

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

2
New cards

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.

3
New cards

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

4
New cards

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);

5
New cards

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.

6
New cards

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( );

}

7
New cards

What output is produced by the following code?

for (int n = 1; n

1

2

3

4

8
New cards

What output is produced by the following code?

int n;

for (n = 1; n > 4; n++)

System.out.println(n);

Nothing happens.

9
New cards

What output is produced by the following code?

for (int n = 4; n > 0; n--)

System.out.println(n);

4

3

2

1

10
New cards

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

11
New cards

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

12
New cards

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);

13
New cards

What output is produced by the following code?

for (int count = 0; count

0

0

1

0

1

2