
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
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.
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 Wayground
9 questions
Python Review 5/23
Flashcard
•
9th - 12th Grade
10 questions
CODEHS Looping Unit 5 Review
Flashcard
•
9th - 12th Grade
8 questions
HTML Tags (Basic) Flashcard
Flashcard
•
11th - 12th Grade
7 questions
Instructiuni Repetitive C++
Flashcard
•
10th Grade
5 questions
User Inputs JavaScript
Flashcard
•
10th Grade
13 questions
WD 1st Semester Test
Flashcard
•
9th - 12th Grade
11 questions
Deklaracja dwuwymiarowej tablicy C++
Flashcard
•
10th Grade
8 questions
Warm Up- EduBlocks- Terms and Concepts
Flashcard
•
9th - 12th Grade
Popular Resources on Wayground
20 questions
Brand Labels
Quiz
•
5th - 12th Grade
11 questions
NEASC Extended Advisory
Lesson
•
9th - 12th Grade
10 questions
Ice Breaker Trivia: Food from Around the World
Quiz
•
3rd - 12th Grade
10 questions
Boomer ⚡ Zoomer - Holiday Movies
Quiz
•
KG - University
25 questions
Multiplication Facts
Quiz
•
5th Grade
22 questions
Adding Integers
Quiz
•
6th Grade
10 questions
Multiplication and Division Unknowns
Quiz
•
3rd Grade
20 questions
Multiplying and Dividing Integers
Quiz
•
7th Grade
Discover more resources for Computers
18 questions
Artificial Intelligence and Machine Learning Quiz
Quiz
•
9th Grade
35 questions
Computer Terminology Exercise #1 ( Multiple Choice)
Quiz
•
9th Grade
20 questions
Computer Components Exercise #4 - CTEA
Quiz
•
9th - 12th Grade
30 questions
Computer Terminology Exercise #1 ( Fill Ins)
Quiz
•
9th Grade