AP Computer Science Unit 4 Quiz Project Stem

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

1/9

encourage image

There's no tags or description

Looks like no tags are added yet.

10 Terms

1
New cards

int count = 4;

while (count <= 7) {

count++;

System.out.print(count + " "); }

What are the first and last numbers output?

5 8

2
New cards

When would it be more beneficial to use a while loop instead of a for loop?

When you need your loop to be controlled by user input.


A while loop would generally work better for this than a for loop.

3
New cards

Choose which three values should replace the blanks in the for loop so that it loops through the numbers: 3 6 9 12 15.


Note that the choices will fill in the three blanks in the order which they appear.

for(int i = ______ ; i ______ ; i ______ )

{

System.out.print(i + " ");

}

3, <= 15, += 3


The first value of i is 3, the last value for which i <= 15 is satisfied is 15 and i increases by 3 each time, thus printing 3 6 9 12 15

4
New cards

How many times will the following loop repeat?

int num = 49;

while (num > 0)

{

if (num % 2 == 0)

{

num++;

}

else

{

num--;

}

}

Infinite Loop


The loop will enter with num=49. As 49%2 is 1 num will be decreased to 48. On the next run as 48%2 is 0 num will be increased to 49. The loop will then enter with num=49 again, etc.

5
New cards

The following loop is intended to print the even numbers from 20 to 26 inclusive:


int x = 20;

while (x < 26)

{

System.out.print(x);

x++;

}

What would you need to change in order for the code to work correctly?

The x++ needs to be x += 2 and the x < 26 needs to be <=

6
New cards

What is output to the screen by the following code?


int num = 1987;

while (num > 0)

{

num = num / 10;

System.out.print(num % 10 + " ");

}

8 9 1 0


num is divided by 10 at the start of each run of the while loop, then the last digit is printed. On the first run num is set to 1987/10 = 198, so 8 is printed. On the last run, num is set to 1/10 = 0, so 0 is printed.

7
New cards

Consider the following code segment:

int c = 1;

while (c <= 10)

{

if (c % 3 == 1)

{

System.out.print(c + " ");

}

c++;

}


Which of the following produce the exact same output?


(1)

int c = 1;

while (c <= 10)

{

c++;

if (c % 3 == 1)

{

System.out.print(c + " ");

}

}


(2)

int c = 1;

while (c <= 10)

{

System.out.print(c + " ");

c += 3;

}


(3)

int c = 0;

while (c <= 10)

{

c++;

if ( c % 3 == 1)

{

System.out.print(c + " ");

}

}

2 and 3

8
New cards

Choose which three values should replace the blanks in the for loop so that it loops through the numbers: 10 9 8 7 6 5 4 3 2 1.

Note that the choices will fill in the three blanks in the order which they appear.


for (int i = ______; i ______; i ______)

{

System.out.print(i + " ");

}

10, >= 1, --


The first value of i is 10, the last value for which i >= 1 is satisfied is 1 and i decreases by 1 each time.

9
New cards

Consider the following code segment.


int a = 0;

while (/* missing code */)

{

System.out.print((a + 5) + " ");

a += 10;

}

Consider the following possible replacements for /* missing code */.


(1) a < 45

(2) a < 40

(3) a != 45


Which of the proposed replacements for /* missing code */ will cause the code segment to print only the values 5 15 25 35?

II only


This is correct. If expression II is used, then the while loop runs when a = 0, a = 10, a = 20 and a = 30 but not when a = 40 since the condition a < 40 is no longer satisfied. Since the loop prints values 5 greater than a, the values printed are 5 15 25 35 . If expression I is used, 45 will be printed in addition to these numbers as when a = 40, the condition a < 45 is satisfied, and a + 5 = 45. If expression III is used then the program will result in an infinite loop, since a, which begins with a value of 0 and is increased by 10 each run of the while loop, is never set to 45.

10
New cards

Consider the following code segment.


for (int k = 30; k > 0; k = k - 3)

{

if (k % 5 == 0)

{

System.out.print(k + " ");

}

}


What is printed as a result of executing the code segment?

30 15


This is correct. The initial value of k is 30, and it is reduced by 3 on each iteration of the for loop as long as it is greater than 0. Therefore k takes values of positive multiples of 3, beginning with 30 and working downwards. The code causes k to be printed only if its current value satisfies k % 5 == 0. Therefore the values of k printed are positive integers smaller than 30 which are multiples of both 3 and 5: the numbers printed therefore are 30 and 15.