Java Chapter 3 Test Questions – Flashcards

Unlock all answers in this set

Unlock answers
question
Like all high-level programming languages, Java provides _ :statements that let you choose actions with alternative courses.
answer
selection statements
question
_ use conditions that are Boolean expressions. A Boolean expression is an expression that evaluates to a Boolean value: true or false. We now introduce Boolean types and relational operators
answer
Selection statements
question
How do you compare two values, such as whether a radius is greater than 0, equal to 0, or less than 0? Java provides __ operators (also known as _ operators), shown in Table 3.1, which can be used to compare two values
answer
six relational, comparison
question
less than or equal to (as relational operator)
answer
<=
question
equal to (as relational operator)
answer
==
question
not equal to (as relational operator)
answer
!=
question
The _ testing operator is two equal signs (==), not a single equal sign (=). The latter symbol is for _.
answer
equality, assignment
question
double radius = 1; System.out.println(radius > 0); this displays:
answer
true
question
A variable that holds a Boolean value is known as a
answer
Boolean variable
question
A variable that holds a Boolean value is known as a Boolean variable. The boolean _ is used to declare Boolean variables.
answer
data type
question
assign true to lightsOn
answer
boolean lightsOn = true;
question
True and false are _, just like a number such as 10. They are treated as reserved words and cannot be used as identifiers in the program.
answer
literals
question
An _ is a construct that enables a program to specify alternative paths of execution.
answer
if statement
question
Java has several types of selection statements: _ if statements, _ if-else statements, _ statements, _ if-else statements, _ statements, and _ expressions.
answer
one-way, two-way, nested if, multi-way, switch, conditional expressions
question
A one-way if statement executes an action if and only if the condition is true. The syntax for a one-way if statement is:
answer
if (boolean-expression) { statement; }
question
The block braces can be omitted if they enclose a _. For example, the following statements are equivalent. if (i > 0) System.out.println("i is positive");
answer
single statement
question
(1) If the number is a multiple of 5, the program displays HiFive. If the number is divisible by 2, it displays HiEven. (Just write if statement)
answer
if (number % 5 == 0)
question
An _ statement decides the execution path based on whether the condition is true or false.
answer
if-else
question
The inner if statement is said to be _ inside the outer if statement.
answer
nested
question
style, called _, avoids deep indentation and makes the program easy to read
answer
multi-way if-else statements
question
The following errors are common among new programmers. Common Error 1: forgetting _
answer
Forgetting Braces
question
Common Error 2: Wrong Semicolon at the
answer
if Line
question
if (radius >= 0); same as if (radius >= 0) { } What type of error is this?
answer
empty block
question
if (radius >= 0); same as if (radius >= 0) { }. This mistake is hard to find, because it is neither a compile error nor a runtime error; it is a _.
answer
logic error
question
Common Error 3: Redundant Testing of
answer
Boolean Values
question
if (even == true) is redundant. Better:
answer
if (even)
question
if (even = true) is wrong but even so, won't show a _ error. Should be:
answer
compile, ==
question
Common Error 4: __ Ambiguity
answer
Dangling else
question
Common Error 5: _of Two Floating-Point Values
answer
Equality Test
question
double x = 1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1; System.out.println(x == 0.5); Here, x is not exactly 0.5, but is 0.5000000000000001. You cannot reliably test equality of two _ values.
answer
floating-point
question
you can compare whether they are close enough by testing whether the difference of the two numbers is less than some threshold. That is, two numbers x and y are very close if |x?y| ; e for a very small value, e. e, a Greek letter pronounced _, is commonly used to denote a very small value
answer
epsilon
question
Normally, you set e to _for comparing two values of the double type and to _ for float
answer
1E-14, 1E-7
question
setup the EPSILON variable for a floating variable
answer
final double EPSILON = 1E-14;
question
if statement to check whether 0.5000000000000001 (x) is approximately 0.5
answer
if (Math.abs(x - 0.5) ; EPSILON)
question
delete
answer
d
question
The _ method can be used to return the absolute value of a
answer
Math.abs(a)
question
improve: Often, new programmers write the code that assigns a test condition to a boolean variable like the code in (a): if (number % 2 == 0) even = true; else even = false;
answer
boolean even = number % 2 == 0;
question
You can use _ to obtain a random double value between 0.0 and 1.0, excluding 1.0.
answer
Math.random()
question
The previous programs generate random numbers using System.currentTimeMillis(). A better approach is to use the random() _ in the Math _.
answer
method, class
question
A better approach is to use the random() method in the Math class. Invoking this method returns a random value of the type _.
answer
double
question
_ returns a random single-digit integer (i.e., a number between 0 and 9).
answer
(int)(Math.random() * 10)
question
// 1. Generate a random single-digit integer, number1 between 1-10
answer
int number1 = (int)(Math.random() * 10);
question
// If number1 ; number2, swap number1 with number2. First line in program (ignoring setup parameters)
answer
if (number1 ; number2) {
question
// If number1 ; number2, swap number1 with number2 line 2
answer
int temp = number1;
question
// If number1 ; number2, swap number1 with number2 line 3
answer
number1 = number2;
question
// If number1 ; number2, swap number1 with number2 line 4 (Swap)
answer
number2 = temp;
question
// If number1 ; number2, swap number1 with number2 line 5: the closing line
answer
}
question
_ (line 53) is defined in the System class. Invoking this method terminates the program. The status 0 indicates that the program is terminated normally. A nonzero status code indicates abnormal termination.
answer
System.exit(status)
question
System.exit(status) (line 53) is defined in the System class. Invoking this method terminates the program. The status 0 indicates that the program is terminated _.
answer
normally
question
System.exit(status) (line 53) is defined in the System class. Invoking this method terminates the program. A _ status code indicates abnormal termination.
answer
nonzero
question
An initial value of 0 is assigned to tax (line 20). A _ error would occur if it had no initial value, because all of the other statements that assign values to tax are within the if statement. The compiler thinks that these statements may not be executed and therefore reports a compile error.
answer
compile
question
For all programs, you should write a small amount of code and test it before moving on to add more code. This is called _ development and testing
answer
incremental
question
_ operators, also known as Boolean operators, operate on Boolean values to create a new Boolean value.
answer
Logical
question
the __ operator, which negates true to false and false to true.
answer
not (!)
question
The __ of two Boolean operands is true if and only if both operands are true
answer
and (;;)
question
The __ of two Boolean operands is true if at least one of the operands is true
answer
or (||)
question
The __ of two Boolean operands is true if and only if the two operands have different Boolean values
answer
exclusive or (^)
question
Note that p1 ^ p2 is the same as _
answer
p1 != p2
question
!
answer
logical negation
question
;;
answer
logical conjunction
question
||
answer
logical disjunction
question
^
answer
logical exclusion
question
Example (assume age = 24, weight = 140) is false, because (age ; 18) is _.
answer
!(age ; 18), true
question
_ is true, because (weight == 150) is false.
answer
!(weight == 150)
question
(age ; 18) _ (weight ;= 140) is true, because (age ; 18) and (weight ;= 140) are both true
answer
;;
question
(age ; 34) _ (weight ;= 150) is false, because (age ; 34) and (weight ;= 150) are both false
answer
||
question
(age ; 18) _ (weight 18) is true.
answer
|| (or operator)
question
(age ; 34) ^ (weight ; 140) is _, because (age ; 34) and (weight ; 140) are both false.
answer
false
question
(age ; 34) ^ (weight ;= 140) is _, because (age ; 34) is false but (weight ;= 140) is true
answer
true
question
if (number % 2 == 0 || number % 3 == 0) System.out.println(number + "__"); ;;;
answer
is divisible by 2 or 3
question
if (number % 2 == 0 ^ number % 3 == 0) System.out.println(number + "is __");
answer
divisible by 2 or 3 but not both
question
1 ;= no ;= 31 should be
answer
(1 ;= no) ;; (no ;= 31)
question
In mathematics, the expression 1 ;= numberOfDaysInAMonth ;= 31 is correct. However, it is incorrect in Java, because 1 ;= numberOfDaysInAMonth is evaluated to a __, which cannot be compared with 31.
answer
boolean value
question
_'s law, named after Indian-born British mathematician and logician can be used to simplify Boolean expressions. The law states: !(condition1 && condition2) is the same as !condition1 || !condition2 !(condition1 || condition2) is the same as !condition1 && !condition2
answer
De Morgan
question
!(number == 2 || number == 3) is better written as
answer
number != 2 && number != 3
question
In programming language terminology, && and || are known as the _ or _ operators
answer
short-circuit, lazy
question
A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400. Collect the input for year:
answer
int year = input.nextInt();
question
Generate a lottery number (0-100) & call the variable lottery
answer
int lottery = (int)(Math.random() * 100);
question
enter a guess of lottery number
answer
int guess = input.nextInt();
question
A _ statement executes statements based on the value of a variable or an expression.
answer
switch
question
Here is the full syntax for the switch statement: switch (switch-expression) { case value1: statement(s)1; ___; ...
answer
break
question
Here is the full syntax for the switch statement: switch (switch-expression) { ___ statement(s)1; break; case value2: statement(s)2; break; ... case valueN: statement(s)N; break; default: statement(s)-for-default; }
answer
case value1:
question
Here is the full syntax for the switch statement: switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; ... case valueN: statement(s)N; break; __ }
answer
default: statement(s)-for-default;
question
Here is the full syntax for the switch statement: ____ case value1: statement(s)1; break; case value2: statement(s)2; break; ... case valueN: statement(s)N; break; default: statement(s)-for-default; }
answer
switch (switch-expression) {
question
The value1, . . ., and valueN must have the same __ as the value of the switchexpression. Value1, . . ., & valueN are ___ expressions, meaning that they cannot contain variables, such as 1 + x.
answer
data type, constant
question
When the value in a case statement matches the value of the switch-expression, the statements starting from this case are executed until either a _ _ or the end of the switch statement is reached
answer
break statement
question
The _ which is optional, can be used to perform actions when none of the specified cases matches the _.
answer
default case, switch-expression
question
The _ case, which is _, can be used to perform actions when none of the specified cases matches the switch-expression
answer
default, optional
question
The keyword break is _. The break statement immediately ends the switch statement.
answer
optional
question
Do not forget to use a break statement when one is needed. Once a case is matched, the statements starting from the matched case are executed until a _ statement or the __ statement is reached
answer
break, end of the switch
question
Do not forget to use a break statement when one is needed. Once a case is matched, the statements starting from the matched case are executed until a break statement or the end of the switch statement is reached. This is referred to as _.
answer
fall-through behavior
question
To avoid programming errors and improve code maintainability, it is a good idea to put a _ in a case clause if break is purposely omitted.
answer
comment
question
A __ evaluates an expression based on a condition
answer
conditional expression
question
if (x > 0) y = 1; else y = -1; Alternatively, as in the following example, you can use a conditional expression to achieve the same result.
answer
y = (x > 0) ? 1 : -1;
question
Conditional expressions are in a completely different style, with no explicit if in the statement. The syntax is:
answer
boolean-expression ? expression1 : expression2;
question
Suppose you want to assign the larger number of variable num1 and num2 to max. You can simply write a statement using the conditional expression:
answer
max = (num1 > num2) ? num1 : num2;
question
The symbols ? and : appear together in a conditional expression. They form a _ and also called a _ operator because it uses three operands.
answer
conditional operator, ternary (It is the only ternary operator in Java.)
question
Operator _ and _ determine the order in which operators are evaluated.
answer
precedence, associativity
question
If operators with the same precedence are next to each other, their associativity determines the order of evaluation. All binary operators except assignment operators are __.
answer
left associative
question
Assignment operators are right associative. Therefore, the expression a = b += c = 5 is equivalent to
answer
a = (b += (c = 5))
question
Finding logic errors, on the other hand, can be very challenging. Logic errors are called _. The process of finding and correcting errors is called debugging
answer
bugs
question
A common approach to debugging is to use a combination of methods to help pinpoint the part of the program where the bug is located. You can _ the program (i.e., catch errors by reading the program), or you can insert print statements in order to show the values of the variables or the execution flow of the program.
answer
hand-trace
question
JDK includes a command-line debugger, _, which is invoked with a class name. _ is itself a Java program, running its own copy of Java .
answer
jdb, interpreter
question
__: The debugger lets you trace all of the method calls. This feature is helpful when you need to see a large picture of the program-execution flow.
answer
Displaying call stacks
Get an explanation on any task
Get unstuck with the help of our AI assistant in seconds
New