Module 04: Iteration Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What would the method call myMethod(“Karel The Dog”, ‘e’) output?

A

3

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

What kind of error would the method call myMethod(“Frog”) cause?

public void myMethod(String x)

{

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

{

System.out.println(x.substring(i, i+1));

}

}

  1. Compile Time Error: Missing return value
  2. Runtime Error: String index out of range
  3. Compile Time Error: Unexpected return value
  4. Compile Time Error: Syntax Error in method definition
A
  1. Runtime Error: String index out of range
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What will the call to the method funWithNumbers(314159) print to the screen?

A

314159

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

What does the call to the method someMethod(3,1) output?

A

10

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

What will the call to method patternGrid(3,4,’#’) print?

A

####

####

####

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

What would the call to method divideByTen(340) output?

public int divideByTen(int num)

{

while(num / 10 >= 10)

{

num /= 10;

}

return num;

}

A

34

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

Consider the following method:

What is returned as a result of the call mystery(“Lunchbox”, 4)?

A

xoxbox

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

Consider the following code segment:

int x = 10;

int y = 2;

int count = 1;

while(x > y)

{

x /= y;

count++;

}

What will the value of count be after executing the code segment?

A

3

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

What will the final result be when run in the console?

A

* * * *

* * *

* *

*

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

Consider the following code segment:

String word = “Cafeteria”;

for(/* missing condition */)

{

System.out.print(word.substring(i+1,i+2) + “ “);

}

The code segment is intended to print every other letter in the word, starting with index 0, to produce the result C f t r a. Which of the following can be used to replace /* missing condition */ so that the code segment works as intended?

  1. int i = 0; i < word.length(); i+=2
  2. int i = -1; i < word.length(); i+=2
  3. int i = 0; i < word.length(); i++
  4. int i = -1; i <= word.length(); i+=2
  5. int i = word.length() - 1; i < -1 ; i-=2
A
  1. int i = -1; i < word.length(); i+=2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Consider the following code segment:

Which of the following for loops would return the same value if System.out.println(sum) were printed?

III only

II, III and IV

II only

II and III

III and I

A

II and III

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

Consider the following method:

What would the value of sum be after the method call countPs(“Peter Piper picked a pack of pickled peppers”)?

A

9

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

How many times will the word “Heyo!” be printed in the following code segment?

A

15

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

Consider the following code segment:

int num = 8;

for(int i = num; i > 0; i -= 3) //Line 2

{

System.out.print(“ “ + i + “ “);

}

Which of the following best explains how changing i > 0 to i >= 0 will change the result?

  1. An additional value will be printed, as the for loop will iterate one additional time.
  2. There will be no change in the program because the for loop will iterate the same number of times.
  3. One fewer value will be printed, as the for loop will iterate one fewer time.
  4. This program will result in an infinite loop, as the condition will never be false.
A
  1. There will be no change in the program because the for loop will iterate the same number of times
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Consider the following method:

What value word would be printed as a result of the call mix(“Hippopotamus”, “p”)?

A

Hi0o0tamus

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

Consider the following incomplete code segment, which is intended to increase the value of each digit in a String by one. For example, if num is 12345, then the resulting String would be 23456.

Which of the following should replace /* Missing Loop Header */ so that the code segment works as intended?

  1. counter < num.length() - 1
  2. counter <= num.length()
  3. counter < num.length()
  4. counter > num.length()
  5. counter < num.indexOf(counter)
A
  1. counter < num.length()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Consider the following code segment:

Which of the following best explains the result of changing j < 4 to j > 4 on line 1?

  1. The numbers will be printed in reverse order compared to the original because the outer loop will occur in reverse order.
  2. The program will produce the same result, as the number of iterations in the outer loop hasn’t changed.
  3. An infinite loop will occur because the termination of the loop will never be reached.
  4. No output will be produced, as the boolean condition will never be met in the outer for loop.
A
  1. No output will be produced, as the boolean condition will never be met in the outer for loop
18
Q

What, if anything, is printed as a result of executing this statement?

  1. 150
  2. 310
  3. 10204080
  4. 10204080160
  5. There will not be any result printed, as ints cannot be converted to type String.
A
  1. 10204080
19
Q

What are while loops?

A

while (boolean expression)

{

//code executes until false

}

20
Q

What are infinite loops?

A

Occurs when the expression in a while loop never evaluates to false. The program continues to run infinitely

21
Q

What is the purpose of “break?”

A

Breaks out of a while loop and executes statement that immediately follows while loop

22
Q

What is the purpose of return?

A

Keyword used in methods to return a value back to the initial program that called the method

23
Q

How can you avoid infinity loops?

A
  1. Add a condition to the while loop
  2. Add a “break” statement
  3. Add a return keyword
24
Q

What are for loops?

A
  • Another tool we can use to repeat code sequences
  • for loops allows repeating a set of statement a specfic number of times!

for(variable initialization; boolean expression; increment)

{

//will execute if boolean is true, and until the boolean expression is false

}

  • variable initialization: The initialization; this variable controls the for loop execution
  • boolean expression: if the expression is false, the for loop will not execute
  • increment: changes the value of the loop control variables
25
Q

What is “off by one error?”

A

When a for loop iterates one too few or one to many times, it’s referred to as an Off by One Error

26
Q

What is the difference between for and while loops?

A

While loops work better for programs with undetermined amounts of iterations

For loops are best for programs with a predetermined number of iterations

27
Q

What are string transversals?

A
  • The substring() method is particularly useful when attempting to traverse Strings
  • TRAVERSING is the process of going through a String one character at a time, often using loops

General String Traversal:

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

{

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

}

28
Q

How can you transverse a string using charAt()?

A
  • We can also use chartAt(int index) to access individual characters in a String

General String Traversal:

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

{

char character = string.charAt(i);

}

29
Q

How do you transverse a string to determine how many characters are upperclass?

A

public int numUpperCase(String string)

{

int counter = 0;

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

{

char character = string.charAt(i); //iterates through each character if(isUpperCase(character))

//if the character is uppercase, add one to counter

{

counter++;

}

}

return counter;

}

30
Q

How do you transverse a string to replace a substring with another substring?

A
31
Q

How do you transverse a string to create this method using a while loop?

A

Using the indexOf() method, we will continue to check the string while the String to remove is in string

32
Q

How do you create a method that reverses the order of a string?

A
33
Q

What does “\t” do?

A

Inserts a new tab at specific point in the text

34
Q

What are nested loops?

A

📎 Putting a loop inside another loop in called nesting

The number of times a nested loop will run is calculated by the number of iterations in the inner loop * iteration of the outer loop

35
Q

What is an algorithm?

A

Step-by-step process that solves a problem

36
Q

What is the statement execution count?

A

The number of times a statement is executed by the program

37
Q

What is Big-O Notation?

A

A way to represent how long an algorithm will take the execute helps to determine how efficient different approaches are.

38
Q

What makes a good algorithm?

A
  1. Correctness
  2. Efficiency (run time and memory requirements)
    • Important because it affects program speed, and program cost
  3. Easy to understand by someone else
39
Q

What is Efficiency Cost in algorithms?

A

📎 An efficient algorithm is one that makes economical use of processing time and computer money

Questions Asks:

  1. How long does it take to perform this task?
  2. Is there another approach that will get the answer more quickly?

Running a computer uses CPU Processing Time

  • Processing Time is the amount of the time the computer spends processing program instructions
  • Computer has a limit to its CPU usage at any given time Computer slows down when computer reaches it upper limit - when applications use to much processing time

Speed also affects program cost

  • Serves cost money to run - more CPU usage, the most servers cost
  • Limiting process time reduces CPU usage, and can save money on server cost
40
Q

How do you measure algorithmic performance?

A

📎 Important to be able to measure (or make an educated guess) about the space and time needed to execute an algorithm = able to compare alternative approaches to a problem you need to solve

  • Absolute time of an algorithm can’t be predicted: