Unit 4 Flashcards

1
Q

difference between selection and repetitions?

A

if vs. loops (while or for loops)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

ALWAYS USE CURLIES?

A

real

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

3 parts of a for loop header

A
  1. initialized loop variable
  2. test loop variable in loop header
  3. change loop variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how do you create an infinite while loop?

A

while(true)
{
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

name of the stopping value of a loop

A

sentinel value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

true/false

all 3 parts of a for statement should all refer to the same variable

A

technically possible to not, but def SHOULD (true)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

if it exists within the method, does a return statement HAVE to be the end of a method?

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

can you put return constant statements in an if-else in a loop, such as with

while (a>=0)
if (word.substring(a, a+1).equals(c))
{
return a;
}
else
return -1;
a–
“? why?

A

no, as that return statement returns that number outside of the loop and ends it prematurely

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

is there an empty string after EVERY string?

A

yes, this is why youre able to traverse strings until the very end (at indexOf = -1, whatever variable = “”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

can you declare a variable in a loop?

A

yes, but it only remains in the scope of that loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

4-1-5: Consider the following code segment. What is count’s value after running this code segment? (To trace through the code, keep track of the variable count and its value through each iteration of the loop.)

int count = 1;
while (count <= 10)
{
count *= 2;
}
count = count - 10;
A. 0
B. 1
C. 16
D. 6

A

D

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

4-1-6: What does the following code print? (To trace through the code, keep track of the variable x and its value, the iteration of the loop, and the output every time through the loop.)

int x = -5;
while (x < 0)
{
x++;
System.out.print(x + “ “);
}
A. 5 4 3 2 1
B. -5 -4 -3 -2 -1
C. -4 -3 -2 -1 0

A

C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

generally, in loops, when you want to count to a number and you start at 0, you use ______ (<, <=) and when you want to count to a number and you start at 1, you use _____ (<, <=)

A

<

<=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly