If x is an int where x = 1, what will x be after the following loop terminates?
while (x < 100)
x -= 2;
infinite loop
How many times does the code snippet below display "Hello"?
int i = 0;
while (i != 7)
{
System.out.println("Hello");
i++;
}
6
What is the output of the code snippet given below?
String s = "abcde";
int i = 1;
do
{
if (i > 1)
{
System.out.print(s.substring(i, i + 1));
}
}
while (i < 5);
no output, infinite loop, i doesn't increase
Name the 3 types of loops you've learned so far
for loop, while loop, do-while loop
What is a traversal?
Something that goes through , loops through another thing. TO traverse means to go through.
If x is an int where x = 1, what will x be after the following loop terminates?
while (x < 10)
x *= 2;
16
What is the output of the following code snippet?
int i = 1;
while (i < 10)
{
System.out.print(i + " ");
i = i + 2;
if (i == 5)
{
i = 9;
}
}
1 3 9
What is the output of the code snippet given below?
String s = "aeiou";
int i = 0;
do
{
System.out.print(s.substring(i, i + 1));
i++;
if (i >= 2)
{
i = 5;
}
}
while (i < 5);
ae
What string method would you use if you wanted to print out one letter at a time?
substring
How would you check to see if a number is odd?
if ( i % 2 == 1)
(number is odd if this condition is met)
Given the following code, where x = 0, what is the resulting value of x after the for-loop terminates?
for(int i=0;i<5;i++){
x += i; }
10
What is the output of the code snippet given below?
String s = "12345";
int i = 2;
while (i < 5)
{
System.out.print(s.substring(i, i + 1));
i+=2;
}
3 5
What is the difference between a while and do-while loop?
A do-while loop will execute at least once no matter what the condition is, while a while loop will only execute if the condition is true.
Initialization (int i = 0)
Condition ( i < word.length)
increment/decrement (i++ or i--
When evaluating a nested loop, which loop executes first?
The inner loop ( remember minutes hand vs hours hand reference )
How many times will the following loop iterate?
int x = 10;
while (x > 5)
{ System.out.println(x);
x--; }
5 times
What is the output of the code snippet given below?
String s = "12345";
int i = 1;
while (i < 5)
{
System.out.print(s.substring(i, i + 1));
i+=3;
}
2 5
What does this code do?
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum = sum + i * 2;
}
produces the sum of the first n even numbers
for (int i = 0; i < 100; i++)
for (int j = 0; j < 10; j++)
how many iterations will take place?
1000 iterations
The following nested loop structure will execute how many times?
for(int j = 0; j < 10; j++)
for(int k = 0; k <100; k++)
1000
What is the output of the code snippet given below?
int i = 0;
while (i != 9)
{
System.out.print(i + " ");
i = i + 2;
}
infinite loop
How many times does the following loop run?
int i = 0;
int j = 0;
do
{
System.out.println("" + i + ";" + j);
i++;
if (i % 2 == 0)
{
j--;
}
}
while (j >= 1);
one time
When will the loop in the following code snippet stop?
Scanner in = new Scanner(System.in);
double sum = 0;
int count = 0;
System.out.print("Enter values: ");
do
{
double value = in.nextDouble();
sum = sum + value;
count++;
System.out.print("Enter values, Q to quit: ");
}
while (in.hasNextDouble() && count < 10);
When the count is greater than 10 or enters an alphanumeric character
Consider the following code segment.
for (int i = 1; i < 5; i ++)
for (int k = i; k > 2; k--)
System.out.print(k + “ “);
What is printed as a result of executing the code segment?
343, despite k's condition initially not being true, i will still increase and then eventually k's condition will be true, thus allowing for the compiler to print 3 then 43 on the next iteration and then it breaks once i's condition is no longer true.
What will be the final output of the following code snippet when a user enters input values in the order 10, 20, 30, 40, 40, and -3?
public static void main(String[] args)
{
double sum = 0;
int count = 0;
double salary = 0;
double average = 0;
Scanner reader = new Scanner(System.in);
System.out.println("Enter salaries (-1 to stop): ");
while (salary != -1)
{
salary = reader.nextInt();
if (salary != -3)
{
sum = sum + salary;
count++;
}
}
if (count > 0)
{
average = sum / count;
System.out.println("The Average Salary: " + average);
}
else
{
System.out.println("No data!");
}
return 0;
}
28.0