CSCI Ch 5 Review (Conditionals and Loops)

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

SR 5.9 How do block statements help us in the construction of conditionals?

A block statement groups several statements together. We use them to define the body of an if statement or loop when we want to do multiple things based on the boolean condition.

What would the following program do? if (total != sum) System.out.println("total does NOT equal sum");

It prints a sentence only if the variables total and sum do not contain the same value

SR 5.5 What is a truth table?

a truth table is a table that shows all possible truants of a boolean expression, given all possible combinations of variables and conditions.

What is the alphabetical ordering under the lexicographic ordering?

Uppercase letters come before lowercase example: "able" and "Baker" are two codes being compared. Baker would be deemed to be first because of the uppercase B.

Every iterator object in Java has a method called ____ that returns a boolean value indicating if there is at least one more item to process.

hasNext

What are the three kinds of conditional statements?

if if-else switch

SR 5.16Assuming the String variables s1 and s2 have been initialized, write an expression that prints out the two strings on separate lines in lexicographic order.

if (s1.compareTo(s2) < 0) System.out.println(s1 + "\n" + s2); else System.out.println(s2 + "\n" + s1);

What would the following program do? if (numBooks < stackCount + inventoryCount + duplicateCount) reorder = true;

the conditional first adds three values together, then compares the result to the value stored in numBooks. If numBooks is less than the other three values combined, the boolean variable reorder is set to true.

SR 5.3 What are the equality operators? The relational operators? The logical operators?

the equality operators are equal (==) and not equal (!=). The relational operators are less than (<), less than or equal to (<=), greater than (>), and greater than or equal to (>=). The logical operators are not (!), and (&&), and or (||).

In the following program, is the else clause matched to the inner of statement or the outer if statement? if ( code == 'R') if (height <= 20) System.out.println("Situation Normal"); else System.out.println("Bravo!);

the indentation in this example implies that it is part of the inner of statement and that is correct. An else clause is always match to the closest unmatched if that preceded it.

SR 5.19 What output is produced by the following code fragment? int low = 10, high = 0; while (low<=high) { System.out.println(low); low++; }

the loop is not entered, so there is no output.

SR 5.18 What output is produced by the following code fragment? int low = 0, high = 10; while (low<high) { System.out.println(low); low++; }

the output is the integers 0 through 9, printed one integer per line.

What are the results of the && operator?

the result is true of both operands are true, but false otherwise.

SR 5.6 Assuming done is a boolean variable and value is an int variable, create a truth table for the expression: (value > 0) || !done

the truth table is: Value > 0 | done | !done | (value > 0) || !done true | true | false | true true | false | true | true false | true | false | false false | false | true | true

What are the two possible values for its one operand?

true or false

Which operator hold highest precedence of the three logical operators?

1st: NOT 2nd: AND 3rd: OR

SR 5.28 Write a declaration for a variable named dice that is an ArrayList oe Die objects.

ArrayList<Die> dice = new ArrayList <Die> ();

Logical operators also use the boolean results. What are the three logical operators?

! (logical NOT) && (logical AND) || (logical OR)

The index values of an ArrayList begin at ___ ___ ___

0 not 1

SR 5.10 What is a nested if statement?

A nested if occurs when the statement inside an if or else clause i an if statement. a nested if lets the programmer make a series of decisions. Similarly, a nested loop is a loop within a loop.

SR 5.27 What type of element does an ArrayList hold?

An ArrayList generally holds references to the Objects class, which means that it can hold any type of object at all. A specie type of element can and should be specified in the ArrayList declaration ro restrict the type of objects that can be added and eliminate the need to cast the type when extracted.

SR 5.26 What are the advantages of using an ArrayList object?

An ArrayList stores and manages multiple objects at one time. It allows you to access the objects continuously as they are added and removed. An ArrayList dynamically increases its capacity as needed,

SR 5.17 What is an infinite loop? Specifically, what causes it?

An infinite loop is a repetition statement that never terminates. Speciffically, the body of the loop never causes the condition to become false.

An ______ object stores a list of objects and allows you to refer to each one by an integer index value.

ArrayList

SR 5.4 Given the following declarations, what is the value of each of the listed boolean expressions? int value1 = 5, value2 = 10; boolean done = true; a. value1 <= value2 b. (value1 + 5) >= value2 c. value1 < value2 / 2 d. value 2 != value1 e. !(value1 == value2) f.(value1 < value2) || done g. (value1 > value2) || done h. (value1 < value2) && !done i. done || !done j. ((value1 > value2) || done) && (!done || (value2 > value1))

Assuming the given declarations, the values are: a. true b. true c. false d. true e. true f. true g. true h. false i. true j. true

SR 5.8 What output is produced by the following code fragment given the assumptions below? if (num1 < num2) System.out.print("red"); if ((num1 + 5) < num2) System.out.print("white"); else System.out.print("blue"); System.out.println("yellow"); a. Assuming the value of num1 is 2 and the value of num2 is 1o? b. Assuming the value of num1 is 10 and the value of num2 is 2? c. Assuming the value of num1 is 2 and value of num2 is 2?

Based on given assumptions, the output would be: a. red white yellow b. blue yellow c. blue yellow

SR 5.13 Why must we be careful when competing floating point values for equality?

Because they are stored internally as binary numbers, comparing floating point values for exact equality will be true only if they are the same bit-by-bit. It is better to use a reasonable tolerance value and consider the difference between the two values.

A _____ statement is a collection of statements enclosed in braces that allow programers to replace a single statement.

Block

In the following program, is the else clause matched to the inner of statement or the outer if statement? if ( code == 'R') { if (height <= 20) System.out.println("Situation Normal"); } else System.out.println("Bravo!);

By using the block statement in the first if statement, we establish that the else clause belongs to the first.

______ statements (aka selection statement) allows programers to choose which statement will be executed next.

Conditional

What would the following program do? if (height <= MAX) adjustment = 0; else adjustment = MAX - height;

If the conditional is true, the first assignment statement is executed; if the condition is false, the second assignment statement is executed. only one or the other will be executed, because a boolean condition evaluates to either true or false. Note that proper indentation is used again to communicate that the statements are part of the governing if statement.

What would the following program do? if (total > amount) total = total + (amount + 1);

If the value is greater than the value in amount, the assignment statement is executed; otherwise the assignment statement is skipped.

A _____ (aka repetition statement) allows programers to execute a programming statement over and over again.

Loop

SR 5.20 What output is produced by the following code fragment? int low = 0, high = 10; while (low<=high) { System.out.println(low); high=high - low; }

Since the value of high always remains larger than the value of low, the code loops continuously, producing many lines of zeros, until the program is terminated.

What would the following program do? if (!done && (count > MAX)) System.out.println("Completed.");

The value of the boolean variable done is either true or false, and the NOT operator reverses that value. The value of count is either greater than MAX or it isn't.

Why should you not use the equality or relational operators to compare String objects?

The String class contains a method called equals that returns a boolean value that is true if the two strings being compared contain exactly the same characters and is false otherwise.

Why should you not use the equality operator (==) when comparing floating point variables?

The equality operator looks for all digits to be the same. If the numbers are slightly off, then they will not match. Finding the absolute value is helpful in troubleshooting this issue.

SR 5.25 Assume the Scanner object fileScan has been initialized to read from a file. Write a while loop that calculated the average number of characters per line of the file.

The following code prints out the average number of characters per line: int numChars = 0; int numLines = 0; String holdLine; while (fileScan.hasNext()) { numLines++; numLine = fileScan.nextLine(); numChars += holdLines.length(); } System.out.println ((double)numChars/numLines);

SR 5.1 What is meant by the flow of control through a program?

The following of control through a program determines the program statements that will be executed on a given run of the program.

Why is the following code misleading to the compiler? if (depth >= UPPER_LIMIT) delta = 100; else System.out.println("WARNING: Delta is being reset to ZERO"); delta = 0; //not part of the else clause!

The indentation and the logic of the code implies that the variable delta is reset to zero only when depth is less than UPPER_LIMIT. However, without using a block, the assignment statement that resets delta to zero is not governed by the if-else statement at all.

SR 5.21 What output is produced by the following code fragment? int low = 0, high = 10, mid; while (mid<=high) { mid = low; while (mid<=high) { System.out.print(mid +" "); mid++; } System.out.println(); low++; }

The output is 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 3 4 5 6 7 8 9 10 4 5 6 7 8 9 10 5 6 7 8 9 10 6 7 8 9 10 7 8 9 10 8 9 10 9 10 10

What is the result of the || operator?

The result is true if one or the other or bother operands are true, but false otherwise.

SR 5.14 How do we compare strings for equality?

We compare strings for equality using the equals method of the String class, which returns a boolean result. The compareTo method of the String class can also be used yo compare strings. It returns a positive, 0, or negative integer result depending on the relationship between the two strings.

SR 5.29 What output is produced by the following code fragment? ArrayList<String> na,es = new ArrayList<String>(); names.add("Andy"); names.add("Betty"); names.add(1, "chet"); names.add(1, "Don"); names.remove(2); System.out.println(names);

[Andy, Don, Betty]

SR 5.24 Devise statements that create each of the following Scanner objects. a. One for interactive input, which reads from System.in. b. One that read from the file "info.dat". c. One that reads from the String variable infoString,

a. Scanner user = new Scanner(System.in); b. Scanner infoFileScan = new Scanner(new File("info.dat")); c. Scanner infoStringScan = new Scanner(infoString);

SR 5.11 For each assumption, what output is produced by the following code fragment? if (num1 >= num2) { System.out.print(" red "); System.out.print(" orange "); } if ((num1 + 5) >= num2) System.out.print(" white "); else if ((num1 + 10) >= num2) { System.out.print(" black "); System.out.print(" blue "); } else System.out.print(" yellow "); System.out.println(" green "; a. num1 is 5 num2 is 4 b. num 1 is 5 num2 is 12 c. num 1 is 5 num2 is 27

a. red orange white yellow b. black blue green c. yellow green

Why is the compareTo method more versatile than than the equals method?

because, instead of returning a boolean value, the compareTo method returns an integer.

what type of expression evaluates your program either true or false?

boolean expression

When a ____ statement is executed, the flow of execution transfers immediately to the statement after the one governing the current flow.

break It "breaks" out of the loop

SR 5.2 What type of conditions are conditional and loops based on?

each conditional and loop is based on a boolean condition that evaluates to either true or false.

What would the following program do? if (count > 20) System.out.println("Count exceeded");

either the value stored in count is grater than 20 or it's not. If it is, the println statement is executed. If not the println is skipped and processing continues with whatever code follows.

The == and != operators are called ______ operators. They test whether two values are equal or not equal, respectfully.

equality

SR 5.12 Write an expression that will print a message based on the value of the int variable named temperature. If temperature is equal to or less than 50, it prints "It is cool." on one line and "Dress warmly." on the next. If temperature is greater than 80, it prints "It is warm." on one line and "Dress cooly." on the next. If temperature is between 50 and 80, it prints "It is pleasant." on one line and "Dress pleasantly" on the next.

if (temperature <= 50) { System.out.println("It is cool."); System.out.println("Dress warmly."); } else if (temperature > 80) { System.out.println("It is warm."); System.out.println("Dress cooly"); } else { System.out.println("It is pleasant."); System.out.println("Dress pleasantly."); }

What is the role of a sensitive value?

it indicates the end of the input of a while loop.

What would the following program do? if (total == sum) System.out.println("total equals sum");

it prints a sentence only if the variable total and sum contain the same value

A ____ is an object that has methods that allow you to process a collection of items one at a time. That is, it lets you step through each item and interact with it as needed.

iterator

! This logical operand is also known as a ___ _____

logical complement (yields the opposite value)

A ____ ___ statement allows programers to make another decision after determining the results of a previous decision.

nested if

A _____ is a string of characters that reads the same forwards or backwards. examples: radar, kayak, deified...

palindrome

____ operators let programers decide relative ordering between values. (examples: >, <, >=, <=)

relational

How does a while statement different from an if statement?

unlike the if statement, after the body id executed, the condition is evaluated again. If it is still true, the body is executed again. the is repetition continues until the condition becomes false; then processing continues with the statement after the body of the while loop.

What are the three types of loop statements?

while do for


Kaugnay na mga set ng pag-aaral

Financial Accounting Final Review

View Set

Chapter 12: Case Study Question #3

View Set

D'Antoni Case Studies- Lower Limb

View Set

Policy riders for disability protection

View Set

12 Venture John the Baptist 12/6/20 Gr 6

View Set

NR 224 Fundamentals Final Review

View Set

Intermediate Accounting: Chapter 12

View Set