
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
5 questions
AP Computer Science A: <= Unit 4

Flashcard
•
10th - 12th Grade
5 questions
Enhanced For Loops

Flashcard
•
10th - 12th Grade
6 questions
Understanding Loops and Lists

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

Flashcard
•
9th - 12th Grade
10 questions
CS Python Fundamentals Flashcard 4 Practice

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

Flashcard
•
KG
9 questions
Python Review 5/23

Flashcard
•
9th - 12th Grade
10 questions
❄⛄AP CSA - Unit 6 Review

Flashcard
•
9th - 12th Grade
Popular Resources on Wayground
18 questions
Writing Launch Day 1

Lesson
•
3rd Grade
11 questions
Hallway & Bathroom Expectations

Quiz
•
6th - 8th Grade
11 questions
Standard Response Protocol

Quiz
•
6th - 8th Grade
40 questions
Algebra Review Topics

Quiz
•
9th - 12th Grade
4 questions
Exit Ticket 7/29

Quiz
•
8th Grade
10 questions
Lab Safety Procedures and Guidelines

Interactive video
•
6th - 10th Grade
19 questions
Handbook Overview

Lesson
•
9th - 12th Grade
20 questions
Subject-Verb Agreement

Quiz
•
9th Grade
Discover more resources for Computers
40 questions
Algebra Review Topics

Quiz
•
9th - 12th Grade
10 questions
Lab Safety Procedures and Guidelines

Interactive video
•
6th - 10th Grade
19 questions
Handbook Overview

Lesson
•
9th - 12th Grade
20 questions
Subject-Verb Agreement

Quiz
•
9th Grade
40 questions
LSHS Student Handbook Review: Pages 7-9

Quiz
•
11th Grade
24 questions
Scientific method and variables review

Quiz
•
9th Grade
10 questions
Characteristics of Life

Quiz
•
9th - 10th Grade
19 questions
Mental Health Vocabulary Pre-test

Quiz
•
9th Grade