Chapter 3 Decision Structure (Review)

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

You can use this method to display formatted output in a console window.

System.out.printf

2. Explain why a misplaced semicolon can cause an if statement to operate incorrectly.

because the if statement isn't complete without its conditionally executed statement

This type of expression has a value of either true or false.

boolean expression

To create a block of statements, you enclose the statements in these.

braces {}

1. // Warning! This code contains ERRORS! if (x == 1); y = 2; else if (x == 2); y = 3; else if (x == 3); y = 4;

Each if clause is prematurely terminated by a semicolon.

True or False: The = operator and the == operator perform the same operation.

False

True or False: When an if statement is nested in the else clause of another statement,the only time the inner if statement is executed is when the boolean expression of the outer if statement is true.

False

8. Briefly describe how the || operator works.

The || operator is known as the logical OR operator. It takes two boolean expressions as operands and creates a boolean expression that is true when either of the subexpressions is true.

This section of a switch statement is branched to if none of the case expressions match the switch expression.

default

True or False: The scope of a variable is limited to the block in which it is defined.

True

4. What happens when you compare two String objects with the == operator?

You compare two String object's addresses.

This is a boolean variable that signals when some condition exists in the program.

flag

2. // Warning! This code contains an ERROR! if (average = 100) System.out.println("Perfect Average!");

if clause uses assign operator instead condition operator.

This is an if statement that appears inside another if statement.

nested if statement

This is an empty statement that does nothing.

null statement

When determining whether a number is inside a range, it's best to use this operator.

&&

How does the character 'A' compare to the character 'B'?

'A' is less than 'B'

10. When does a constructor execute? What is its purpose?

??????

5. Explain the purpose of a flag variable. Of what data type should a flag variable be?

A flag is a boolean variable that signals when some condition exists in the program. When the flag variable is set to false, it indicates the condition does not yet exist. When the flag variable is set to true, it means the condition does exist.

5. The following statement should determine whether x is not greater than 20. What is wrong with it? if (!x > 20)

The ! operator is only applied to the variable x, not the expression. The code should read: if (!(x > 20))

True or False: When an if statement is nested in the if clause of another statement, the only time the inner if statement is executed is when the boolean expression of the outer if statement is true.

True

&& , || , and ! are __________.

logical operators

4. // Warning! This code contains ERRORS! switch (score) { case (score > 90): grade = 'A'; break; case(score > 80): grade = 'b'; break; case(score > 70): grade = 'C'; break; case (score > 60): grade = 'D'; break; default: grade = 'F'; }

switch case cannot use boolean.

3. Why is it good advice to indent all the statements inside a set of braces?

By indenting the conditionally executed statements, you are causing them to stand out visually. This is so you can tell at a glance what part of the program the if statement executes.

1. Explain what is meant by the phrase "conditionally executed."

Conditionally executed code is executed only under a condition, such as an expression being true.

7. Briefly describe how the && operator works.

It takes two boolean expressions as operands and creates a boolean expression that is true only when both subexpressions are true.

7. The following statement should determine whether count is outside the range of 0 through 100. What is wrong with it? if (count < 0 && count > 100)

The statement should use the || operator instead of the && operator.

3. // Warning! This code contains ERRORS! if (num2 == 0) System.out.println("Division by zero is not possible."); System.out.println("Please run the program again "); System.out.println("and enter a number besides zero."); else Quotient = num1 / num2; System.out.print("The quotient of " + Num1); System.out.print(" divided by " + Num2 + " is "); System.out.println(Quotient);

The conditionally executed blocks of code should be enclosed in braces.

9. Assume that partNumber references a String object. The following if statement should perform a case-insensitive comparison. What is wrong with it? if (partNumber.equals("BQ789W4")) available = true;

The equalsIgnoreCase method should be used instead of the equals method.

10. What is wrong with the following code? double value = 12345.678; System.out.printf("%.2d", value);

The statement should be System.out.printf("%.2.f", value);

6. The following statement should determine whether count is within the range of 0 through 100. What is wrong with it? if (count >= 0 || count <= 100)

The statement should use && operator instead of the || operator

9. Why are the relational operators called "relational"?

They determine whether a specific relationship exists between two values. The relationships are greater-than, less-than, equal-to, not equal-to, greater-than or equal-to, and less-than or equal-to.

6. What risk does a programmer take when not placing a trailing else at the end of an if-else-if statement?

This is not a syntax error, but can lead to logical errors. If you omit the trailing else from an if-else-if statement, no code will be executed if none of the statement's boolean expressions are true.

The conditional operator takes this many operands.

Three

True or False: A conditionally executed statement should be indented one level from the if clause.

True

True or False: All lines in a conditionally executed block should be indented one level.

True

The if statement is an example of a __________.

decision structure

>, <, and == are __________.

relational operators

8. The following statement should assign 0 to z if a is less than 10; otherwise, it should assign 7 to z. What is wrong with it? z = (a < 10) : 0 ? 7;

the ? should before :

An else clause always goes with __________.

the closest previous if clause that doesn't already have its own else clause

This determines whether two different String objects contain the same string.

the equals method


Ensembles d'études connexes

9) The Axial Skeleton: The Vertebral Column

View Set

Crisis 1 Final Practice Questions

View Set

*IMPORTANT* Valvular Heart Disorders 34Qw/exp

View Set