Section 7.11 Assessment: While and For Loops
Subgoals for Evaluating a Loop.
Exercises Exercises
- Prints the value in num
- Prints the sum of all the integers between 1 and num, inclusive
- Prints the sum of all even integers between 1 and num, inclusive
- Prints the sum of all odd integers between 1 and num, inclusive
- Nothing is printed, it is an infinite loop
- 5 20 5
- 5 20 10
- 5 25 5
- 5 25 10
- 30 25 10
2.
3.
4.
Q4: Considering the following code, which best describes what the code does?
int var = 0;
int num = 50;
for (int x = 1; x <= num; x += 2) {
var = var + x;
}
System.out.println(var);
5.
Q5: What is printed as a result of executing the following code segment?
int typeA = 0;
int typeB = 0;
int typeC = 0;
for (int k = 1; k <= 50; k++) {
if (k % 2 == 0 && k % 5 == 0)
typeA++;
if (k % 2 == 0)
typeB++;
if (k % 5 == 0)
typeC++;
}
System.out.println("%d %d %d", typeA, typeB, typeC);
You have attempted 1 of 2 activities on this page.