What does the following code print?
int x = -4;
while (x < 0)
{
x++;
System.out.print(x + " ");
}
-3 -2 -1 0
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
Which statement can we use inside of a loop to end it?
System.out.println;
break;
SENTINEL;
else;
break;
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.
Why do we use while loops in Java?
To break out of some block of code
To do something if a condition is true
To repeat some code while a condition is true
To repeat something for a fixed number of times
To repeat some code while a condition is true
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);
}
}
A. You can add x-- after the print statement.
B. You can add x = -1 after the print statement.
C. You can add x++ after the print statement.
Both A and B above.
A and B
What will this while loop do?
while(true)
{
System.out.println("Hello");
}
Print Hello in an infinite loop
Why do we use for loops in Java?
To break out of some block of code
To do something if a condition is true
To do something while a condition is true
To repeat something for a fixed number of times
To repeat something for a fixed number of times
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?
(n + 1) / 2
n / 2
(n - 1) / 2
n
n-1
(n + 1) / 2
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
What does the following code print?
for (int i = 2; i < 9; i++)
{
System.out.print(i + ", ");
}
2, 3, 4, 5, 6, 7, 8,
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?
A. It may return a string that is equal to str2.
B. It may return a string that has no characters in common with str2.
C. It may return a string that is equal to str1.
Both A and C are true
None of these choices will be true.
A and C
What is the general formula for traversing a String character by character?
for(int i = 0; i < string.length(); i++)
{
String character = string.substring(i, i +1);
}
for(int i = 0; i <= string.length(); i++) {
String character = string.substring(i, i +1);
}
for(int i = 0; i < string.substring(i, i+1); i++)
{
String character = string.length();
}
for(int i = string.length(); i > 0; i++)
{
String character = string.substring(i, i +1);
}
1
Given
String str = “RETRIEVER”;
int index = str.substring(1, 4).indexOf(“R”);
what is the value of index?
2
Which of the following will execute without throwing an exception error?
String str1 = “Golden”;
String str2 = “Retriever”;
if (str1.equals(str2))
{
System.out.println(“Golden Retriever!”);
}
String str1 = “Golden”;
String str2 = str1.substring(4); System.out.println(str1 + str2);
None of the above
A and B above.
A and B
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
1 1 1 1 2 2 2 3 3 4
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.
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
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
How often is the inner loop of a nested loop run?
Infinitely
Twice as often as the outer loop
Each time the outer loop runs
One less time than the outer loop runs
Each time the outer loop runs
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
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
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
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
Why is having efficient algorithms important?
It reduces the cost of running a program.
It can improve the speed that programs operate.
It increases the speed of innovations.
Both 1 and 2.
Both 2 and 3.
both 1 and 2
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
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