Chapter 3 Book Quiz Flashcards

1
Q

The if statement is an example of a .
a. sequence structure
b. decision structure
c. pathway structure
d. class structure

A

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.

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

This type of expression has a value of either true or false .
a. binary expression
b. decision expression
c. unconditional expression
d. boolean expression

A

d. boolean expression
Explanation: A boolean expression is one that evaluates to either true or false.

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

> , < , and == are .
a. relational operators
b. logical operators
c. conditional operators
d. ternary operators

A

a. relational operators
Explanation: Relational operators compare two values and return a boolean result, such as >, <, and ==.

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

&& , || , and ! are .
a. relational operators
b. logical operators
c. conditional operators
d. ternary operators

A

b. logical operators
Explanation: These operators (&&, ||, and !) are used to combine or negate boolean expressions in Java.

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

This is an empty statement that does nothing.
a. missing statement
b. virtual statement
c. null statement
d. conditional statement

A

c. null statement
Explanation: A null statement, also called an empty statement, does nothing and is represented by a semicolon ;.

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

To create a block of statements, you enclose the statements in these.
a. parentheses ()
b. square brackets []
c. angled brackets <>
d. braces {}

A

d. braces {}
Explanation: A block of code (or a compound statement) is enclosed in braces {}.

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

This is a boolean variable that signals when some condition exists in the program.
a. flag
b. signal
c. sentinel
d. siren

A

a. flag
Explanation: A flag is a boolean variable used to track or signal when a particular condition is true or false.

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

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

A

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’.

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

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

a. nested if statement
Explanation: A nested if statement is an if statement that appears inside another if statement.

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

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

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.

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

When determining whether a number is inside a range, it’s best to use this operator.
a. &&
b. !
c. ||
d. ? :

A

a. &&
Explanation: The && (logical AND) operator is used to combine conditions, which is ideal when checking if a number lies within a specific range.

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

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

A

c. the equals method
Explanation: The equals method is used to compare the contents of two String objects for equality.

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

The conditional operator takes this many operands.
a. one
b. two
c. three
d. four

A

c. three
Explanation: The conditional (ternary) operator ? : takes three operands: the boolean condition, the value if true, and the value if false.

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

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

A

b. default
Explanation: The default section of a switch statement is executed when no case matches the value of the switch expression.

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

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

A

c. System.out.printf
Explanation: The System.out.printf method is used to display formatted output to the console in Java.

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

TRUE OR FALSE: The = operator and the == operator perform the same operation.

A

FALSE
Explanation: The = operator is used for assignment, while the == operator is used for comparison.

17
Q

TRUE OR FALSE: A conditionally executed statement should be indented one level from the if clause.

A

TRUE
Explanation: Indentation is used to improve readability, and conditionally executed statements are typically indented one level from the if clause.

18
Q

TRUE OR FALSE: All lines in a conditionally executed block should be indented one level.

A

TRUE
Explanation: Indenting all lines in a conditionally executed block helps improve readability and structure.

19
Q

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 .

A

TRUE
Explanation: The inner if statement will only execute if the condition of the outer if statement is true.

20
Q

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 .

A

FALSE
Explanation: The inner if in the else block will execute only if the condition of the outer if statement is false.

21
Q

TRUE OR FALSE: The scope of a variable is limited to the block in which it is defined.

A

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.

22
Q

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;

A

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.

23
Q

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

A

corrected code: if (average == 100)
System.out.println(“Perfect Average!”);

Error: Using the assignment operator (=) instead of the equality operator (==) in the if condition.

24
Q

// 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)

A

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.

25
Q

// 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’;
}

A

Corrected Code:
Instead of using boolean conditions in the case labels, we can use ranges and replace the switch statement with an if-else chain. Here’s how to modify the program:

// Corrected code
if (score > 90) {
grade = ‘A’;
} else if (score > 80) {
grade = ‘B’;
} else if (score > 70) {
grade = ‘C’;
} else if (score > 60) {
grade = ‘D’;
} else {
grade = ‘F’;
}

A switch statement is designed to work with constant values or simple expressions (like integers, characters, or enums), not with complex boolean expressions like score > 90. Hence, the most appropriate solution for this type of range-based logic is an if-else structure.

26
Q

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

A

if (!(x > 20))
This checks if x is not greater than 20 by applying the negation to the expression x > 20. If x > 20 is false, then !(x > 20) will be true, and the block will execute.

Problem: The ! operator cannot be applied to a numeric value like x.
Solution: Use the comparison operator <= to check if x is not greater than 20, or use !(x > 20) to apply the negation correctly.

27
Q

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)

A

Correcting the Statement:
To check if count is within the range of 0 through 100, you need to use the logical AND (&&) operator, not the OR (||) operator, because you want both conditions to be true at the same time. Here’s the corrected version:

if (count >= 0 && count <= 100)

Problem: The OR operator (||) was used, which caused the condition to always be true.
Solution: Use the AND operator (&&) to ensure that count is both greater than or equal to 0 and less than or equal to 100.

28
Q

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)

A

Correcting the Statement:
To determine whether count is outside the range of 0 through 100, you should use the logical OR (||) operator. This way, you check if count is either less than 0 or greater than 100.

Here’s the corrected version:

if (count < 0 || count > 100)

Problem: The conditions count < 0 && count > 100 are contradictory and will never be true at the same time.
Solution: Use the OR (||) operator to check if count is less than 0 or greater than 100.

29
Q

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;

A

Corrected Version:
To fix the statement and assign 0 to z if a is less than 10, otherwise assign 7 to z, you should write:

z = (a < 10) ? 0 : 7;

Problem: The ternary operator syntax was incorrect because the order of operands was wrong.
Solution: Use the correct syntax: condition ? value_if_true : value_if_false;

30
Q

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;

A

Corrected code:
if (partNumber.equalsIgnoreCase(“BQ789W4”))
available = true;

Problem: The equals() method performs a case-sensitive comparison.
Solution: Use equalsIgnoreCase() to perform a case-insensitive comparison

31
Q

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

A

Corrected Code:
If you want to print the double value rounded to two decimal places, you should use:

double value = 12345.678;
System.out.printf(“%.2f”, value);

The d specifier is for integers, and the f specifier is for floating-point numbers. For formatting a double, you should use %.2f instead of %.2d.