coding unit 4

studied byStudied by 3 people
5.0(1)
learn
LearnA personalized and smart learning plan
exam
Practice TestTake a test on your terms and definitions
spaced repetition
Spaced RepetitionScientifically backed study method
heart puzzle
Matching GameHow quick can you match all your cards?
flashcards
FlashcardsStudy terms and definitions
Get a hint
Hint

What does the following code print?

int x = -4;

while (x < 0)

{

x++;

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

}

1 / 28

flashcard set

Earn XP

Description and Tags

29 Terms

1

What does the following code print?

int x = -4;

while (x < 0)

{

x++;

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

}

-3 -2 -1 0

New cards
2

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

New cards
3

Which statement can we use inside of a loop to end it?

  1. System.out.println;

  2. break;

  3. SENTINEL;

  4. else;

  1. break;

New cards
4

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.

New cards
5

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

  1. To repeat some code while a condition is true

New cards
6

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

New cards
7

What will this while loop do?

while(true)

{

System.out.println("Hello");

}

Print Hello in an infinite loop

New cards
8

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

  1. To repeat something for a fixed number of times

New cards
9

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

New cards
10

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

New cards
11

What does the following code print?

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

{

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

}

2, 3, 4, 5, 6, 7, 8,

New cards
12

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

New cards
13

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);

}

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

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

}

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

{

String character = string.length();

}

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

{

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

}

1

New cards
14

Given

String str = “RETRIEVER”;

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

what is the value of index?

2

New cards
15

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!”);

}

  1. String str1 = “Golden”;

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

  1. None of the above

  2. A and B above.

A and B

New cards
16

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

New cards
17
What will this code produce?

\
for (int j = 1; j

1 1 1 1 2 2 2 3 3 4

New cards
18

What does the following code print?

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

{

for (int y = 1; y <= 4; y++)

{

System.out.print("*");

}

System.out.println();

}

A rectangle of 5 rows with 4 stars per row.

New cards
19

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

New cards
20

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

New cards
21

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

  1. Each time the outer loop runs

New cards
22

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

New cards
23

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

New cards
24

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

New cards
25

How many times does the following loop iterate?

// x has been initialized with a positive int value

for (int count = 0; count <= x; count++) {

System.out.print("*");

}

x+1 times

New cards
26

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

New cards
27

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

New cards
28

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

New cards
29
New cards

Explore top notes

note Note
studied byStudied by 19 people
107 days ago
5.0(2)
note Note
studied byStudied by 12 people
844 days ago
4.0(1)
note Note
studied byStudied by 31 people
831 days ago
5.0(1)
note Note
studied byStudied by 17 people
919 days ago
5.0(2)
note Note
studied byStudied by 28 people
35 days ago
5.0(2)
note Note
studied byStudied by 18 people
837 days ago
5.0(1)
note Note
studied byStudied by 12 people
770 days ago
5.0(1)
note Note
studied byStudied by 4337 people
646 days ago
5.0(6)

Explore top flashcards

flashcards Flashcard (123)
studied byStudied by 34 people
678 days ago
5.0(1)
flashcards Flashcard (29)
studied byStudied by 12 people
573 days ago
5.0(1)
flashcards Flashcard (82)
studied byStudied by 16 people
36 days ago
5.0(1)
flashcards Flashcard (36)
studied byStudied by 21 people
152 days ago
5.0(2)
flashcards Flashcard (30)
studied byStudied by 1 person
798 days ago
5.0(1)
flashcards Flashcard (223)
studied byStudied by 19 people
450 days ago
5.0(1)
flashcards Flashcard (65)
studied byStudied by 4 people
198 days ago
5.0(1)
flashcards Flashcard (87)
studied byStudied by 3 people
335 days ago
5.0(1)
robot