
Unit #4- Loops and Nested Loops
Flashcard
•
Computers
•
9th - 12th Grade
•
Practice Problem
•
Hard
Brendan Kerchner
FREE Resource
Student preview

8 questions
Show all answers
1.
FLASHCARD QUESTION
Front
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?
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.
Access all questions and much more by creating a free account
Create resources
Host any resource
Get auto-graded reports

Continue with Google

Continue with Email

Continue with Classlink

Continue with Clever
or continue with

Microsoft
%20(1).png)
Apple
Others
Already have an account?
Similar Resources on Wayground
12 questions
Tires
Flashcard
•
9th - 12th Grade
10 questions
Daily Class Opener 01 - HTML and CSS Intro
Flashcard
•
9th - 12th Grade
10 questions
Arrays
Flashcard
•
10th - 12th Grade
9 questions
CIvics Questions #41-50
Flashcard
•
9th - 12th Grade
10 questions
Classroom Procedures and Policies
Flashcard
•
9th Grade - University
12 questions
AP CSP Loops Practice
Flashcard
•
9th - 12th Grade
9 questions
A-Frame Review
Flashcard
•
10th Grade
12 questions
Code.org - Unit 6 Lists, Loops, and Traversals
Flashcard
•
10th Grade - University
Popular Resources on Wayground
15 questions
Fractions on a Number Line
Quiz
•
3rd Grade
20 questions
Equivalent Fractions
Quiz
•
3rd Grade
25 questions
Multiplication Facts
Quiz
•
5th Grade
54 questions
Analyzing Line Graphs & Tables
Quiz
•
4th Grade
22 questions
fractions
Quiz
•
3rd Grade
20 questions
Main Idea and Details
Quiz
•
5th Grade
20 questions
Context Clues
Quiz
•
6th Grade
15 questions
Equivalent Fractions
Quiz
•
4th Grade