Chapter 3 Book Quiz Flashcards
(31 cards)
The if statement is an example of a .
a. sequence structure
b. decision structure
c. pathway structure
d. class structure
b. decision structure
Explanation: The if statement is used to make decisions based on a boolean condition, which is why it is classified as a decision structure.
This type of expression has a value of either true or false .
a. binary expression
b. decision expression
c. unconditional expression
d. boolean expression
d. boolean expression
Explanation: A boolean expression is one that evaluates to either true or false.
> , < , and == are .
a. relational operators
b. logical operators
c. conditional operators
d. ternary operators
a. relational operators
Explanation: Relational operators compare two values and return a boolean result, such as >, <, and ==.
&& , || , and ! are .
a. relational operators
b. logical operators
c. conditional operators
d. ternary operators
b. logical operators
Explanation: These operators (&&, ||, and !) are used to combine or negate boolean expressions in Java.
This is an empty statement that does nothing.
a. missing statement
b. virtual statement
c. null statement
d. conditional statement
c. null statement
Explanation: A null statement, also called an empty statement, does nothing and is represented by a semicolon ;.
To create a block of statements, you enclose the statements in these.
a. parentheses ()
b. square brackets []
c. angled brackets <>
d. braces {}
d. braces {}
Explanation: A block of code (or a compound statement) is enclosed in braces {}.
This is a boolean variable that signals when some condition exists in the program.
a. flag
b. signal
c. sentinel
d. siren
a. flag
Explanation: A flag is a boolean variable used to track or signal when a particular condition is true or false.
How does the character ‘A’ compare to the character ‘B’?
a. ‘A’ is greater than ‘B’
b. ‘A’ is less than ‘B’
c. ‘A’ is equal to ‘B’
d. You cannot compare characters
b. ‘A’ is less than ‘B’
Explanation: Characters in Java are compared based on their Unicode values, and ‘A’ has a lower Unicode value than ‘B’.
This is an if statement that appears inside another if statement.
a. nested if statement
b. tiered if statement
c. dislodged if statement
d. structured if statement
a. nested if statement
Explanation: A nested if statement is an if statement that appears inside another if statement.
An else clause always goes with .
a. the closest previous if clause that doesn’t already have its own else clause
b. the closest if clause
c. the if clause that is randomly selected by the compiler
d. none of these
a. the closest previous if clause that doesn’t already have its own else clause
Explanation: The else clause is associated with the closest if that does not already have its own else.
When determining whether a number is inside a range, it’s best to use this operator.
a. &&
b. !
c. ||
d. ? :
a. &&
Explanation: The && (logical AND) operator is used to combine conditions, which is ideal when checking if a number lies within a specific range.
This determines whether two different String objects contain the same string.
a. the == operator
b. the = operator
c. the equals method
d. the stringCompare method
c. the equals method
Explanation: The equals method is used to compare the contents of two String objects for equality.
The conditional operator takes this many operands.
a. one
b. two
c. three
d. four
c. three
Explanation: The conditional (ternary) operator ? : takes three operands: the boolean condition, the value if true, and the value if false.
This section of a switch statement is branched to if none of the case expressions match the switch expression.
a. else
b. default
c. case
d. otherwise
b. default
Explanation: The default section of a switch statement is executed when no case matches the value of the switch expression.
You can use this method to display formatted output in a console window.
a. Format.out.println
b. Console.format
c. System.out.printf
d. System.out.formatted
c. System.out.printf
Explanation: The System.out.printf method is used to display formatted output to the console in Java.
TRUE OR FALSE: The = operator and the == operator perform the same operation.
FALSE
Explanation: The = operator is used for assignment, while the == operator is used for comparison.
TRUE OR FALSE: A conditionally executed statement should be indented one level from the if clause.
TRUE
Explanation: Indentation is used to improve readability, and conditionally executed statements are typically indented one level from the if clause.
TRUE OR FALSE: All lines in a conditionally executed block should be indented one level.
TRUE
Explanation: Indenting all lines in a conditionally executed block helps improve readability and structure.
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
Explanation: The inner if statement will only execute if the condition of the outer if statement is true.
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
Explanation: The inner if in the else block will execute only if the condition of the outer if statement is false.
TRUE OR FALSE: The scope of a variable is limited to the block in which it is defined.
TRUE
Explanation: A variable’s scope refers to the part of the program where it can be accessed, which is limited to the block or method where it is defined.
Find the errors in the following code:
1.
// Warning! This code contains ERRORS!
if (x == 1);
y = 2;
else if (x == 2);
y = 3;
else if (x == 3);
y = 4;
Corrected Code: if (x == 1) {
y = 2;
} else if (x == 2) {
y = 3;
} else if (x == 3) {
y = 4;
}
Semicolon after if/else if conditions: The semicolon causes the code block to terminate prematurely, so the code within the if, else if, and else statements doesn’t execute correctly.
Correct use of braces {}: To group statements within if/else if conditions, braces must be used.
// Warning! This code contains an ERROR!
if (average = 100)
System.out.println(“Perfect Average!”);
corrected code: if (average == 100)
System.out.println(“Perfect Average!”);
Error: Using the assignment operator (=) instead of the equality operator (==) in the if condition.
// 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)
Corrected Code:
// 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);
}
Used curly braces {} to group statements inside the if and else blocks.
Corrected variable names to match the case (num1 and num2).
Added a missing semicolon at the end of the System.out.println(Quotient) statement.
Fixed formatting for better readability.