Consider the following code segment. int count = 0; for (int i = 10; i > 0; i--) { for (int j = 0; j < 20; j += 2) { count++; } } System.out.println("count = " + count); What is the output of the code segment?

Unit #4- Loops and Nested Loops

Flashcard
•
Computers
•
9th - 12th Grade
•
Hard
Brendan Kerchner
FREE Resource
Student preview

8 questions
Show all answers
1.
FLASHCARD QUESTION
Front
Back
count = 100
Answer explanation
The outer loop will execute 10 times, and the inner loop will execute 10 times (i.e., from 0 to 19 by 2’s), so the count would be incremented 100 times, so count = 100 is the correct answer.
2.
FLASHCARD QUESTION
Front
Consider the following code segment. for (int i = 0; i <= 4; i++) { String printLine = ""; for (int j = 0; j <= i; j++) { printLine += String.valueOf(j); } System.out.println(printLine); } What will be the output of the code segment?
Back
0 01 012 0123 01234
3.
FLASHCARD QUESTION
Front
Consider the following code segment. int printNum = 0; for (int i = 0; i < 4; i++) { String printLine = ""; for (int j = 0; j <= i; j++) { printLine += String.valueOf(printNum); printNum++; } System.out.println(printLine); } What will be the output of the code segment?
Back
0 12 345 6789
Answer explanation
The outer loop iterates four times so there will be four lines of output. On each iteration of the inner loop, the string format of the integer printNum is appended to printLine (the line to be printed at the bottom of the outer loop). The integer printNum starts at 0, and it iterates i times (the loop control variable), so 0 is the first line printed. At the end of the inner loop, printNum is incremented by 1. On the second iteration of the outer loop, the loop control variable i = 1, so the inner loop will be executed twice, and 12 (the values of printNum) will be printed to the screen. This continues until finally 6789 is printed and the code segment ends.
4.
FLASHCARD QUESTION
Front
Consider the following code segment. for (int i = 0; i < 5; i++) { String printLine = ""; for (int j = 0; j <= i; j++) { printLine = "*" + printLine; } for (int j = 0; j <= (4 - i); j++) { printLine = " " + printLine; } System.out.println(printLine); } What is the output of the following code segment?
Back
* ** *** **** *****
Answer explanation
The outer loop for (int i = 0; i < 5; i++) will execute five times, which corresponds to the number of pattern rows that will be printed. At the top of the outer loop, the string that will be printed to the screen printLine is set to empty so that individual characters can be added to it. We then have two inner loops that follow. The first one, for (int j = 0; j <= i; j++), will add the proper number of "*" characters to printLine based on the current row. The second inner loop, for (int j = 0; j <= (4 - i); j++), will add the proper number of " " (space) characters to printLine based on the current row. At the end of the outer loop, the current contents of printLine are printed to the screen resulting in the following pattern:
*
**
***
****
*****
5.
FLASHCARD QUESTION
Front
Which statements will loop every character of String myString?
Back
I and II
Answer explanation
Statements I and II model the for loop to visit every character of myString: for (int i = 0;i < myString.length();i++). Option III will stop at myString.length() - 1, which means that the last character of myString will not be visited.
6.
FLASHCARD QUESTION
Front
Consider the following code. int count = 0; for (int x = 1; x <= 3; x++) { /* missing loop header */ { count++; } } System.out.println(count); Which loop header will make the code print 6 as the value of count? Options:
for (int y = 0; y <= 2; y++), for (int y = 0; y < 3; y++), for (int y = 2; y >= 0; y--),
for (int y = 3; y > 0; y--),
for (int y = 0; y < x; y++)
Back
for (int y = 0; y < x; y++)
7.
FLASHCARD QUESTION
Front
Consider the following code segment. String str = "Self-belief"; String reverse = ""; String ithLetter; for(int i=0; i < str.length(); i++) { ithLetter = str.substring(i,i+1); reverse = ithLetter + reverse; } System.out.println(reverse); What will be the output of the above code segment?
Back
feileb-fleS
Answer explanation
The for loop is designed to start at the first letter of string str and to add that to reverse. After the first iteration, reverse = "S". On the second iteration of the for loop, i = 1, so append the first "e" in str to the beginning of reverse, so reverse now equals "eS".
And the loop will continue until the end of the string str index.
8.
FLASHCARD QUESTION
Front
Consider the code segment. int count = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 5; j++) { count++; } } System.out.println("count = " + count); What will be output as a result of running the code segment?
Back
count = 50
Similar Resources on Quizizz
10 questions
CS Python Fundamentals Flashcard 4 Practice

Flashcard
•
9th - 12th Grade
8 questions
2D array flashcard

Flashcard
•
9th - 12th Grade
7 questions
[862] Esp_Unity M1L2

Flashcard
•
KG
7 questions
[862] Esp_Unity M1L2

Flashcard
•
KG
12 questions
CSP-A114 Flashcard

Flashcard
•
9th - 12th Grade
10 questions
Arrays

Flashcard
•
10th - 12th Grade
6 questions
lists/tuples

Flashcard
•
9th - 12th Grade
6 questions
Python -- Inputs

Flashcard
•
9th - 12th Grade
Popular Resources on Quizizz
15 questions
Character Analysis

Quiz
•
4th Grade
17 questions
Chapter 12 - Doing the Right Thing

Quiz
•
9th - 12th Grade
10 questions
American Flag

Quiz
•
1st - 2nd Grade
20 questions
Reading Comprehension

Quiz
•
5th Grade
30 questions
Linear Inequalities

Quiz
•
9th - 12th Grade
20 questions
Types of Credit

Quiz
•
9th - 12th Grade
18 questions
Full S.T.E.A.M. Ahead Summer Academy Pre-Test 24-25

Quiz
•
5th Grade
14 questions
Misplaced and Dangling Modifiers

Quiz
•
6th - 8th Grade
Discover more resources for Computers
17 questions
Chapter 12 - Doing the Right Thing

Quiz
•
9th - 12th Grade
30 questions
Linear Inequalities

Quiz
•
9th - 12th Grade
20 questions
Types of Credit

Quiz
•
9th - 12th Grade
20 questions
Taxes

Quiz
•
9th - 12th Grade
17 questions
Parts of Speech

Quiz
•
7th - 12th Grade
20 questions
Chapter 3 - Making a Good Impression

Quiz
•
9th - 12th Grade
20 questions
Inequalities Graphing

Quiz
•
9th - 12th Grade
10 questions
Identifying equations

Quiz
•
KG - University