coding unit 4

5.0(1)
studied byStudied by 4 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/28

flashcard set

Earn XP

Description and Tags

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

29 Terms

1
New cards
What does the following code print?

int x = -4;

while (x < 0)

{

x++;

System.out.print(x + " ");

}
\-3 -2 -1 0
2
New cards
How many times will the following code print out the value of x?

public static void main(String\[\] args)

{

int x = 1;

while (x > 10)

{

System.out.println(x);

x--;

}

}
0 times
3
New cards
\
Which statement can we use inside of a loop to end it?

\

1. System.out.println;
2. break;
3. SENTINEL;
4. else;

2. break;
4
New cards
\
Why does the following code produce an infinite loop? Choose the best answer.

public static void main(String\[\] args)

{

int x = 7;

while (x > 0)

{

System.out.println(x);

}

}
The loop doesn’t ever change the value of x in the loop, so it never ends.
5
New cards
\
Why do we use while loops in Java?

\

1. To break out of some block of code
2. To do something if a condition is true
3. To repeat some code while a condition is true
4. To repeat something for a fixed number of times

3. To repeat some code while a condition is true
6
New cards
\
Which statement could be added to the while loop below so that it doesn’t run indefinitely?

public static void main(String\[\] args)

{

int x = 7;

while (x > 0)

{

System.out.println(x);

}

}

\

1. A. You can add x-- after the print statement.
2. B. You can add x = -1 after the print statement.
3. C. You can add x++ after the print statement.
4. Both A and B above.
A and B
7
New cards
What will this while loop do?

while(true)

{

System.out.println("Hello");

}
Print Hello in an infinite loop
8
New cards
\
Why do we use for loops in Java?

\

1. To break out of some block of code
2. To do something if a condition is true
3. To do something while a condition is true
4. To repeat something for a fixed number of times

4. To repeat something for a fixed number of times
9
New cards
\
Consider the following loop, where n is some positive integer.

for (int i = 0; i < n; i += 2)

{

if (/\* condition to test \*/)

{

/\* perform some action \*/

}

}

\
In terms of n, which Java expression represents the maximum number of times that /\* perform some action \*/ could be executed?

\

1. (n + 1) / 2
2. n / 2
3. (n - 1) / 2
4. n
5. n-1

1. (n + 1) / 2
10
New cards
Which for loop will properly print “hello” 10 times?

A

for(int i = 0; i < 10; i++)

{

System.out.println("hello");

}

B

for(int i = 10)

{

System.out.println("hello");

}

C

for(int i = 0; i++; i < 10)

{

System.out.println("hello");

}

D

for(int i = 10; i < 0; i++)

{

System.out.println("hello");

}
A
11
New cards
What does the following code print?

for (int i = 2; i < 9; i++)

{

System.out.print(i + ", ");

}
2, 3, 4, 5, 6, 7, 8,
12
New cards
\
Consider the mystery method below.

public static String mystery(String str1, String str2)

{

int index = str1.indexOf(str2);

return str1.substring(index, index + str2.length());

}

What is true about mystery?

\

1. A. It may return a string that is equal to str2.
2. B. It may return a string that has no characters in common with str2.
3. C. It may return a string that is equal to str1.
4. Both A and C are true
5. None of these choices will be true.
A and C
13
New cards
\
What is the general formula for traversing a String character by character?

\

1. for(int i = 0; i < string.length(); i++)

{

String character = string.substring(i, i +1);

}


2. for(int i = 0; i
1
14
New cards
Given

String str = “RETRIEVER”;

int index = str.substring(1, 4).indexOf(“R”);

what is the value of index?
2
15
New cards
\
Which of the following will execute *without* throwing an exception error?

\

1. String str1 = “Golden”;

String str2 = “Retriever”;

if (str1.equals(str2))

{

System.out.println(“Golden Retriever!”);

}


2. String str1 = “Golden”;

String str2 = str1.substring(4); System.out.println(str1 + str2);


3. None of the above
4. A and B above.
A and B
16
New cards
\
Consider the following method.

public void processString (String str)

{

str = str.substring(2, 3) + str.substring(1, 2) + str.substring(0, 1);

}

What is printed as result of executing the following statements (in a method in the same class)?

String str = "Frog";

processString(str); System.out.println(str);
Frog
17
New cards
What will this code produce?

\
for (int j = 1; j
1 1 1 1
2 2 2
3 3
4
18
New cards
What does the following code print?

for (int i = 1; i < 6; i++)

{

for (int y = 1; y
A rectangle of 5 rows with 4 stars per row.
19
New cards
How many stars are output when the following code is executed?

\
for (int i = 0; i < 5; i++)

{

for (int j = 0; j < 10; j++)

{

System.out.println("\*");

}

}
50
20
New cards
\
What is printed as a result of executing the following code snippet?

for (int k = 0; k < 25; k = k + 2)

{

if (k % 3 == 0)

{

System.out.print(k + " ");

}

}
0 6 12 18 24
21
New cards
\
How often is the inner loop of a nested loop run?

\

1. Infinitely
2. Twice as often as the outer loop
3. Each time the outer loop runs
4. One less time than the outer loop runs

3. Each time the outer loop runs
22
New cards
Consider the following code snippet. Is count < 5 always true, always false, or sometimes true/sometimes false at point 2?

int count = 0;

while (count < 5)

{

System.out.println("CodeHS Rocks!");

count++;

\
// point 2

}
sometimes true/sometimes false
23
New cards
Consider the following code snippet. Is count < 5 always true, always false, or sometimes true/sometimes false at point 1?

int count = 0;

while (count < 5)

{

// point 1

System.out.println("CodeHS Rocks!");

count++;

\
}
always true
24
New cards
Consider the following code snippet. Is count < 5 always true, always false, or sometimes true/sometimes false at point 3?

int count = 0;

while (count < 5)

{

\
System.out.println("CodeHS Rocks!");

count++;

\
}

// point 3
always false
25
New cards
How many times does the following loop iterate?

// x has been initialized with a positive int value

for (int count = 0; count
x+1 times
26
New cards
\
Why is having efficient algorithms important?

\

1. It reduces the cost of running a program.
2. It can improve the speed that programs operate.
3. It increases the speed of innovations.
4. Both 1 and 2.
5. Both 2 and 3.
both 1 and 2
27
New cards
\
How many times does the following loop execute?

// x has been initialized with a positive int value greater than 5

int count = 5;

while (count < x)

{

count++;

}
x-5 times
28
New cards
How many times does the following loop iterate?

// x has been initialized with a positive int value

int count = 0; while (count < x)

{

count++;

}
x times
29
New cards