Ch12 and Ch13 compsci vocab and quiz questions
How many times will the following while() loop body execute? boolean done = true; while (!done) { done = false; }
0 times
Given the following code, how many times will the while() loop body execute? int A = 1; int B = 2; while ((A < B) && (B < 5)) { A++; B++; }
3 times
What is the output of the following code? for (int i=5; i<10; i = i + 2) { System.out.print(i); }
579
When the following code is run, how many times will the '#' symbol appear on the screen? for (int i=0; i < 2; i++) { for (int j=0; j < 3; j++) { System.out.print("#"); } }
6
Loop
A program loop will execute the same block of statements more than one time.
Which of the following scenarios is a good fit for a while() loop?
All of these are a good fit <<< You want to execute a loop so long as some condition is true You want to build a loop that may never execute You want to run a loop that does not need a numeric index
Which of the following for() loops will execute 10 times
All of these will execute 10 times
Immutable
An object with contents that cannot be changed
In a flowchart, what symbol represents program flow between other shapes?
Arrow
When you are ready to convert your pseudocode into Java code, which of the following approaches is most useful?
Copy your pseudocode into the Java source file as comments to guide your Java statements
In a flowchart, what symbol represents a decision point in your code?
Diamond
In an algorithm to compare two strings for equality, what easy check and resulting action can be done first, before entering the main loop?
If the two strings have different lengths, you can return false right away
What does the pseudocode below use to identify the body of a while() loop? WHILE score LESS THAN 100 PRINT "Keep Playing" SET score = score + 1 PRINT "Your score is now 100 or more"
Indentation
How many times will the following do-while() loop execute? boolean done; do { done = false; } while (!done);
Infinite loop
What is wrong with the following while() loop? int i=0; while (i < 10) { System.out.println(i); }
Infinite loop
Which of the following best describes the type and order of the expressions that go between the for() loop parentheses
Initialization statement, test expression, change statement
What is printed to the screen by the following code? String input = "Mxyzptlk"; for (int i=0; i<input.length(); i++) { char c = input.charAt(i); if (c == 'y') { break; } System.out.print(c); }
Mx
What is printed to the screen by the following code? String input = "Mxyzptlk"; for (int i=0; i<input.length(); i++) { char c = input.charAt(i); if (c == 'y') { continue; } System.out.print(c); }
Mxzptlk
What term is used to refer to loops placed inside other loops?
Nested
Nested
Nested code is entirely contained inside an outer block of code. Both inner and outer blocks may use any mixture of flow-control logic.
If you write your own algorithm to find a substring within a string, what kind of loops are a natural fit?
Nested for() loops
Which of the following statements about algorithms is true?
Once you have a working algorithm, it is still possible to find a better or more efficient way to do something
In a flowchart, what symbol represents a specific step or action your algorithm needs to take?
Rectangle
When using the Euclidean algorithm to find the Greatest Common Divisor (GCD), what is the key step within the main algorithm loop?
Replace the larger of the two values with the difference between the two values
Which answer describes the most likely meaning of the pseudocode statement below? CALL mystery ON hauntedHouse WITH clue1, clue2
Run the "mystery" method through the "hauntedHouse" reference and pass in "clue1" and "clue2" as parameters
Calculating the area of a circle can be done with what kind of algorithm?
Sequential
In the algorithm to make change, you will determine the number of coins of what type first?
The largest coins (quarters)
In an algorithm to find prime numbers, why would your main algorithm loop stop checking for individual divisors at halfway to the target number?
There is no way for numbers larger than half of the target to divide evenly into the target
What is the purpose of pseudocode?
To help translate program requirements into a relaxed code sketch, without worrying about syntax details
What is the main purpose of flowcharts?
To help visualize the code you need to write
Given the following code, what combination of user-entered username and password values will allow the program to print "Come in!"? Scanner input = new Scanner(System.in); String username = ""; do { System.out.println("Enter username: "); username = input.nextLine(); String password = ""; while (!password.equals("knockknock")) { System.out.println("Enter password: "); password = input.nextLine(); } } while (!username.equals("boss")); System.out.println("Come in!"); input.close();
Username "boss" and password "knockknock"
Why do we use a for() loop in an algorithm to count the number of occurrences of a specific letter within a string?
We need a numeric index to get a character, and we know how many times the loop needs to iterate from the string length
At what point in the program flow through a for() loop is the loop index variable updated by the change statement
When the loop body is finished and program flow returns to the top
Return
When your method body is ready to exit, you use the __________ keyword to end the method and pass back any required data.
Body
a block of code marked by opening and closing curly braces.
Flowchart
a useful picture that helps break down your algorithm into a series of easily understood questions, answers and actions.
Sentinel
a value entered into a loop that will cause the loop logic to exit.
Flowcharts are capable of representing which kind of algorithm?
a. All of these can be represented b. Iterative algorithms c. Branching algorithms d. Sequential algorithms
Which of the following tools can be useful when trying to design an algorithm?
a. All of these can be useful b. Pseudocode c. A flowchart d. A diagram illustrating a key process or visualizing the data
Which of the following statements are true?
a. None of these are true b. You cannot place while() loops inside for() loops c. You cannot place for() loops inside other for() loops d. You cannot place while() loops inside do-while() loops
Which of the following lines correctly sets a "score" variable equal to 100 in pseudocode?
a. SET SCORE = 100 b. DECLARE score as 100 c. SCORE ← 100 d. All will work; there is no single, correct way to write pseudocode
Pseudocode
an informal, human-readable way to design code without worrying about language-specific syntax.
Prime Number
any number greater than 1 that is only evenly divisible by itself and the number 1.
Which of the following statements will cause the loop to exit, skipping remaining statements and iterations and continuing after the loop body?
break
Break
causes program execution to immediately exit the current loop.
String algorithms often rely on what string method to read a specific character by index from the string?
charAt()
Which of the following statements will cause the next loop iteration to start, skipping the remaining statements in the loop?
continue
For()
generally used to execute a set of statements repeatedly when you know the number of loop iterations in advance.
Which of the following update expressions is valid to replace the ??? phrase within the for() loop below for (int i=0; i<10; ???)
i = i + 1 i = i - 5 i++ All of these are valid
Continue
immediately skips the remainder of the statements in a loop body and starts the next loop iteration.
Increment Operator
increases the value of a numeric variable by one.
Infinite Loop
occurs when the program flow is stuck inside a loop and can never reach the exit conditions for that loop.
Euclidean Algorithm
relies on the fact that if two numbers have a common divisor, then the difference between those two numbers also shares that same common divisor.
Diamond
represents a decision point in the program flow.
Rectangle
represents one or more actions a program will take.
Which of the following statements will cause the program to exit the current method completely, skipping all remaining parts of the loop and other statements within the method?
return
Arrow
shows program flow from step to step in the algorithm.
Iteration
the act of repeating a block of code multiple times.
Algorithm
the exact sequence of steps your program takes to solve a problem.
GCD
the largest value that will divide evenly into both numbers.
Iterate
traversing or walking is the process of accessing each element in an array or list in order to do something useful with the data.
What shape will be produced on the console when the following code is run? int size = 3; for (int row=1; row <=size; row++) { for (int col=1; col<=row; col++) { System.out.print("#"); } System.out.println(); }
triangle
Do-While()
used to execute a set of statements while a condition is true, with the condition tested at the end of the loop.
While()
used to execute a set of statements while a logical expression is true.
Design Phase
where requirements are transformed into concrete, detailed plans to help you write code.