ap comp sci unit 4 (lessons 1-3)

¡Supera tus tareas y exámenes ahora con Quizwiz!

Consider the following code, assume a and b are integers and have been initialized. What is output when a = 55 and b = 29? int f = 0; int d = 2; while (d <= a) { if (a % d == 0 && b % d == 0) { f = d; } d++; } System.out.println(f);

0

Consider the following code: int a = 0; int b = 3; while(a < 4 && b < 4) { System.out.println(a + " " + b); a++; b--; } What is output?

0 3 1 2 2 1 3 0

Consider the following code: int n = 70; while(n > 35) { System.out.print(n % 10 + " "); n -= 5; } What is output?

0 5 0 5 0 5 0

What is output? for (int i = 1; i <= 6; i++) { System.out.print(i + " "); }

1 2 3 4 5 6

What is output by the following code segment? for (int i = 12; i >= 3; i -= 3) { System.out.print(i + " "); }

12 9 6 3

Consider the following code: int n = 14; while(!(n % 2 == 0 && n % 3 == 0)) { System.out.println(n); n += 5; } What is the last number printed by this code?

19

Choose which three values should replace the blanks in the for loop so that it loops through the numbers: 2 4 6 8. 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 + " "); }

2, <= 8, += 2

What are the first and last numbers printed by the following code: int x = 20; while (x <= 25) { System.out.print(x + " "); x++; }

20 25

What is output to the screen by the following code: int n = 4; while(n < 8) { n++; System.out.println(n + " "); }

5 6 7 8

Consider the following code: int n = 5; while(n <= 18) { n += 2; System.out.print(n + " "); } What is output?

7 9 11 13 15 17 19

Often on the AP exam they ask you to print out the first and last number printed by a loop. What are the first and last values printed by: int n = 3; while(n < 23) { n += 5; System.out.print(n + " "); }

8 23

What is output to the screen by the following code: int n = 10; while(6 < n) { n--; System.out.println(n + " "); }

9 8 7 6

What is the difference between the two loops? I. int c = 0; while (c != 13){ c = scan.nextInt(); } II. int c = 0; while (c != 13){ c++; }

I stops by using user input, II stops by using a count variable.

What is one potential problem with the following loop? int n = 7; while(n != 6) { System.out.println(n); }

Since n does not change the loop will not stop

What does the following loop do: int num = scan.nextInt(); int sum = 0; while (num > 0) { sum += num % 10; num /= 10; } System.out.print(sum);

Sums each digit of the number the user enters.

What is one potential problem with the following loop? Scanner scan = new Scanner(System.in); System.out.print("Enter integers. Enter -1 to exit."); System.out.println(" Count of integers entered before -1 will be returned."); int n = 0; int c = 0; while(n != -1) { n = scan.nextInt(); c++; } System.out.println(c);

The loop counts when the user enters -1 into the keyboard, so the count will be one too many

i++

This tells the loop what to count by. More specifically it increments or changes the loop control variable in a set way. After the loop has finished running, the control variable is incremented in preparation for the next iteration of the loop.

i < 10

This tells the loop when to stop. This boolean statement is checked before every iteration of the loop after the increment/initialization statement is run. The loop exits when it is false.

When might it be more helpful to use a while loop instead of a for loop?

When you do not know how many times a loop will repeat.

the difference between if statements and while loops

a while loop will keep executing the code over and over as long as the condition is true

for loops

another kind of loop that focuses on counting

3 parts of a for loop

i = 0, i < 10, i++

the two components of while loops

the condition, and the code to execute if the condition is true

i = 0

this initializes the loop. It runs once on the first iteration of the loop to set the starting value for the loop control variable.

for loop v. while loop

we use a for loop instead of a while loop when we have a definite starting and stopping point, or when we know exactly how many times we want to repeat something


Conjuntos de estudio relacionados

Chapter 28- Face and Neck Injuries

View Set

II Lecture Chapter 14 Short Answer: Breast Surgery pp 314

View Set

Acting Ethically and Socially Responsible

View Set

Pearson VUE Exam Prep: Financing and Settlement

View Set

Nutrition Chapter 13 Water Soluble vitamins

View Set

Introduction to psychology M 32: LearningCurve 32a. Stress and Illness

View Set

XCEL Life and Health Insurance Test Review

View Set